A beginner’s guide to Docker — how to create a client/server side with 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-ComposeDocker 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.

Docker (individual container) VS Docker-Compose (several containers)A typical use caseThis 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-ComposeNow 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 projectTo 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 file2.

Create your serverIn order to start with reminders of Docker’s basics, we will start by creating the server.

2a.

Create filesMove 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.

py0 directories, 3 files2b.

Edit the Python fileYou 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 fileYou 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 fileHere 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 clientIn order to continue with reminders of Docker’s basics, we will create the client.

3a.

Create filesMove 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└── Dockerfile0 directories, 2 files3b.

Edit the Python fileYou 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 fileAs for the server, we will create a basic Dockerfile that will be in charge of executing our Python file.

4.

Back to Docker-ComposeAs 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-ComposeOnce 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 build6.

Run Docker-ComposeYour 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 runThere 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 availableIf 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.

gael-thomas/Client-Server-Docker-Compose-exampleDocker-Compose Client/Server example.

Created for "A beginner's guide to Docker – how to create a client/server…github.

comUseful commands for DockerAs 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 downDisplays log output from services (example: ‘docker-compose logs -f client’).

$ docker-compose logs -f [service name]Lists containers.

$ docker-compose psExecutes a command in a running container (example: ‘docker-compose exec server ls’).

$ docker-compose exec [service name] [command]Lists images.

$ docker-compose imagesIt’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.

Last beginner’s guide to DockerA beginner’s guide to Docker — how to create your first Docker applicationYou are a developer and you want to start with Docker?.This article is made for you.

medium.

freecodecamp.

org.

. More details

Leave a Reply