Akshat (akshu20791@gmail.
com)
What is Docker-Compose?
Docker-Compose is a tool provided by Docker. To make it simple, this
tool is implemented to solve architectural problems in your projects.
As you may have noticed in my previous article, we created a simple
program that displayed “Docker is magic!” when it was launched.
Unfortunately, when you are a developer, you rarely create a stand-
alone program (a program that does not require any other services to
run, such as a database).
However, how do you know if you need Docker-Compose? It’s easy if
your application requires several services to run, you need this tool.
For example, if you create a website that needs to connect to your
database to authenticate users (here 2 services, website, and database).
Docker-compose offers you the possibility to launch all these services
in a single command.
Difference between Docker and Docker-Compose
Docker is used to manage an individual container (service) for your
application.
pg. 1
Akshat (akshu20791@gmail.com)
Docker-Compose is used to manage several containers at the same
time for the same application. This tool offers the same features as
Docker but allows you to have more complex applications.
A typical use case
This tool can become very powerful and allow you to deploy
applications with complex architectures very quickly. I will give you a
concrete case study that will prove that you need it.
Imagine, you are the proud creator of your web software.
pg. 2
Akshat (akshu20791@gmail.com)
Your solution offers two websites. The first allows stores to create their
online store in a few clicks. The second is dedicated to customer
support. These two sites interact with the same database.
You are beginning to be successful, and your server is no longer
sufficient. So, you decide to migrate your entire software to another
machine.
Unfortunately, you didn’t use docker-compose. So you’re going to have
to migrate and reconfigure your services one after the other, hoping
nothing has been forgotten.
If you had used a docker-compose, in only a few commands, you would
have deployed your entire architecture on your new server. All you
have to do now is make a few configurations and load the backup of
your database to finalize the migration.
Now let’s create your first client/server-side application with
Docker-Compose
Now that you know what docker-compose is going to be used for, it’s
time to create your first client/server-side application!
pg. 3
Akshat (akshu20791@gmail.com)
The objective of this tutorial is to create a small website (server) in
Python that will contain a sentence. This sentence must be retrieved by
a program (client) in Python that will display the sentence.
1. Create your project
To create your first client/server application, I invite you to create a
folder on your computer. It must contain at root the following file and
folders:
• A ‘docker-compose.yml’ file (docker-compose file that will
contain the necessary instructions to create the different
services).
• A ‘server’ folder (this folder will contain the files necessary to
set up the server).
• A ‘client’ folder (this folder will contain the files necessary to
set up the client).
You should have this folder architecture:
.
├── client/
├── docker-compose.yml
└── server/2 directories, 1 file
2. Create your server
pg. 4
Akshat (akshu20791@gmail.com)
To start with reminders of Docker’s basics, we will start by creating the
server.
2a. Create files
Move to your ‘server’ folder and create the following files:
• A ‘server.py’ file (python file that will contain the server
code).
• An ‘index.html’ file (HTML file that will contain the sentence
to be displayed).
• A ‘Dockerfile’ file (docker file that will contain the necessary
instructions to create the environment of the server).
You should have this folder architecture in the following path ‘server/’:
.
├── Dockerfile
├── index.html
└── server.py0 directories, 3 files
You can add the following code to the ‘server.py’ file:
pg. 5
Akshat (akshu20791@gmail.com)
This code will allow you to create a simple web server inside this folder.
It will retrieve the content of the index.html file to share it on a web
page.
2c. Edit the Html file
You can add the following sentence to the ‘index.html’ file:
2d. Edit the Docker file
Here we will create a basic Dockerfile that will be in charge of
executing our Python file. To do that, we are going to use the official
image created to execute Python.
pg. 6
Akshat (akshu20791@gmail.com)
3. Create your client
To continue with reminders of Docker’s basics, we will create the
client.
3a. Create files
Move to your ‘client’ folder and create the following files:
• A ‘client.py’ file (python file that will contain the client code).
• A ‘Dockerfile’ file (docker file that will contain the necessary
instructions to create the environment of the client).
pg. 7
Akshat (akshu20791@gmail.com)
Normally you should have this folder architecture in the following path
‘client/’:
.
├── client.py
└── Dockerfile0 directories, 2 files
3b. Edit the Python file
You can add the following code to the ‘client.py’ file:
This code will allow you to get the content of the server web page and
to display it.
pg. 8
Akshat (akshu20791@gmail.com)
3c. Edit the Docker file
As for the server, we will create a basic Dockerfile that will be in charge
of executing our Python file.
4. Back to Docker-Compose
As you may have noticed, we have created two different projects, the
server, and the client, both with a Dockerfile.
So far, nothing has changed from the basics you already know.
Now we are going to edit the ‘docker-compose.yml’ at the root of the
repository.
pg. 9
Akshat (akshu20791@gmail.com)
5. Build Docker-Compose
Once the docker-compose is set up, your client/server application need
to be built. This step corresponds to the ‘docker build’ command but
applied to the different services.
$ docker-compose build
6. Run Docker-Compose
Your docker-compose is built! Now it’s time to start! This step
corresponds to the ‘docker run’ command but applied to the different
services.
$ docker-compose up
There you go, that’s it. You should see “Docker-Compose is magic!”
displayed in your terminal.
Useful commands for Docker
As usual, I have prepared a list of orders that may be useful to you with
docker-compose.
• Stops containers and removes containers, images… created by
‘docker-compose up’.
pg. 10
Akshat (akshu20791@gmail.com)
$ docker-compose down
• Displays log output from services (example: ‘docker-compose
logs -f client’).
$ docker-compose logs -f [service name]
• Lists containers.
$ docker-compose ps
• Executes command in a running container (example: ‘docker-
compose exec server ls’).
$ docker-compose exec [service name] [command]
• Lists images.
$ docker-compose images
pg. 11