docker-compose.yml
. This is the Docker Compose file, which will contain the instructions necessary to start and configure the services.server
. It will contain the files necessary for the operation of the server.client
. Here will be the files of the client application. . βββ client/ βββ docker-compose.yml βββ server/ 2 directories, 1 file
server
folder and create the following files in it:server.py
. It will contain the server code.index.html
file. This file will contain a fragment of text that the client application should display.Dockerfile
file. This is a Docker file that will contain the instructions needed to create the server environment.server/
folder should look like: . βββ Dockerfile βββ index.html βββ server.py 0 directories, 3 files
server.py
file: #!/usr/bin/env python3 # python. # -. # - , Python. import http.server import socketserver # . handler = http.server.SimpleHTTPRequestHandler # , 1234. # , , docker-compose. with socketserver.TCPServer(("", 1234), handler) as httpd: # , . httpd.serve_forever()
index.html
file, the contents of which will later be displayed on a web page.index.html
file: Docker-Compose is magic!
Dockerfile
file that will be responsible for organizing the runtime environment for the Python server. As a basis of the created image we will use the official image intended for the execution of programs written in Python. Here are the contents of the Dockerfile: # , Dockerfile . # 'FROM'. # python ( DockerHub). # , , 'python', - 'latest'. FROM python:latest # , Python, 'server.py' 'index.html'. # , 'ADD'. # , 'server.py', , . # , '/server/', , . # '/server/'. ADD server.py /server/ ADD index.html /server/ # 'WORKDIR', , . # . # , , '/server/'. WORKDIR /server/
client
project and create the following files in it:client.py
. There will be a client code.Dockerfile
file. 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.py βββ Dockerfile 0 directories, 2 files
client.py
file: #!/usr/bin/env python3 # Python. # 'index.html' . # , Python. import urllib.request # 'http://localhost:1234/'. # , , 'http://localhost:1234'. # localhost , . # 1234 - , . fp = urllib.request.urlopen("http://localhost:1234/") # 'encodedContent' ('index.html'). # 'decodedContent' ( , ). encodedContent = fp.read() decodedContent = encodedContent.decode("utf8") # , ('index.html'). print(decodedContent) # . fp.close()
Dockerfile
for the client, responsible for creating the environment in which the client Python application will run. Here is the code of the client Dockerfile
: # , Dockerfile. FROM python:latest # 'client.py' '/client/'. ADD client.py /client/ # '/client/'. WORKDIR /client/
Dockerfile
file. 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 docker-compose.yml
file 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.docker-compose.yml
: # docker-compose . # "3" - . version: "3" # , docker-composes . # 1 = 1 . # , , ... # , , 'services'. services: # , . # , . # (): . # , . # . # , , 'server'. server: # "build" # Dockerfile, , # . # 'server/' , # Dockerfile. build: server/ # , . # "python ./server.py". command: python ./server.py # , 'server/server.py' 1234. # ( ), # . # 'ports'. # : [ ]:[ ] # 1234 # 1234 ( # ). ports: - 1234:1234 # (): . # 'client'. client: # 'client/ , # Dockerfile . build: client/ # , . # "python ./client.py". command: python ./client.py # 'network_mode' . # , 'localhost' . network_mode: host # 'depends_on' , , # , , . # , 'client' 'server'. depends_on: - server
docker-compose.yml
been entered into docker-compose.yml
, the project needs to be assembled. This step of our work resembles the use of the docker build
, but the corresponding command relates to several services: $ docker-compose build
docker run
command is executed: $ docker-compose up
Docker-Compose is magic!
.1234
to service client requests. Therefore, if you go to the browser at http: // localhost: 1234 / , it will display a page with the text Docker-Compose is magic!
.docker-compose up
: $ 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: https://habr.com/ru/post/450312/
All Articles