Docker Compose
Last update: January 2023
What is Docker Compose?
In the Building a multi container app section of this course, we had the problem of having to run multiple complex command to get the multi container web application up and running. For example, we had to create the images (docker build), run the containers (docker run), create the network, etc. Whenever we wanted to stop the web application, we had to stop each container manually. Running manually each of these stated commands is time consuming and prone to multiple errors. We can solve this problem by automating the start/stop of our multi containers setups with all assets, components and configuration with only one commands by using Docker compose.
Docker compose is a Docker tool aiming at easing the build of simple to complex Docker environments by automating it set ups and configuration.
Installation
Docker compose is already installed on MacOS and Windows when installing Docker Desktop. On Linux, Docker compose needs to be installed separately following the Docker documentation.
Creation of Docker Compose file
Docker Compose files are YAML files (extension .yml
). YAML files follow a specific syntax made with key:value
pair and indentation rules.
Versioning and releases
Several versions of Docker Compose exist which are different in the features offered and the syntax to use. When creating a Docker Compose file, we need to specify the version of Docker compose to use, so our instructions will be run under this version of Docker Compose.
Here is listed Docker Compose existing versions and its associated features.
Services
The services keyword is referring to the containers configuration. The docker-compose.yml
file should at least contains one service (one container).
For example, the multi containers web application Dockerized earlier is split into three containers (frontend, backend, database). Each containers represents a service as shown below. The services can be named as we wish.
Note that indentation is very important here. services is parent of three childs backend
, mongodb
, and frontend
. We can recognize the hierarchy by the double blank indentations under the services
key.
Configuration of Docker Compose file
Configuration options
Images
The image keyword refers to the image name we want to use to build the container mongodb
.
Volumes
The volumes key refers to one or multiple volumes assigned to this container. Each -
(dash) refers to one volume. The syntax to specify the named/anonymous volumes or bind mounts are the same using the Docker commands.
Specificity for named volumes
Also, when configuring named volumes the keyword volumes should be specified in the Docker compose file at the top level next to services. We can then specify the name of the named volume with the syntax <volume-name>:
This only applied to named volumes and not for bind mounts or anonymous volumes.
To specify bind mount, the following syntax should be used:
Environment variables and files
The environment keyword is used to specify the environment variables. Two syntax are possible:
We might also want to use an environment variable file in case some information such as the database credentials need to be kept private.
The env_file keyword is to specify the location of the environment variables files (relative path to the Docker Compose file).
Networks
By default, Docker Compose adds all services specified in a same file in the same default network. Therefore, it is optional to specify the network each services should be part of using the keyword networks. Though, it is up to the user to specify the networks the container should be part of using the key networks
.
Note on remove and detach mode
The remove (--rm
) or detach (-d
) mode do not need to be specified in the Docker compose file, since by default, in Docker compose, the containers are removed automatically when the services stop and the detach mode can be specified when starting the services.
Build
The build option is to be used when not using a pre built image or an image fetched from Docker Hub. The build option point to a folder where a Dockerfile exist. Docker Compose will build the container image based on the configuration found in the Dockerfile.
Two syntaxes for this option may exist.
Ports
The keyword ports allows to specify public port to expose. In the example below, we expose the container's port 80 to our localhost port 80.
Depends on
The depends_on key word should be used when a service relies on another service to be up and running. This specify to Docker Compose that a service should not be run before the other one that it depends upon is up and running.
stdin_open and tty
To run a container in interactive mode (-it
) we need to add two options in the configuration file, the stdin_open and tty options both should be set to true.
Configuring the multi containers web application
Configuring the MongoDB container
Below is an example of how to configure the MongoDB container based on the below Docker command using a Docker Compose file.
Below is an excerpt of the docker-compose.yml
file.
Configuring the backend container
In this section, we want to configure the backend component of the web application using a Docker Compose file.
The configuration will reflect the Docker command below that was used earlier in the course to configure the backend.
For the environment variables , we created a environment variable file in the env folder and added to it the MongoDB credentials. The env_file
keyword refers to the location of the environment variable file using a relative path.
Because the backend service depends upon the MongoDB database to be able to run properly, the depends_on keyword should be added to the backend service configuration.
Configuring the frontend container
In this section, we want to configure the frontend component of the web application using a Docker Compose file.
The configuration will reflect the Docker command below that was used earlier in the course to configure the frontend.
Unlike the two others containers, the frontend container needs to be run in interactive mode. This is why the service should be configured with the options stdin_open and tty.
Docker names
Docker Compose creates auto-generated name for running container against the following pattern: folder_servicename_incrementnumber. We might want Docker Compose to name the container a name of our choice.
Adding the container_name
field in Docker Compose file under each services allows us to specify a name for the running containers.
Docker Compose up & down
Start: To build, fetching and start every services include in the Docker Compose file, we only need to run one command:
The command above will build/fetch all images specified and run the containers.
By default, Docker Compose will start in attach mode.
To start in detach mode, add flag -d
Docker Compose will run existing built images when starting the services. To force Docker Compose to rebuild the image use the build flag.
Stop: To shut every services and network down. To delete the volumes, we can add the flag -v
.
Build: To build the images without starting the services.
Last updated