0% found this document useful (0 votes)
36 views9 pages

Docker Compose

Docker Compose is a tool for defining and running multi-container Docker applications using a YAML file for configuration. It allows users to manage the lifecycle of applications, including starting, stopping, and rebuilding services, and works across various environments. The document provides a step-by-step guide on setting up a Flask application with Redis using Docker Compose, including creating necessary files and commands to run the application.

Uploaded by

sathwik.iiimail
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views9 pages

Docker Compose

Docker Compose is a tool for defining and running multi-container Docker applications using a YAML file for configuration. It allows users to manage the lifecycle of applications, including starting, stopping, and rebuilding services, and works across various environments. The document provides a step-by-step guide on setting up a Flask application with Redis using Docker Compose, including creating necessary files and commands to run the application.

Uploaded by

sathwik.iiimail
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Docker Compose

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. To learn more about all the features of
Compose, see the list of features.

P
LL
S
AB

Compose works in all environments: production, staging, development, testing, as well as CI


workflows.
PL

Using Compose is basically a three-step process:


1. Define your app’s environment with a Dockerfile so it can be reproduced
anywhere.
2. Define the services that make up your app in docker-compose.yml so they can be
run together in an isolated environment.
LW

3. Run docker-compose up and Compose starts and runs your entire app.

A docker-compose.yml looks like this:


version: '2.0'
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code ## Bind Volumes
- logvolume01:/var/log ## Named Volumes (we will generally use this concept when we have
Filsystem like AWS EFS/EBS - Elastic File System in place)
network: flipkart
links:
- redis
redis:
image: redis
network: flipkart

volumes:
logvolume01: {}

network:
- flipkart
For more information about the Compose file, see the Compose file reference.
Compose has commands for managing the whole lifecycle of your application:

P
● Start, stop, and rebuild services
● View the status of running services
● Stream the log output of running services

LL
Prereq-
Docker Engine Should be installed in the server.

Install Docker Compose


S
1. Run this command to download the current stable release of Docker Compose:
AB
sudo curl -L
"https://github.com/docker/compose/releases/download/1.28.2/docker-compose-
$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

To install a different version of Compose, substitute 1.25.5 with the version of Compose
you want to use.
PL

2. Apply executable permissions to the binary:

sudo chmod +x /usr/local/bin/docker-compose


LW

Note: If the command docker-compose fails after installation, check your path. You can also
create a symbolic link to /usr/bin or any other directory in your path.

For example:

sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

Exercise
Step 1:
Define the application dependencies.
1. Create a directory for the project:
2. $ mkdir composetest
3. $ cd composetest

4. 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)

P
def get_hit_count():
retries = 5

LL
while True:
try:
return cache.incr('hits')
except redis.exceptions.ConnectionError as exc:
if retries == 0:
raise exc
retries -= 1
time.sleep(0.5)
S
AB
@app.route('/')
def hello():
count = get_hit_count()
return 'Hello World! I have been seen {} times.\n'.format(count)
PL

In this example, redis is the hostname of the redis container on the application’s
network. We use the default port for Redis, 6379.

Note the way the get_hit_count function is written. This basic retry loop lets us attempt
our request multiple times if the redis service is not available. This is useful at startup
LW

while the application comes online, but also makes our application more resilient if the
Redis service needs to be restarted anytime during the app’s lifetime. In a cluster, this
also helps handling momentary connection drops between nodes.

5. Create another file called requirements.txt in your project directory and paste this in:
flask
redis

Step 2: Create a Dockerfile


In this step, you write a Dockerfile that builds a Docker image. The image contains all the
dependencies the Python application requires, including Python itself.
In your project directory, create a file named Dockerfile and paste the following:
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:

P
● Build an image starting with the Python 3.7 image.
● Set the working directory to /code.

LL
● 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.
S
For more information on how to write Dockerfiles, see the Docker user guide and the Dockerfile
reference.
AB
Step 3: Define services in a Compose file
Create a file called docker-compose.yml in your project directory and paste the following:
version: '3'
services:
PL

web:
build: .
ports:
- "5000:5000"
LW

redis:
image: "redis:alpine"

This Compose file defines two services: web and redis.

‘web’ service
The web service uses an image that’s built from the Dockerfile in the current directory. It then
binds the container and the host machine to the exposed port, 5000. This example service uses
the default port for the Flask web server, 5000.

‘redis’ service
The redis service uses a public Redis image pulled from the Docker Hub registry.
Step 4: Build and run your app with Compose
1. From your project directory, start up your application by running docker-compose up.
$ docker-compose up

