Docker Compose
We first learned how to run a docker container using the docker run command. What if we need to set up a complex application running multiple services?
What is Docker compose?
Docker Compose is used to run multiple containers as a single service. For example, lets imagine you have an application which required NGNIX and MySQL, with docker compose you could create one configuration file (in yaml format) called dockercompose.yml which would start both the containers as a service without the need to start each one separately.
A minimal Docker Compose application consists of three components:
A
Dockerfilefor each container image you want to build.A YAML file,
docker-compose.yml, that Docker Compose will use to launch containers from those images and configure their services.The files that comprise the application itself.
Docker Compose Installation
Compose uses the Docker Engine, so you’ll need to have the Docker Engine installed on your device. You can run Compose on Windows, Mac, and 64-bit Linux. Installing Docker Compose is actually quite easy. On desktop systems, such as Docker Desktop for Mac and Windows, Docker Compose is already included. No additional steps are needed. On Linux systems, you’ll need to:
Install the Docker Engine
Run the following command to download Docker Compos
sudo curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose3. Apply permissions to the binary, like so:
sudo chmod +x /usr/local/bin/docker-compose4. Test the installation to check it worked properly
sudo docker-compose --version
docker-compose version 1.26.2, build eefe0d31Now you have Docker Compose downloaded and running properly.
Example Voting App
For demostration Lets use Docker Sample Voting app which is available in github and you can simply download it from here or simple run : wget https://codeload.github.com/dockersamples/example-voting-app/zip/master
This app is consist of several componenets and uses different technologies:

This simple application will be used to showcase how easy it is to set up an entire application stack consist of different components in docker. But first lets keep aside docker compose and see how we would put this together this application stack using docker run command .
Before starting make sure that all images of applications are already built are available on Docker local repository, build them using docker build command under related directory:
Out of the 5 different components 2 of them redis and postgres images are already available . and now Docker run commads:
docker run -d --name redis redis
docker run -d --name=vote -p 5000:80 voting-app
docker run -d --name=db -e POSTGRES_PASSWORD=postgres postgres:9.4
docker run -d --name=result -p 5001:80 result-app
docker run -d --name=worker worker
It seems good but it doesn't work! The problem is that we have successfully run all the different containers but we haven't actually linked them together.
We haven't told the voting-app to use this particular redis instance, also we haven't told the worker and the result-app to use this particular PostgresSQL database that we ran. That is where we use links. Link is a command line option which is used to link two containers togehter.
In our example to make vote app aware of redis service we add a link option while running the voting app container to link it ti the redis container:
Under the hood it creates an entry into the /etc/hosts file on the voting app container. It adds an entry with the hostname redis with an internal IP of the redis container.
and for the worker application we need to add two links:
The same thing should be done for the result app to communicate with the database:
now you can open you browser and vote and check the results:

caution : Using links this way is depricated and the support may be removed in future!
Once we have docker run command tested and ready it is easy to generate docker compose file from it, but before that lets stop all the previous containers we ran manually.
docker compose file
As we said Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration
Here is a simple Compose file for the voting app sample (docke-compose-simple.yml has been modified):
Docker Compose up
and now let bring it up with docker-compose up command:
and check the results!
it is possible to see what container are running via docker-compose ps command:
before going to the next command lets bring down what we have started by using docker compose:
Docker Compose Down
Docker Compose Build
It is not necessary for Docker Compose to have all required images available in docker registry, as we mentioned 2 of 5 different images are already available on Docker Hub. They are official images from redis and postgres .
If you like to instruct Docker compose to run Docker Build instead of building images manually we can replace the image inline with a build line and specify the location of a directory which contains the application code and a Dockerfile with instructions to build the docker image (original docker-compose-sample.yml):
first lets remove stopped containers and consequently we can remove images:
This time when you run docker compose up command, it will first build the images give a temporary name for it and then use those images to run containers:
and check the results in your browser!
Docker Compose Versions
The Compose file defining services, networks, and volumes for a Docker application.Docker compose file changes over time and many new options is added into it as time passes. So many versions of Docker composes exist.
There could be many reasons why someone would want to use older compose versions. It could either be because they are still using older Docker versions or they already have compose files that are currently in use. Lets take a look at famous votingapp in all 3 formats to capture the differences:
Following is the votingapp in compose v1 format.
This version had a number of limitations for example it we wanted to deploy containers on a different network other than the default bridge network there was no way! Also it was imposible to define starting order of containers
Following command could be used to deploy:
docker-compose up -d
With version 2 the format of file also changed a little bit. We no longer specify our stack information directly as we did in version 1, instead of that, all information are encapsulated in the service section. So we have to create a property called services in the root of the file and then move all the services underneath that.
Another different is networking, in version 1 Docker Compose attaches all the containers it runs to the default bridge network and then used links to enable communication between them. With version 2 Docker Compose automatically creates a dedicated bridge network for each application and then attaches all containers to that new network, all containers are then able to communicate to each other using each others's service name.
So basically there is no need to use links in version 2.
depends option also get introduced in version 2, so we can specify the starting order of containers by using that:
We will still use the same docker-compose up command:
docker-compose up -d
As of today version 3 is the latest version and its structure is similar to version 2.
Version 3 comes with support for Docker swarm and there are some options were removed and added
There are 2 ways to deploy an application in compose v3 format.
The first is the traditional option using docker-compose.
docker-compose up -d
The above option ignores the parameters under deploy section.
2. The second preferred option is to use “docker stack” approach as shown below. With this, the Docker services gets directly deployed in the Swarm mode cluster.
docker stack deploy --compose-file vote
Docker Compose Network
Previously in voting app example we have just deployed all containers on the default bridge netwrok, we can also modify this structure to manage traffic based on the different sources they come from. For example we can separate the user generated traffic from the applications internal traffic.

So we create front end network dedicated for user traffic and a back end network dedicated for applications. next We connect each container to the right network (docker-compose.yml):
again before using docker-compose up command first use docker-compose stop and docker container prune to remove previously deployed images.
that's all!
.
.
---
With the special thanks of Mumshad Mannambeth.
https://www.tutorialspoint.com/docker/docker_compose.htm
https://www.infoworld.com/article/3254689/docker-tutorial-get-started-with-docker-compose.html
https://www.educative.io/blog/docker-compose-tutorial
https://miro.medium.com/max/3116/1*jLReq3Ng6RhqzJo4kBPnpA.png
https://docs.docker.com/compose/compose-file/compose-versioning/
https://sreeninet.wordpress.com/2017/03/28/comparing-docker-compose-versions/
.
Last updated
Was this helpful?