Docker Network and Containers communication

When you deal with docker containers with some basic configuration without services nor docker-compose, you may need to test their interaction, that’s the topic of this tutorial.

Let’s keep it simple. It’s an example of use out of the docker documentation.

Demystification of network communication between containers

 Let’s first review what network you have on your machine :

docker network ls

If you have something like the following :

5a85b45dcf73 elk_network bridge local

The bridge type of network (default one) will do the trick as per the documentation. However when looking at the docker guide, you can notice that you already own a default network under the name bridge and this one won’t fit all your needs.

Now issue the following :

 docker network inspect elk_network

We use elk_network as the network name for the sake of the example here, we will discuss more about the Elastic Stack in a next docker tutorial.

If you have already started your containers, they should be listed within the results of the above command.

Example :

"Containers": {
     "64a5b8d2a60e18ac6b7ae73b3e2bec2656d9da43976aef84ebaaba92335af6d6": {
    "Name": "silly_kirch",
    "EndpointID": "9110b162d10555201ced16573804630b3f839fc90009f0ef6fa10ba3c50560bb",
    "MacAddress": "02:42:ac:12:00:02",
    "IPv4Address": "172.18.0.2/16",
    "IPv6Address": ""
},

You will notice the IP addresses under the containers part. If you log into one of your running containers listed here, you can try to ping their respective IP address with success :

docker exec -it CONTAINER_ID bash

If you can not use bash, pick up sh instead.

ping IP_OF_OTHER_CONTAINER

This way makes sure you are on the same network.

How about answering to DNS request matching the name of your network ?

Make your container name reachable from within your containers

Try to ping a container :

ping OTHER_CONTAINER_NAME

ping: OTHER_CONTAINER_NAME: Name or service not known

The problem is that you can not do this with the default bridge network proposed by docker on installation (remember the bridge network name ?), you should build your own user defined network.

You could also alter the /etc/hosts files within your containers to fill in details about the name lookup but this is just a temporary solution as on the next start of your containers, the ip can change. You could of course add a script to handle that which runs on container start but once again for production, this is not the way to go and Docker has the solution for you !

Docker User defined network

Let’s first fix our network problem (in case you don’t already have a bridge network) by creating it with the proper type :

 docker create network elk_network

Now check via inspect that no container is attached then start your container and see them listed within the elk_network inspect.

So far, no big deal, same behavior. Now try again to ping using the name :

CONTAINER_ID BACH # ping OTHER_CONTAINER_NAME

That should work !

If you haven’ named your containers properly, try running them with the –name parameter. Beware the docker command you are using, if you are missing the –rm parameter while issuing docker run then you should use docker start otherwise you will have to remove your container first.

Running exec instead of attach

If you have followed the article carefully, you likely noticed that we are using docker exec and not docker attach in order to access the bash of a container.

If a container started with bash, then you could simply use the attach command. In the other case you need to start a new process to enter your container and use bash or shell…

I hope you enjoyed those lines as much as I did when I was trying things on my machine. If you learnt something, I’m very pleased and I will be looking forward hearing from you, don’t hesitate to drop a comment !

More resources :

Documentation for Docker run.

Docker attach command.

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Want more information?

Related links will be displayed within articles for you to pick up another good spot to get more details about software development, deployment & monitoring.

Stay tuned by following us on Youtube.

%d bloggers like this: