Docker on Windows - Part 3 Creating A Container with Docker Compose

Welcome back and thank you for checking out my third and final blog post in the series of running Docker on Windows. In the first blog post, we set up Docker on Windows. On the second, we spun up an Nginx container. Today, we're going to spin up an Nginx container, but do it the automated way with Docker Compose! Docker Compose is a manifest file of your container and it's settings. You can have one container in your Docker Compose file or multiple. Let's get started.

First, let's confirm Docker Compose is installed and running on your system. Open up PowerShell and run:

docker-compose


If you see several switches and commands, you are good to go! If not, please confirm you installed Docker with the instructions in the first blog post of this series.

Next, we're going to open up VSCode. If you do not have VSCode installed, please do so by following this link: https://code.visualstudio.com/

Once VSCode is open, we want to ensure we have a few extensions. Those two extension are YAML by RedHat and Docker.

Next, let's go ahead and open up a new file. We can save this file on our desktop.

Please Note: For production code, you'll want to get in the habit of storing this in a Git repo. That way you can write whatever Compose files you want, then git push the code up to your repo where it is safely stored. For testing purposes, we'll go ahead and put it right on our desktops.

Now, let's start writing our Compose file!

The first thing we'll specify is our API version for Docker Compose. This will be version 3.

Next, we're going to start with our service block. The service block in our case, is Nginx.

Please note the two spaces under "services" for "nginx". This is a standard YAML syntax. Please ensure to follow it so you don't receive any errors.

Let's call our image now. Remember in part 2 of the docker series when we pulled down an image? This is the same thing essentially.

Now that we have our image, let's give our container a name!

Next we're going to do a little Docker trick. One of the features of a container is to start the container > run the application > stop the container. In our case, we don't want to do that. We're going to run a command to tail anything. This essentially keeps the container running because it's a never-ending command.

Let's specify our volumes. Volumes can be used for a few purposes. Specifying persistent storage and mounting a specific file in the container. For our purposes, we want to mount a specific nginx.conf config file in our container. We will do it like so:

The first part is where my Nginx config that I want to import into my container is living. In my case, it's on my desktop. Then I have a colon separating where the config is, and where it's going. In our case it's going to /etc/nginx/nginx.conf. Look at it as the "source" is on the left and the "destination" is on the right. Putting in an Nginx config file is purely your choice. I did it for the purposes of showing a part of what volumes are.

Finally, we're going to go ahead and specify our ports.

It is very important to have that colon followed by the port again. If you don't, Dockers networking will forward your app to a different port.

Next, let's go ahead and save that to our desktop. We want to name it "docker-compose.yml" as that is the naming convention for a Docker Compose file.

Let's get back into PowerShell and kick off our Docker compose config. You'll want to run:

docker-compose up -d



What this will do is spin up the Docker Compose config file that's in your location of where you are in your shell, and the -d is to detach from the container. If you don't do that, your shell will be stuck in the "running" state of the container and if you CTRL + C, it'll kill the container.

Note that my current location in my shell is my desktop, which is where my Docker Compose file is living.

Now, let's kick it off!

My screenshot shows "creating". This was because I was testing this prior :).

Because Docker needs access to your C: drive, you may see a pop-up on the bottom right of your screen to allow access to Docker. You'll then be prompted for your password. This is, in short, a UAC prompt, but for Docker. If you are not comfortable doing that, please feel free to run this in a VM instead.

Now, let's run docker container ls to see our container running.

Finally, let's do a docker exec -ti yourcontainerid /bin/bash and cat /etc/nginx/nginx.conf

If you imported an nginx.conf file, you'll see your entries. In my case, I just created a bogus nginx.conf config for testing purposes.

There you have it! You have now successfully spun up a container in an automated fashion with Docker Compose.

I truly hope you all have enjoyed the 3 part Docker in Windows series. It was a pleasure writing the content and thank you everyone for reading.

Docker on Windows - Part 3 Creating A Container with Docker Compose
Share this