Saturday, 9 May 2020

Docker Network: Communicate between containers

‘docker network’ command is used to establish communication between containers.

Step 1: Create a network.
docker network create my_network_1
$docker network create my_network_1
4e7b04536608fd84cc1bfad3fb45205063417df181f72f18b8eb3b6cae825a0d


Step 2: List all the networks.
docker network ls
$docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
f2fb9b25f38f        bridge              bridge              local
f0b207369485        host                host                local
4e7b04536608        my_network_1        bridge              local
7e1bfc494e53        none                null                local

Step 3: Add a container to the network ‘my_network_1’.

sleep.py
import time
while True:
    time.sleep(2)

Execute the below command to start the container container_1.

docker run --rm -d --net my_network_1 --name container_1 -v $(pwd):/src python:3 python3 /src/sleep.py

--rm: Remove the container once the command finish execution.
-d: Run the container in background, so we can't see all the log messaages of docker in terminal.
--net my_network_1: Add the container to network my_network_1.
--name container_1: Name of the container is container_1.
-v $(pwd):/src: Mount current directory to /src folder.
python:3: Image name with tag
python3 /src/sleep.py:  Command to run

$docker run --rm -d --net my_network_1 --name container_1 -v $(pwd):/src python:3 python3 /src/sleep.py
32e743cbe40f49ed9f581e2f5e4ff93f765df6ecb1724f1063b0f09ff91b9ab6

You can execute the command ‘docker container ls’ to confirm that the container is up and running.
$docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
32e743cbe40f        python:3            "python3 /src/sleep.…"   19 seconds ago      Up 18 seconds                           container_1

Step 4: Add the second container to the network ‘my_network_1’.

docker run --rm -it --net my_network_1 --name container_2 python:3 /bin/bash

Above command runs the container container_2 with image python:3 in interactive mode.
$docker run --rm -it --net my_network_1 --name container_2 python:3 /bin/bash
root@052aac3ac7c4:/#

Since both the containers container_1, container_2 are in the same network, you can ping the container ‘container_1’ from container_2.

root@052aac3ac7c4:/# ping container_1
PING container_1 (172.18.0.2) 56(84) bytes of data.
64 bytes from container_1.my_network_1 (172.18.0.2): icmp_seq=1 ttl=64 time=0.086 ms
64 bytes from container_1.my_network_1 (172.18.0.2): icmp_seq=2 ttl=64 time=0.174 ms
64 bytes from container_1.my_network_1 (172.18.0.2): icmp_seq=3 ttl=64 time=0.168 ms
64 bytes from container_1.my_network_1 (172.18.0.2): icmp_seq=4 ttl=64 time=0.274 ms
64 bytes from container_1.my_network_1 (172.18.0.2): icmp_seq=5 ttl=64 time=0.170 ms
64 bytes from container_1.my_network_1 (172.18.0.2): icmp_seq=6 ttl=64 time=0.166 ms
^C
--- container_1 ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 104ms
rtt min/avg/max/mdev = 0.086/0.173/0.274/0.054 ms

Come out of the container by executing ‘exit’ command.

Stop the container ‘container_1’.

$docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
32e743cbe40f        python:3            "python3 /src/sleep.…"   6 minutes ago       Up 6 minutes                            container_1
$
$docker container stop container_1

container_1
$
$
$docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

Remove the network ‘my_network_1’ by executing the below command.
docker network rm my_network_1

$docker network rm my_network_1
my_network_1
$
$docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
f2fb9b25f38f        bridge              bridge              local
f0b207369485        host                host                local
7e1bfc494e53        none                null                local
$



Previous                                                    Next                                                    Home

No comments:

Post a Comment