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:
    1.
    A Dockerfile for each container image you want to build.
    2.
    A YAML file, docker-compose.yml, that Docker Compose will use to launch containers from those images and configure their services.
    3.
    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:
    1.
    Install the Docker Engine
    2.
    Run the following command to download Docker Compos
1
sudo curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Copied!
3. Apply permissions to the binary, like so:
1
sudo chmod +x /usr/local/bin/docker-compose
Copied!
4. Test the installation to check it worked properly
1
sudo docker-compose --version
2
docker-compose version 1.26.2, build eefe0d31
Copied!
Now 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
1
[[email protected] ~]# unzip master.zip
2
[[email protected] ~]# cd example-voting-app-master
3
[[email protected] example-voting-app-master]# ls -1
4
architecture.png
5
docker-compose-javaworker.yml
6
docker-compose-k8s.yml
7
docker-compose-simple.yml
8
docker-compose-windows-1809.yml
9
docker-compose-windows.yml
10
docker-compose.yml
11
docker-stack-simple.yml
12
docker-stack-windows-1809.yml
13
docker-stack-windows.yml
14
docker-stack.yml
15
ExampleVotingApp.sln
16
Jenkinsfile
17
k8s-specifications
18
kube-deployment.yml
19
LICENSE
20
MAINTAINERS
21
README.md
22
result
23
vote
24
worker
Copied!
This app is consist of several componenets and uses different technologies:
architecture and data flow of this simple voting application
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:
1
[[email protected] example-voting-app-master]# cd vote/
2
[[email protected] vote]# docker build -t voting-app .
3
.
4
.
5
[[email protected] vote]# cd ..
6
[[email protected] example-voting-app-master]# cd result/
7
[[email protected] result]# docker build -t result-app .
8
.
9
.
10
[[email protected] vote]# cd ..
11
[[email protected] example-voting-app-master]# cd worker/
12
[[email protected] worker]# docker build -t worker .
13
.
14
.
15
[[email protected] example-voting-app-master]# docker image ls
16
REPOSITORY TAG IMAGE ID CREATED SIZE
17
worker latest 97fc58e537dc 8 seconds ago 1.72GB
18
result-app latest ac2bada5b913 13 minutes ago 146MB
19
voting-app latest 55342be3bf02 23 minutes ago 84.2MB
20
node 10-slim 645d30ad6e70 5 days ago 135MB
21
python 2.7-alpine 8579e446340f 3 months ago 71.1MB
22
microsoft/dotnet 2.0.0-sdk fde8197d13f4 2 years ago 1.64GB
Copied!
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.
How containers find each other?
By naming containers when running them we can help applications to communicate with each other . For example this piece of code shows how vote app looks for redis db running on a redis container.
1
[[email protected] vote]# cat app.py | grep redis
2
from redis import Redis
3
def get_redis():
4
if not hasattr(g, 'redis'):
5
g.redis = Redis(host="redis", db=0, socket_timeout=5)
6
return g.redis
7
redis = get_redis()
8
redis.rpush('votes', data)
Copied!
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:
1
docker run -d --name redis redis
2
docker run -d --name=vote -p 5000:80 --link redis:redis voting-app
Copied!
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:
1
docker run -d --name=db -e POSTGRES_PASSWORD=postgres postgres:9.4
2
docker run -d --name=worker --link db:db --link redis:redis worker
Copied!
The same thing should be done for the result app to communicate with the database:
1
docker run -d --name=result -p 5001:80 --link db:db result-app
Copied!
1
[[email protected] ~]# docker ps
2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3
f33904391a77 result-app "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:5001->80/tcp result
4
f96e3486c339 worker "/bin/sh -c 'dotnet …" About an hour ago Up About an hour worker
5
1d2b1a59823a postgres:9.4 "docker-entrypoint.s…" About an hour ago Up About an hour 5432/tcp db
6
57df1da9104a voting-app "gunicorn app:app -b…" About an hour ago Up About an hour 0.0.0.0:5000->80/tcp vote
7
aee31cf9dcf9 redis "docker-entrypoint.s…" About an hour ago Up About an hour 6379/tcp redis
Copied!
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.
1
[[email protected] ~]# docker ps
2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3
f33904391a77 result-app "docker-entrypoint.s…" 2 days ago Up 2 days 0.0.0.0:5001->80/tcp result
4
f96e3486c339 worker "/bin/sh -c 'dotnet …" 2 days ago Up 2 days worker
5
1d2b1a59823a postgres:9.4 "docker-entrypoint.s…" 2 days ago Up 2 days 5432/tcp db
6
57df1da9104a voting-app "gunicorn app:app -b…" 2 days ago Up 2 days 0.0.0.0:5000->80/tcp vote
7
aee31cf9dcf9 redis "docker-entrypoint.s…" 2 days ago Up 2 days 6379/tcp redis
9
[[email protected] ~]# docker stop f33 f96 1d2 57d aee
10
f33
11
f96
12
1d2
13
57d
14
aee
15
[[email protected] ~]# docker ps
16
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Copied!

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):
1
version: "3"
2
3
services:
4
vote:
5
image: voting-app
6
command: python app.py
7
volumes:
8
- ./vote:/app
9
ports:
10
- "5000:80"
11
12
redis:
13
image: redis:alpine
14
ports: ["6379"]
15
16
worker:
17
image: worker
18
19
db:
20
image: postgres:9.4
21
environment:
22
POSTGRES_USER: "postgres"
23
POSTGRES_PASSWORD: "postgres"
24
25
result:
26
image: result-app
27
command: nodemon server.js
28
volumes:
29
- ./result:/app
30
ports:
31
- "5001:80"
32
- "5858:5858"
33
Copied!

