How To Use Docker Compose To Build And Run Windows Containers

Docker compose is a tool that can be used to define and run multi-container Docker applications. In this video, Matt will walk through how to create a YAML file to configure a simple 2 Windows container application. The YAML file will define 2 “services“, IIS and SQL. Once the services are defined in the YAML file, we will use “docker-compose build“ to build the services to check for any errors. Once the build completes successfully, we will us “docker-compose up“ to build and start the containers for our services. We will also go over the difference of running containers in the background with Docker Compose. Finally, we will go over how to stop and remove containers using “docker-compose down“, “docker-compose stop“ and “docker-compose rm“
Let's go over using Docker compose to build and run windows containers. I have set up a simple demo to show you how to build a couple docker containers from a yaml file which Docker compose will consume in my directory structure on the left, you can see that inside a folder called compose I have some files and folders. First, I have a folder called back end in a folder called front end each of these folders holds the files necessary to build a container if I expand front end. You'll see that I have a docker file in a folder called dub dub. Dub root my dub dub. Dub root folder contains an HTML file that I will be copying to the container at build time now, let's take a look at the front end dockerfile. It's a pretty basic build where I'm using the latest version of the Microsoft. IAS container image as my base on line 3. Copying my dub dub dub root folder to the container on line 9. Then removing the IAS start files that are in the dub dub. Dub root folder on the container by default on lines 14 and 15. I'm also specifying that I want port 80 exposed which isn't entirely necessary because the IAS container image already exposes that port. I just left it in for reference. In my back end folder the docker file is pretty similar to this one. Now let's take a look at the Docker compose file. Docker compose reads yamil files, which tell it what containers need to be built and how to build them in the Yaml file. We need to specify services to define our containers here. I'm setting up 2 services back end and front end under each service. I'm using the build keyword to layout my build instructions. I'm specifying context to be the location of the folder that contains the dockerfile for each service. Then I'm using the dockerfile keyword to specify the name of the dockerfile to use. You could put all your doctor files in the same folder if you wanted to, and name them. Something like dockerfile front end and Dockerfile back end for example, then just specify the name of the correct file for each service. I separated them into different folders to make it easier to visualize. Lastly, I'm using depends on under my front end service to specify that the front end depends on the back end. This means that the back end, will be built and started first now that we have our files all set up. Let's switch over to powershell in my powerShell window on the bottom. I have changed to my compose directory containing my files in my top window. Let's run docker images. I currently have the Microsoft Windows Server Core, and IAS container images already downloaded and if I run. Docker PS minus a you can see that I don't currently have any containers running now. I'm going to run Docker compose build which is going to use the Yaml file to build the container images so we can make sure that there are no errors. Docker compose will go through each services dockerfile and build the container images. Now that the build has completed successfully let's go back up and run docker images again. I now have 2 additional container images front end and back end. Note that if I run docker PS minus a again no containers have been started yet that is because we only ran a build. Let's go back and run. Docker compose up this time to start up our containers. You'll notice that Docker compose built and started the back end first and then the front end. That is because I specified the depends on in the Yaml file if I run. Docker PS minus a in my top window again, you'll see that we now have 2 containers running. Now in my bottom window. I was never given back control of the console. I'm still inside the Docker Compose Command to get out. I'll need to hit Control C. This will stop the running containers and exit compose. We can specify that we want everything to run in the background by using minus D when running Docker compose up. First, let me run docker PS minus a again to show you that the containers have now exited now let's run compose up again with minus D and see what happens. I have now been released back to the command line in running. Docker PS minus a shows that the containers have been started back up. Now there are a few options for stopping the containers. We could run, Docker compose kill which will forcefully stop any running containers. Docker compose stop, which will stop any running containers, including any that weren't started by the Up Command or we can run Docker compose down this option will stop any containers that were created by the Up Command. Now let's run that command and see what happens. Now that that's finished let's run docker PS minus a again. The containers have been stopped and removed alternatively stop and kill would only stop the containers, they would not have been removed. Next let's talk about scaling with Docker compose so far. We have defined and built 2. Containers then started a single instance of each container. Let's say we want to scale this deployment up with more containers. We can use this by using the scale parameter when running Docker compose up after the word scale. We need to specify the service. We are scaling front end or back end in this case followed by the equal sign then the number of instances of that service that we want started. For example, I'm going to scale front end up to 3 and back end up to 4. Now let's run that command. We can see that is creating backends one through 4. And now front ends one through 3. Once that completes. Let's run docker PS minus a again. And we can see that 4 back end containers and 3 front end containers have been created. Finally, I want to show you how to do some more cleanup when stopping containers. I mentioned that when we use, Docker compose down. It will stop and remove the containers created by the Up Command. If we want to take that a step further and remove the container images that we created we will use the RMI flag. With the RMI flag, we either need to specify all or local if we specify all then all container images that were created by the Docker compose file will be removed. If we specify local all container images that do not have a custom tag specified in the Yaml file will be removed any other images will remain back in my Yaml file on line 9. You'll notice that I have specified a custom tag of V2 for my front end if I were to specify local the front end image would not be removed. First, let me run docker PS and Docker images to show you the currently running containers and images. Now let's run the Down Command specifying RMI all. If I run docker PS and Docker images again, the containers have all been stopped and removed and the images that were created when we ran the up command have also been removed. These images will be rebuilt the next time the compose file is executed. Thank you for watching.