Docker compose is a tool for determine and running more than one container in Docker applications. If you use compose it means that you using a YAML file to configure the application services. Then, just with a single command all the service can be created and started.
Compose can work in all environment such as production, staging, development, testing and also CI workflows. In the compose there are three basic steps you have to do:
- Define your app’s environment with a Dockerfile so it can be reproduced anywhere.
- Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.
- Run docker-compose up and Compose starts and runs your entire app.
This is what docker-compose.yml looks like :
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
- logvolume01:/var/log
links:
- redis
redis:
image: redis
volumes:
logvolume01: {}
Installing Compose in CentOS 7
First you have to run this command below to download the latest of docker compose.
# sudo curl –L "https://github.com/docker/compose/releases/download/1.25.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Next you have to apply executable permissions to the binary.
# sudo chmod +x /usr/local/bin/docker-compose
Make an application using docker compose
For to do this you should already installed both Docker Engine and Docker Compose. First thing that we have to do is define the application dependencies. Let’s create a directory for the project by using this command below.
$ mkdir composetest
$ cd composetest
Then create a file called app.py in your project directory and paste this in.
import time
import redis
from flask import Flask
app = Flask(__name__)
cache = redis.Redis(host='redis', port=6379)
def get_hit_count():
retries = 5
while True:
try:
return cache.incr('hits')
except redis.exceptions.ConnectionError as exc:
if retries == 0:
raise exc
retries -= 1
time.sleep(0.5)
@app.route('/')
def hello():
count = get_hit_count()
return 'Hello World! I have been seen {} times.\n'.format(count)
In this example, the hostname is redis and the default port for Redis is 6379.
Then create requirement.txt file and paste this in that file.
flask
redis
After that we have to create a docker file with the name is Dockerfile and paste the following command below to that file.
FROM python:3.7-alpine
WORKDIR /code
ENV FLASK_APP app.py
ENV FLASK_RUN_HOST 0.0.0.0
RUN apk add --no-cache gcc musl-dev linux-headers
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . .
CMD ["flask", "run"]
This tells Docker to:
- Build an image starting with the Python 3.7 image.
- Set the working directory to /code.
- Set environment variables used by the flask command.
- Install gcc so Python packages such as MarkupSafe and SQLAlchemy can compile speedups.
- Copy requirements.txt and install the Python dependencies.
- Copy the current directory in the project to the workdir in the image.
- Set the default command for the container to flask run.
The next step is create a docker-compose.yml file in your project directory and write the command below to this file.
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"
After all the step above are done, next is to build and run the app with compose. From your project directory, start up your application by running this command.
# docker-compose up
Then, open your browser and go to http://localhost:5000/ or http://127.0.0.1:5000/ to see if the application is running. The webpage should showing something like the picture.

Next, refresh the page and check if the number is change. If its correct, the number should increment like in the picture.

After that open another terminal and see if the redis and web images are in list. You can check the images by typing this command.
$ docker image ls
Next lets add some bind mount for the web service by editing the
docker-compose.yml file.
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
environment:
FLASK_ENV: development
redis:
image: "redis:alpine"
Then we have to re-build and run the app with this command.
return 'Hello from Docker! I have been seen {} times.\n'.format(count)
And finally refresh the app in your browser. It should be showing something like in the picture. The number should be incrementing.