Docker Compose up

and now let bring it up with docker-compose up command:
1
[[email protected] example-voting-app-master]# docker-compose -f my-docker-compose-simple.yml up
Copied!
and check the results!
it is possible to see what container are running via docker-compose ps command:
1
[[email protected] example-voting-app-master]# docker-compose ps
2
Name Command State Ports
3
--------------------------------------------------------------------------------------------------------------------------
4
example-voting-app-master_db_1 docker-entrypoint.sh postgres Up 5432/tcp
5
example-voting-app-master_redis_1 docker-entrypoint.sh redis ... Up 0.0.0.0:32769->6379/tcp
6
example-voting-app-master_result_1 docker-entrypoint.sh nodem ... Up 0.0.0.0:5858->5858/tcp, 0.0.0.0:5001->80/tcp
7
example-voting-app-master_vote_1 python app.py Up 0.0.0.0:5000->80/tcp
8
example-voting-app-master_worker_1 /bin/sh -c dotnet src/Work ... Up
Copied!
before going to the next command lets bring down what we have started by using docker compose:

Docker Compose Down

1
[[email protected] example-voting-app-master]# docker-compose down
2
Stopping example-voting-app-master_db_1 ... done
3
Stopping example-voting-app-master_redis_1 ... done
4
Stopping example-voting-app-master_result_1 ... done
5
Stopping example-voting-app-master_vote_1 ... done
6
Stopping example-voting-app-master_worker_1 ... done
7
Removing example-voting-app-master_db_1 ... done
8
Removing example-voting-app-master_redis_1 ... done
9
Removing example-voting-app-master_result_1 ... done
10
Removing example-voting-app-master_vote_1 ... done
11
Removing example-voting-app-master_worker_1 ... done
12
Removing network example-voting-app-master_front-tier
13
WARNING: Network example-voting-app-master_front-tier not found.
14
Removing network example-voting-app-master_back-tier
15
WARNING: Network example-voting-app-master_back-tier not found.
Copied!

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):
1
version: "3"
2
3
services:
4
vote:
5
build: ./vote
6
command: python app.py
7
volumes:
8
- ./vote:/app
9
ports:
10
- "5000:80"
11
12
redis:
13
image: redis:alpine
14
ports: ["6379"]
15
16
worker:
17
build: ./worker
18
19
db:
20
image: postgres:9.4
21
environment:
22
POSTGRES_USER: "postgres"
23
POSTGRES_PASSWORD: "postgres"
24
25
result:
26
build: ./result
27
command: nodemon server.js
28
volumes:
29
- ./result:/app
30
ports:
31
- "5001:80"
32
- "5858:5858"
Copied!
first lets remove stopped containers and consequently we can remove images:
1
[[email protected] example-voting-app-master]# docker ps -a
2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3
5742021ff9d8 result-app "docker-entrypoint.s…" About an hour ago Exited (137) About an hour ago result
4
8cfe817181ba postgres:9.4 "docker-entrypoint.s…" About an hour ago Exited (137) About an hour ago db
5
9749002aedde voting-app "gunicorn app:app -b…" About an hour ago Exited (0) About an hour ago vote
6
fbcbb234ae9a redis "docker-entrypoint.s…" About an hour ago Exited (0) About an hour ago redis
7
8
[[email protected] example-voting-app-master]# docker container prune
9
WARNING! This will remove all stopped containers.
10
Are you sure you want to continue? [y/N] y
11
Deleted Containers:
12
5742021ff9d83f845d9b6cbac362b88aec0da9a2ad0451572ff44ef6527a8599
13
8cfe817181bac2b461d873a2cd0cd8b33d685dfe937dfd77929ad104cafbb38a
14
9749002aedded02709cfa7f041a5e5202d5e2164861a0986bdab86c57032004e
15
fbcbb234ae9acc395e38d195369f035e98feed9b037831f699011853c7132a8a
16
17
Total reclaimed space: 306kB
Copied!
1
[[email protected] example-voting-app-master]# docker rmi example-voting-app-master_worker result-app voting-app
2
Untagged: example-voting-app-master_worker:latest
3
Deleted: sha256:79b676d6b36e03b13200e45049a618a08677227c8c76f683dd3f0258f2d7711f.
4
.
5
.
6
Untagged: result-app:latest
7
Deleted: sha256:3c0ca14016af3e9a3d52798b117ec52f425ba9966d5046440949edc82c38403b
8
Deleted: sha256:18133a50b0c1ae094ddd41c276c340064efce2a2a131a28030d7b5789a525874
9
.
10
.
11
Untagged: voting-app:latest
12
Deleted: sha256:ee6d47fae4b78a8f8da8d586cd3f6387f131dda5c1220e6865eac704545e013f
13
.
14
.
15
[[email protected] example-voting-app-master]#
16
Copied!
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:
1
[[email protected] example-voting-app-master]# docker-compose -f docker-compose-simple.yml up
Copied!
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:
v1
v2
v3
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
1
redis:
2
image: redis
3
db:
4
image: postgres:9.4
5
vote:
6
image: voting-app
7
ports:
8
- 5000:80
9
links:
10
- redis
Copied!
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:
1
version: 2
2
services:
3
4
redis:
5
image: redis
6
db:
7
image: postgres:9.4
8
vote:
9
image: voting-app
10
ports:
11
- 5000:80
12
depends_on:
13
- redis
14
Copied!
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
1
version: 3
2
services:
3
4
redis:
5
image: redis
6
db:
7
image: postgres:9.4
8
vote:
9
image: voting-app
10
ports:
11
- 5000:80
12
13
Copied!
There are 2 ways to deploy an application in compose v3 format.
    1.
    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):
