Communication between containers in Docker – Docker Compose

Tram Ho

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

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:

Next, create a server-side Dockerfile with the following content:

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

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 :

and Dockerfile

Then the same we build the client:

And to determine its ability to run, we will run 2 statements:

When you’re done, log out of the client and delete the container:

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:

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: ? 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)

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.

Share the news now

Source : Viblo