Docker Certification Training Course : Lecture 5

Docker Compose is a utility using which multi container deployment can be done. Docker Compose is a separate utility, it is not an in-built feature of Docker.

YAML (ain’t another markup language) is a deserialization language, something similar to json and xml. yaml follows so called identation pattern, it means that spaces must be defined properly. There are some applications to validate yaml structure, for example, yamllint ( In the Docker world, yaml is used only in Docker Compose.

First prerequisite to deploy Docker Compose is that Docker must be installed and Docker service must be up and running.

Sequence of commands to install Docker Compose :

  1. sudo curl -L “$(uname -s)-$(uname -m)” -o /usr/local/bin/docker-compose
  2. sudo chmod +x /usr/local/bin/docker-compose
  3. sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
  4. docker-compose –version

Let’s see what the following yaml script defines :

YAML script example

There are two images used in this yaml – nginx and tomcat. version: ‘3’ is the version of Docker Compose file format (version: ‘3’ and version: ‘3.0’ are equal). Also notice, that volume mapping is impossible inside DockerFile. But it is possible in Docker Compose (via yaml script). Ports to be exposed in created containers are defined for both images : 80 for nginx and 8080 for tomcat. Network mode will be discovered further. Also, restart policy is defined.

Docker Compose is considered to be better approach than DockerFile. More about Docker Compose here.

Docker Images can’t be created in Docker Compose, Docker Compose converts Docker Images into a Docker Containers. How to convert Docker Compose file into a Docker Container(s) ?

docker-compose -f <name of yaml script file> up -d – command to convert Docker Compose file into a Docker Container. -d parameter means detached mode, containers are run in the background. See more here

For example, when docker-compose command described above is applied to the yaml script displayed above, two Docker Images will be pulled from Docker Repository and two Docker Containers will be created by Docker Compose.

docker-compose -f <name of yaml script file> down – command that stops containers and removes containers, networks, volumes and images created by up. See more here

Example of creating wordpress application using Docker Compose :

WordPress application in Docker Compose

Pay attention to depends_on clause. It allows to define the sequence of starting the Docker Containers. See more here

An application may be scaled up using Docker Compose. See the following yaml :

YAML example

and the following command

docker-compose -f a.yml up –scale web=2 –scale app=3 –scale db=4 -d – command that creates 2 containers based on the image in web section, 3 in app, 4 in db


Leave a Reply

Your email address will not be published. Required fields are marked *