Docker 101 : Networking : Connecting containers directly without the "docker0" bridge.



Sometimes we want to run a container that has the "main" application running and a "helper" container that is directly connected to the "main" one.


This "helper" container would for example run a logging program for the application running in the "main" container.

The docker bridge:

Usually when we create a new container it attaches itself to the default docker virtual bridge docker0.


We check it using the below command:


Attaching the "helper" container to the "main" container:

We attach our "helper" container to the "main" container. 
The "helper" container will have the same network address as the "main" container. 
We can't open the same ports on the "main" and "helper" container since we will have a conflict of ports.
The "helper" container is only accessible to the "main" container.

Example:

Starting the "main" container "debian_image_1":


We can check that it is still running in the background using the below command:


Starting the "helper" container "
debian_image_2":


We can check that it is still running in the background using the below command:


We use the below options:
  • -i : to run the images in interactive mode, so they don't exit after they start.
  • -d : to run the image in a detached mode in the background.
  • --net=container: attach the debian_image_2 directly to the debian_image_1 instead of the default docker0 bridge.
Checking the network interfaces in both containers:

For the debian_image_1 container:


For the debian_image_2 container:


We notice that they share the same network address.

Comments

Leave as a comment:

Archive