1
version: "3"
2
3
services:
4
vote:
5
build: ./vote
6
command: python app.py
7
volumes:
8
- ./vote:/app
9
ports:
10
- "5000:80"
11
networks:
12
- front-tier
13
- back-tier
14
15
result:
16
build: ./result
17
command: nodemon server.js
18
volumes:
19
- ./result:/app
20
ports:
21
- "5001:80"
22
- "5858:5858"
23
networks:
24
- front-tier
25
- back-tier
26
27
worker:
28
build:
29
context: ./worker
30
depends_on:
31
- "redis"
32
- "db"
33
networks:
34
- back-tier
35
36
redis:
37
image: redis:alpine
38
container_name: redis
39
ports: ["6379"]
40
networks:
41
- back-tier
42
43
db:
44
image: postgres:9.4
45
container_name: db
46
environment:
47
POSTGRES_USER: "postgres"
48
POSTGRES_PASSWORD: "postgres"
49
volumes:
50
- "db-data:/var/lib/postgresql/data"
51
networks:
52
- back-tier
53
54
volumes:
55
db-data:
56
57
networks:
58
front-tier:
59
back-tier:
Copied!
again before using docker-compose up command first use docker-compose stop and docker container prune to remove previously deployed images.
1
[[email protected] example-voting-app-master]# docker-compose up
2
Creating network "example-voting-app-master_front-tier" with the default driver
3
Creating network "example-voting-app-master_back-tier" with the default driver
4
Creating db ... done
5
Creating example-voting-app-master_vote_1 ... done
6
Creating example-voting-app-master_result_1 ... done
7
Creating redis ... done
8
Creating example-voting-app-master_worker_1 ... done
9
.
10
.
11
.
Copied!
that's all!
.
.
---
With the special thanks of Mumshad Mannambeth.
.
Last modified 1yr ago