by Gaël Thomas
A beginner’s guide to Docker — how to create a client/server side with Docker-Compose
You are a developer and you want to discover docker-compose? This article is made for you.
After a short introduction on Docker-Compose, you will be able to create your first client/server-side application with Docker.
This article takes into consideration that you know the basics of Docker. If not, don’t panic! I suggest that you read my first article to discover Docker and learn how to create your first application.
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 very simple — 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.
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.
Your solution offers two websites. The first allows stores to create their online store in just 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!
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.
Note: This tutorial takes into consideration that you have already installed Docker on your computer and that you have the basics. If this is not the case, I invite you to refer to my previous article.
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).
Normally you should have this folder architecture:
.├── client/├── docker-compose.yml└── server/
2 directories, 1 file
2. Create your server
In order 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).
Normally you should have this folder architecture in the following path ‘server/’:
.├── Dockerfile├── index.html└── server.py
0 directories, 3 files
2b. Edit the Python file
You can add the following code to the ‘server.py’ file:
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:
This file will be shared by the server when it is started and this sentence will be displayed.
2d. Edit the Docker file
Here we will create a basic Dockerfile that will be in charge of executing our Python file. We will use the official image created to execute Python.
3. Create your client
In order 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).
Normally you should have this folder architecture in the following path ‘client/’:
.├── client.py└── Dockerfile
0 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.
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.
Note: Docker-Compose being very complete, this article aims to give you a concrete and typical example. That’s why you won’t see all the keywords.
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 normally see “Docker-Compose is magic!” displayed in your terminal.
Note: As indicated in the tutorial, your ‘server’ service uses port 1234 of your computer to distribute its content. If you open the ‘http://localhost:1234/’ page on your computer, you should see ‘Docker-Compose is magic!’.
Code is available
If you want to retrieve the complete code to discover it more simply or to execute it, I have put it at your disposal on my Github.
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’.
$ 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 a command in a running container (example: ‘docker-compose exec server ls’).
$ docker-compose exec [service name] [command]
- Lists images.
$ docker-compose images
It’s already over…
Thanks for reading! I regularly share articles on the Medium platform, you can check out my profile.
I am really happy to share with you this new article. I have tried to pass on your first knowledge of Docker-Compose in the simplest way possible. I hope you enjoyed it!
Do not hesitate to give me some feedback to improve my future articles in the comments below.