Use Docker Compose To Share An Entire Application

Joel Lord
InstructorJoel Lord
Share this video with your friends

Social Share Links

Send Tweet

Now that you have three containers ready to be used, you will learn how to create a docker-compose.yaml file. By using Docker Compose, you will be able to share a single YAML file and anyone will be able to run your application.

Docker-compose is part of the Docker tools suite and is an application that will take a YAML file to start all of your containers with one single command.

Using Compose is basically a three-step process:

  1. Define your app’s environment with a Dockerfile so it can be reproduced anywhere.
  2. Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.
  3. Run docker-compose up and Compose starts and runs your applications.

Compose works in all environments: production, staging, development, and testing.

Instructor: [0:00] Now that you have everything stored in containers, you will want to share this entire three-tier application with the rest of your team. So far, you've been using the command line to start manually each one of the containers. You've seen how these instructions can get complex.

[0:17] This works in your local machine, and you could even write some scripts to start and stop those containers, but there is an easier way. This is where Docker Compose will come into play. Docker Compose is part of the Docker Tool Suite and is an application that will take a YAML file to start all of your containers with one single command.

[0:36] The first thing you will need to do is to create a file called docker-compose.yml at the root folder of your project. This is where you will define all the containers that are used by your application. In this file, you will start by telling Docker Compose the version of the API to use. You can use version 3.8 here. You will also define all the services or the containers that will be in your application as well as all the networks.

[1:06] You can start by defining our database service. Before we start describing this service, let's take a look at the original command that we use to start this container. You will want to reproduce this command line inside your YAML file.

[1:21] The first step will be to give a name to this service. We will call it katescourse-db, just like the name that we gave in our command line. We then specify the image to be used. The image will be MySQL 5.7, so we can already remove this one and the name.

[1:40] Let's proceed with each one of the other parameters in our command line. The -d can be omitted because it will automatically be done by docker-compose. Let's copy those. For the ports, we will add a port property. This will take an array, and our first element in the array will be the ports that we've just copied from our docker run command.

[2:02] Let's move on to the environment variables. This will also be an array, to which we will pass each one of the environment variables that we had defined previously. We can ignore --rm as this will also be taken care of by docker-compose. We are up to our volume. Volumes will also be an array in which we will add the volume that we had before.

[2:30] We won't be able to use the current working directory, so we will simply use a directory relative to the path in which we are. Finally, we will need to specify the network that we want to use. This will be in the networks property, which is also an array, and we will specify the name of the network. That's it. we've just reproduced our full docker run line.

[2:55] Now that you've defined the database server, you can define the backend as well. Once again, let's start from the docker run command just like we did for the database. The name for this service was back. It used the image joelord/katescourse-back version 1. We've taken care of the image and the name.

[3:23] We can now mount the ports. --rm is taken care of. Let's create our environment variables. We can add the network. We can ignore the -d as well as the docker run command.

[3:56] Finally, you can do the same for the frontend. The container was called front, the image that was used is joelord/katescourse-front version 1. The ports were 8080, map to port 8080. We also had some environment variables, more specifically the base URL mapping to http://localhost:3001. This one right here. We had no need for a network. That's all we had for our frontend.

[4:36] The last thing that we will need to do is to tell Docker to create a network so that our components are able to use it. The name of our network is katescourse. We will keep all the default parameters, so we can just leave that empty object right here.

[4:55] Now that your Dockerfile is completed, Docker Compose will be able to use this to start all the necessary containers and run your application. From the root folder, use docker-compose. This will start all the services at once.

[5:10] You will see the logs in real time for each one of your services. Just like when you run a docker run command, you can use the -d flag to run all the services in detached mode, if you prefer that. To stop the services, you can use Ctrl-C if running when the logs open, or with docker-compose down if you're running everything in detached mode.

[5:31] You can now send this docker-compose YAML file to another laptop that has Docker installed. From this other machine, you can run the docker-compose up command. Docker will automatically download all the required images, configure them, and start all of your services.

[5:49] It doesn't matter how the other laptop is configured, if it has Node.js installed or MySQL, it doesn't matter which operating system it's using, the application will always run the same in any environment.

[6:03] So far, we've focused mainly on how to use containers in a development environment. You now know how to use containers and even how to build your own if needed. You've also seen how useful they can be and how easy it can be to share multiple services with your colleagues.