Skip to main content

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:

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:

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.


Popular posts from this blog

Run PowerShell code with Ansible on a Windows Host

Ansible is one of the Configuration Manager kings in the game. With it's easy-to-understand syntax and even easier to use modules, Ansible is certainly a go-to when you're picking what Configuration Management you want to use for your organization. Your question may be "but Ansible is typically on Linux and what happens when I'm in a Windows environment?". Luckily I'm here to tell you that Ansible will still work! I was pleasantly surprised with how easy it is to use Ansible on Windows with a little WinRM magic. Let's get started.

Pre-requisites for this post:
1) WinRM set up to connect to your Windows host from Ansible
2) Ansible set up for Windows Remote Management
3) SSH access to the Ansible host
4) Proper firewall rules to allow WinRM (port 5985) access from your Ansible host to your Windows host
5) Hosts file set up in Ansible that has your IP or hostname of your Windows Server.
6) At least one Linux host running Ansible and one Windows Server host …

Running PowerShell commands in a Dockerfile

As Docker continues to grow we are starting to see the containerization engine more and more on Windows. With the need for containers on Windows, we also need the same automation we get in Linux with Dockerfiles. Today we're going to create a Dockerfile that runs PowerShell cmdlets.
Prerequisites; 1. Docker for Windows
2. A code editor (VSCode preferred)

Let's go ahead and get our Dockerfile set up. Below is the Dockerfile I used for this post.

from MAINTAINER Michael Levan RUN powershell -Command Install-WindowsFeature -Name Web-Server RUN powershell -Command New-Item -Type File -Path C:\ -Name config
As you can see from the above, this is a tiny Dockerfile. What this will do is install the IIS Windows 

Feature and create a new file in C:\ called "config".
You should see something very similar to the below screenshot;

Next let's create a running container out of our image. First we'll need to run docker container ls to

 get o…

DevOps tooling in the Microsoft realm

When I really started to dive into automation and practicing DevOps with specific tooling, there were a few key players. At the time Microsoft was not one of them. They were just starting to embrace the open source world, including the art and practice of DevOps. Since then Microsoft has went all in and the tech giant has made some incredible tooling. Recently I switched to a Microsoft-heavy environment and I love it. I went from AWS/Python/Ansible/Jenkins to Azure/PowerShell/ARM/Azure DevOps. My first programming language was PowerShell so being back in the saddle allowed me to do a full circle between all of the different types of tooling in both worlds. Today I want to share some of that tooling with you.

The first thing I want to talk about is ARM. What is ARM? ARM is a configuration management tool that allows you to perform software-defined-infrastructure. Much like Ansible and Terraform, ARM allows you to define what you want your environment to look like at scale. With ARM, yo…