docker-compose.ymlfile. This is the Docker Compose file, which will contain the instructions necessary to start and configure the services.
serverfolder. It will contain the files needed to make the server work.
clientfolder. This will contain the client application files.
. Client── client/ Do── docker-compose.yml Server── server/ 2 directories, 1 file
serverfolder and create the following files in it:
server.pyfile. It will contain the server code.
index.htmlfile. This file will contain a piece of text that the client application should display.
Dockerfilefile. This is a Docker file that will contain the instructions needed to create the server environment.
server/folder should look like:
. Do── Dockerfile Index── index.html Server── server.py 0 directories, 3 files
#!/usr/bin/env python3 # Import python system libraries. # These libraries will be used to create a web server. # You do not need to install something special, these libraries are installed along with Python. import http.server import socketserver # This variable is needed to process client requests to the server. handler = http.server.SimpleHTTPRequestHandler # Here we indicate that we want to start the server on port 1234. # Try to remember this information, as they will be very useful in the future, when working with docker-compose. with socketserver.TCPServer (("", 1234), handler) as httpd: # With this command, the server will run continuously, waiting for requests from the client. httpd.serve_forever ()
index.htmlfile, the contents of which will later be displayed on a web page.
index.htmladd the following text:
Docker-Compose is magic!
Dockerfilefile that will be responsible for organizing the runtime environment for the Python server. As a basis for the image being created, we will use in an official way , designed to run programs written in Python. Here are the contents of the Dockerfile:
# Just in case, I remind you that the Dockerfile must always begin with the import of the base image. # The keyword 'FROM' is used for this. # Here we need to import a python image (with DockerHub). # As a result, we specify 'python' as the name of the image, and 'latest' as the version. FROM python: latest # In order to run code written in Python in a container, we need to import the 'server.py' and 'index.html' files. # In order to do this, we use the 'ADD' keyword. # The first parameter, 'server.py', is the name of the file stored on the computer. # The second parameter, '/server/', is the path where the specified file should be placed in the image. # Here we put the file in the '/server/' image folder. ADD server.py/server/ ADD index.html/server/ # Here we will use the 'WORKDIR' command, possibly new for you. # It allows you to change the working directory of the image. # As such a directory in which all commands will be executed, we set '/server/'. WORKDIR/server/
clientproject folder and create the following files in it:
client.pyfile. There will be a client code.
Dockerfilefile. This file plays the same role as the similar file in the server folder. Namely, it contains a statement describing the creation of an environment for executing client code.
client/folder at this stage should look like this:
. Client── client.py Do── Dockerfile 0 directories, 2 files
#!/usr/bin/env python3 # Import the Python system library. # It is used to download the 'index.html' file from the server. # Nothing special needs to be installed, this library is installed along with Python. import urllib.request # This variable contains a request for 'http://localhost: 1234/'. # Perhaps now you are wondering what is 'http://localhost: 1234'. # localhost indicates that the program works with a local server. # 1234 is the port number that you were asked to remember when setting up the server code. fp = urllib.request.urlopen ("http://localhost: 1234/") # 'encodedContent' corresponds to the encoded server response ('index.html'). # 'decodedContent' corresponds to the decoded response of the server (there will be what we want to display on the screen). encodedContent = fp.read () decodedContent = encodedContent.decode ("utf8") # Display the contents of the file received from the server ('index.html'). print (decodedContent) # Close the connection to the server. fp.close ()
Dockerfilefor the client, responsible for creating the environment in which the client Python application will run.Here's the code for the client’s
# Same as in server Dockerfile. FROM python: latest # Import 'client.py' into the '/client/' folder. ADD client.py/client/ # Set as the working directory '/client/'. WORKDIR/client/
Dockerfilefile. Until now, everything that happens does not go beyond the basics of working with Docker. Now we begin to work with Docker Compose. To do this, refer to the file
docker-compose.yml, located in the root folder of the project.
docker-compose.yml. Our main goal is to make out a practical example that gives you a basic knowledge of Docker Compose.
# The docker-compose file must start with a version tag. # We use "3" as this is the most recent version at the time of this writing. version: "3" # Please note that docker-composes works with services. # 1 service = 1 container. # A service can be a client, server, database server ... # The section in which services will be described begins with 'services'. services: # As already mentioned, we are going to create client and server applications. # This means that we need two services. # First service (container): server. # You can call it the way a developer needs. # The clear name of the service helps to determine its role. # Here we use the 'server' keyword to name the corresponding service. server: # The "build" keyword allows you to set # path to the Dockerfile file to be used to create the image, # which will allow to start the service. # Here, 'server/' corresponds to the path to the server folder, # which contains the corresponding Dockerfile. build: server/ # The command to be run after creating the image. # The following command means running "python ./server.py". command: python ./server.py # Recall that port 1234 is specified as a port in 'server/server.py'. # If we want to access the server from our computer (being outside the container), # we need to organize the redirection of this port to the port of the computer. # The 'ports' keyword will help us. # When using it, the following construction is used: [computer port]: [container port] # In our case, you need to use computer port 1234 and organize its connection with the port # 1234 containers (as it is on this port server # waiting for requests). ports: - 1234: 1234 # Second service (container): client. # This service is named 'client'. client: # Here 'client/corresponds to the path to the folder that contains # Dockerfile file for the client part of the system. build: client/ # The command to be run after creating the image. # The following command means running "python ./client.py". command: python ./client.py # The keyword 'network_mode' is used to describe the type of network. # Here we indicate that the container can access the computer 'localhost'. network_mode: host # The 'depends_on' keyword allows you to specify whether a service should # Before you start, wait for other services to be ready for work. # We need the 'client' service to wait for the availability of the 'server' service. depends_on: - server
docker-compose.yml, the project needs to be assembled.This step of our work resembles the use of the
docker buildcommand, but the corresponding command relates to several services:
$ docker-compose build
docker runcommand is executed:
$ docker-compose up
Docker-Compose is magic!.
1234to service client requests. Therefore, if you go to your browser at http://localhost: 1234/, it will display a page with the text
Docker-Compose is magic!.
$ docker-compose down
$ docker-compose logs -f [service name]
$ docker-compose logs -f [service name].
$ docker-compose ps
$ docker-compose exec [service name] [command]
docker-compose exec server ls.
$ docker-compose images
Source text: [Translation] Docker Compose Beginner's Guide