Hello everyone, in the last article I talked about the topic of connection and link between containers in docker ( link ). As promised, I will write the next topic related to a support tool that anyone who knows Docker may have used, that is Docker Compose.
Of course the article is not entirely written by me, I have read “Docker in Pratice” and translated from 1 chapter in this book combined with the knowledge that I know. The part I mentioned will be Part3 – Chapter 8 – 8.1 – Container communication — beyond manual linking (roughly translated as communication between containers, overcoming the pain of manual linking ).
1. Open the article
In the previous article, we saw how to connect containers to linking containers or use mapping ports with the external environment. However, the link also has some drawbacks. That is, we have to use our hands to determine when to start each container individually (because the container has to start in order for us to link) and furthermore there’s no way to remove the connection. that (linking) between the containers (if 1 container dies, all containers that depend on it must restart to re-create linking .
So we need a tool to solve this problem, yes it’s Docker Compose .
2. Introducing Docker Compose.
Oke, first you have to know what it is. Docker Compose became known as fig , which was created to minimize and eliminate the startup of multiple containers with parameters such as linking, volumes or ports. Docker Inc. liked the idea and they took it, recreated it and released it with a new name.
Docker Compose is a tool for defining and launching complex Docker applications. The main idea of this tool is that instead of associating startup commands with complex containers with tools like Shell scripts or Makefiles , you can define the initial settings when starting the application (or containers). ) and encapsulate them in a single command. At the time of writing, Docker Compose is not recommended for use on production environments. (Should not be used on actual products)
For how to install Docker Compose, refer to the homepage https://docs.docker.com/compose/install/ . There are quite detailed instructions, remember to read them carefully. Also about running docker without sudo, all are also on the homepage, I don’t need to worry offline https://docs.docker.com/install/linux/linux-postinstall/ .
3. Example of usage.
We will create an echo server and a client (this example I took exactly the same in the book out, because they write is also difficult to understand). The client sends 1 message every 5s to the echo server and receives the return message.
First we create the server directory and point to it first:
1 2 3 |
$ <span class="token function">mkdir</span> server $ <span class="token function">cd</span> server |
Next, create a server-side Dockerfile with the following content:
1 2 3 4 |
<span class="token keyword">FROM</span> debian <span class="token keyword">RUN</span> apt <span class="token punctuation">-</span> get update && apt <span class="token punctuation">-</span> get install <span class="token punctuation">-</span> y nmap <span class="token comment">#Install the nmap package, which provides the ncat program used here.</span> <span class="token keyword">CMD</span> ncat <span class="token punctuation">-</span> l 2000 <span class="token punctuation">-</span> k <span class="token punctuation">-</span> <span class="token punctuation">-</span> exec /bin/cat <span class="token comment"># Run the ncat program by default when starting the image.</span> |
Option -l 2000
tells ncat to listen for port 2000 and options -k
tells it to allow multiple connections from the client to occur simultaneously and to continue running after the client closes the connection. The last option --exec /bin/cat
will make ncat run the / bin / cat command for any incoming connections and forward any data from the external connections outside the program.
Next step, we will build Dockerfile with command
1 2 |
$ docker build -t server <span class="token keyword">.</span> |
Now we can install image clinets to send messages to the server. Create the client directory at the same level as the server and then create the client file . py :
1 2 3 4 5 6 7 8 9 10 11 12 |
<span class="token keyword">import</span> socket <span class="token punctuation">,</span> time <span class="token punctuation">,</span> sys <span class="token comment">#Import the Python packages needed.</span> <span class="token keyword">while</span> <span class="token boolean">True</span> <span class="token punctuation">:</span> s <span class="token operator">=</span> socket <span class="token punctuation">.</span> socket <span class="token punctuation">(</span> socket <span class="token punctuation">.</span> AF_INET <span class="token punctuation">,</span> socket <span class="token punctuation">.</span> SOCK_STREAM <span class="token punctuation">)</span> <span class="token comment"># Create a socket object.</span> s <span class="token punctuation">.</span> connect <span class="token punctuation">(</span> <span class="token punctuation">(</span> <span class="token string">'talkto'</span> <span class="token punctuation">,</span> <span class="token number">2000</span> <span class="token punctuation">)</span> <span class="token punctuation">)</span> <span class="token comment"># Use the socket to connect to the ‘talkto’ server on port 2000.</span> s <span class="token punctuation">.</span> send <span class="token punctuation">(</span> <span class="token string">'Hello, worldn'</span> <span class="token punctuation">)</span> <span class="token comment"># Send a string with a newline character to the socket.</span> data <span class="token operator">=</span> s <span class="token punctuation">.</span> recv <span class="token punctuation">(</span> <span class="token number">1024</span> <span class="token punctuation">)</span> <span class="token comment"># Create a buffer of 1024 bytes to receive data, </span> <span class="token comment">#and place the data into the data variable when a message is received.</span> <span class="token keyword">print</span> <span class="token string">'Received:'</span> <span class="token punctuation">,</span> data <span class="token comment"># Print the received data to standard out.</span> sys <span class="token punctuation">.</span> stdout <span class="token punctuation">.</span> flush <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token comment"># Flush the standard out buffer so you can see messages as they come in.</span> s <span class="token punctuation">.</span> close <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token comment"># Close the socket object.</span> time <span class="token punctuation">.</span> sleep <span class="token punctuation">(</span> <span class="token number">5</span> <span class="token punctuation">)</span> <span class="token comment"># Wait five seconds and repeat.</span> |
and Dockerfile
1 2 3 4 5 |
<span class="token keyword">FROM</span> debian <span class="token keyword">RUN</span> apt <span class="token punctuation">-</span> get update && apt <span class="token punctuation">-</span> get install <span class="token punctuation">-</span> y python <span class="token keyword">ADD</span> client.py /client.py <span class="token keyword">CMD</span> <span class="token punctuation">[</span> <span class="token string">"/usr/bin/python"</span> <span class="token punctuation">,</span> <span class="token string">"/client.py"</span> <span class="token punctuation">]</span> |
Then the same we build the client:
1 2 |
$ docker build -t client <span class="token keyword">.</span> |
And to determine its ability to run, we will run 2 statements:
1 2 3 |
docker run --name echo-server -d server docker run --rm --name client --link echo-server:talkto client |
When you’re done, log out of the client and delete the container:
1 2 |
docker <span class="token function">rm</span> -f client echo-server |
Many things can go wrong even in the above example: starting the client first will make the application unable to initialize, forgetting to delete containers can also be a problem if you want to restart, or naming the wrong container can lead to errors . These issues will increase as the application and project architecture become more complex.
Compose helps us by packaging the link between the containers when they are initialized and caft placed in the same text file, managing the input and output of initializing and shutting down the containers.
Compose uses the YAML file to know. Assuming we have the following docker-compose.yml file at the same level as the server and client directories:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<span class="token key atrule">echo-server</span> <span class="token punctuation">:</span> <span class="token comment"># The reference names of the running services are their identifiers: echo-server and client, in this case.</span> <span class="token key atrule">image</span> <span class="token punctuation">:</span> server <span class="token comment">#Each section must define the image used: the client and server images, in this case.</span> <span class="token key atrule">expose</span> <span class="token punctuation">:</span> <span class="token comment">#Expose the echo-server’s port 2000 to other services.</span> <span class="token punctuation">-</span> <span class="token string">"2000"</span> <span class="token key atrule">client</span> <span class="token punctuation">:</span> <span class="token key atrule">image</span> <span class="token punctuation">:</span> client <span class="token key atrule">links</span> <span class="token punctuation">:</span> <span class="token comment"># Define a link to the echo-server. </span> <span class="token comment"># References to talkto within the client will be sent to the echo server. </span> <span class="token comment"># The mapping is done by setting up the /etc/hosts file dynamically in the running container.</span> <span class="token punctuation">-</span> echo <span class="token punctuation">-</span> server <span class="token punctuation">:</span> talkto |
It pee 1: Yaml file is a text file to declare information by level. It is similar in structure to object type -> child object -> child object …-> attribute. For more information, please see here: https://yaml.org/
If you are familiar with the .json file then YAML is .yml is similar to that architecture, but we have removed all the “{” or “}” marks.
Docker-compose.yml ‘s Syntax is easy to read, each service is named and tiered based on spaces, each item has a colon after the name and the attributes of each item will be at the same distance as the beginning of the line . And moreover, the spellings are the same as for commands, assuming the same as linking .
We also use image:
to define the image name used for each service, but you can also use docker-compose to rebuild the image by pointing to the path containing Dockerfile, it will build the image first for you. Proceed to start the container. We will then use the option build:
instead of the image.
Oke after running and printing, we will see on the screen: (looking at the running screen feels like a hack cmn ker =)) this you guys are fooling 100% of girls they believe, well, of course not IT girl)
1 2 3 4 5 6 7 8 9 |
$ docker-compose up Creating dockercompose_server_1 <span class="token punctuation">..</span> . Creating dockercompose_client_1 <span class="token punctuation">..</span> . Attaching to dockercompose_server_1, dockercompose_client_1 client_1 <span class="token operator">|</span> Received: Hello, world client_1 <span class="token operator">|</span> client_1 <span class="token operator">|</span> Received: Hello, world client_1 <span class="token operator">|</span> |
It pee 2: If you get an error like: “Couldn’t connect to Docker daemon at http + unix: //var/run/docker.sock—is it running?” , It’s most likely related to sudo permissions when run docker. I have the reference link above, pull it up and read it again =))
Ok, if you watch enough then you can Ctrl + C to exit the app. You can easily restart it with the up command like before without having to think about removing or anything. Remember that when recreating, the screen will appear Recreating instead of Creating as before.
4. Fasteners
This article shows you the problems that Docker Compose solves for you in practice, but when you run your hands (aka running), it is easy to make mistakes. Docker Compose or in other words helps you to pack applications, services, support you stream, which you have to configure manually or have to define it through scripts (although the principle of doing the same is just, someone is done for you. eat ready).
What I like about this guy is the ability to build quickly (because you only need to define a file and press a single command) when stopping or recreate is also extremely fast), greatly supporting the lip sync The school works among developers, especially during the start-up project phase. Moreover, the easy-to-understand config also helps readers who do not know anything can envision it.
In the following article I will mention a few more specific examples, point out the weaknesses of this tool, and may lead us to a new tool, which helps us to get closer to the lips. school productions, it’s k8s . Thank you for reading, all questions curse as usual, please comment below so I can discuss together.