Creating network "composetest_default" with the default driver


Creating composetest_web_1 ...
Creating composetest_redis_1 ...
Creating composetest_web_1
Creating composetest_redis_1 ... done
Attaching to composetest_web_1, composetest_redis_1

P
web_1 | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
redis_1 | 1:C 17 Aug 22:11:10.480 # oO0OoO0OoO0Oo Redis is starting

LL
oO0OoO0OoO0Oo
redis_1 | 1:C 17 Aug 22:11:10.480 # Redis version=4.0.1, bits=64,
commit=00000000, modified=0, pid=1, just started
redis_1 | 1:C 17 Aug 22:11:10.480 # Warning: no config file specified, using
the default config. In order to specify a config file use redis-server
/path/to/redis.conf
web_1 | * Restarting with stat
S
redis_1 | 1:M 17 Aug 22:11:10.483 * Running mode=standalone, port=6379.
AB
redis_1 | 1:M 17 Aug 22:11:10.483 # WARNING: The TCP backlog setting of 511
cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower
value of 128.
web_1 | * Debugger is active!
PL

redis_1 | 1:M 17 Aug 22:11:10.483 # Server initialized


redis_1 | 1:M 17 Aug 22:11:10.483 # WARNING you have Transparent Huge Pages
(THP) support enabled in your kernel. This will create latency and memory
usage issues with Redis. To fix this issue run the command 'echo never >
/sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your
/etc/rc.local in order to retain the setting after a reboot. Redis must be
LW

restarted after THP is disabled.


web_1 | * Debugger PIN: 330-787-903
redis_1 | 1:M 17 Aug 22:11:10.483 * Ready to accept connections

Compose pulls a Redis image, builds an image for your code, and starts the services you
defined. In this case, the code is statically copied into the image at build time.
Enter http://localhost:5000/ in a browser to see the application running.
You should see a message in your browser saying:

Hello World! I have been seen 1 times.


P
LL
2. Refresh the page.
The number should increment. S
Hello World! I have been seen 2 times.
AB
docker-compose logs --tail 10 -f
PL
LW

3. Switch to another terminal window, and type docker image ls to list local images.
Listing images at this point should return redis and web.
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED
SIZE
composetest_web latest e2c21aa48cc1 4 minutes
ago 93.8MB
python 3.4-alpine 84e6077c7ab6 7 days ago
82.5MB
redis alpine 9d8fa9aa0e5b 3 weeks ago
27.5MB

Step 5: Edit the Compose file to add a bind mount

P
Edit docker-compose.yml in your project directory to add a bind mount for the web service:
version: '3'

LL
services:
web:
build: .
ports:
- "5000:5000"
S
volumes:
AB
- .:/code
environment:
FLASK_ENV: development
redis:
PL

image: "redis:alpine"

Step 6: Re-build and run the app with Compose


LW

From your project directory, type docker-compose up to build the app with the updated Compose
file, and run it.
$ docker-compose up
Creating network "composetest_default" with the default driver
Creating composetest_web_1 ...
Creating composetest_redis_1 ...
Creating composetest_web_1
Creating composetest_redis_1 ... done
Attaching to composetest_web_1, composetest_redis_1
web_1 | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
...

docker compose command to build image again after you have copied the changes from
github

docker-compose up -d --build

Step 7: Update the application

P
Because the application code is now mounted into the container using a volume, you can make
changes to its code and see the changes instantly, without having to rebuild the image.
1. Change the greeting in app.py and save it. For example, change the Hello

LL
World! message to Hello from Docker!:

2. return 'Hello from Docker! I have been seen {} times.\n'.format(count)

3. Refresh the app in your browser. The greeting should be updated, and the counter
should still be incrementing.
S
AB
PL
LW

Step 8: Experiment with some other


commands
If you want to run your services in the background, you can pass the -d flag (for “detached”
mode) to docker-compose up and use docker-compose ps to see what is currently running:
$ docker-compose up -d
Starting composetest_redis_1...
Starting composetest_web_1...

$ docker-compose ps
Name Command State Ports
-------------------------------------------------------------------
composetest_redis_1 /usr/local/bin/run Up
composetest_web_1 /bin/sh -c python app.py Up 5000->5000/tcp

P
docker-compose ps

detach mode

LL
docker-compose up -d

docker-compose stop/start
docker-compose down
S
AB
PL
LW

You might also like