How to run Multiple Docker Containers in the same Network on Debian 11.3

To Run Multiple Docker Containers in the Same Network on Debian 11.3

Introduction:

When Docker runs multiple containers, you can connect them to the same network to communicate. Docker creates virtual networks that let your containers communicate with each other. Containers have IP addresses and hostnames in a network.

Installation Procedure:

Step 1: Check the OS version by using the below command

root@linuxhelp:~# lsb_release -a
No LSB modules are available.
Distributor ID:	Debian
Description:	Debian GNU/Linux 11 (bullseye)
Release:	11
Codename:	bullseye

Step 2: Create Docker network named ‘my networking’ by using the below command

root@linuxhelp:~# docker network create \
> --subnet=192.168.2.0/23 \
> --ip-range=192.168.2.0/24 \
> --gateway=192.168.2.1 \
> my networking
85dcb32835db477819bced835f83a97611228bfb154656768af7923851f4bf1e

Step 3: List the Networks by using the below command

root@linuxhelp:~# docker network ls
NETWORK ID     NAME           DRIVER    SCOPE
edb2129c3783   bridge         bridge    local
fa956c848db3   host           host      local
85dcb32835db   mynetworking   bridge    local
b07e450c26b4   none           null      local

Step 4: Pull the Debian Image from Docker Hub by using the below command

root@linuxhelp:~# docker pull debian
Using default tag: latest
latest: Pulling from library/debian
001c52e26ad5: Pull complete
Digest: sha256:82bab30ed448b8e2509aabe21f40f0607d905b7fd0dec72802627a20274eba55
Status: Downloaded newer image for debian:latest
docker.io/library/debian:latest

Step 5: Run the Debian container in ‘mynetworking’ Docker network named as ‘debian1’ by using the below command

root@linuxhelp:~# docker run --name debian1 --net mynetworking --ip 192.168.2.10 -itd debian
60a7e1b64b25d0c0c4a0901b402b17f72ff4d859f038ab1e20390efb23fd1cfb

Step 6: Run the Debian container in ‘my networking’ Docker network named as ‘debian2’ by using the below command

root@linuxhelp:~# docker run --name debian2 --net mynetworking --ip 192.168.2.11 -itd debian
8d83b44f634d8707362f3bb164578b231214d6208eba6c465fb06c759309592d

Step 7: Login to the debian1 container’s Shell by using the below command

root@linuxhelp:~# docker exec -it debian1 bash

Step 8: Update the APT source List by using the below command

root@60a7e1b64b25:/# apt update
Get:1 http://deb.debian.org/debian bullseye InRelease [116 kB]
Get:2 http://deb.debian.org/debian-security bullseye-security InRelease [48.4 kB]
Get:3 http://deb.debian.org/debian bullseye-updates InRelease [44.1 kB]
Get:4 http://deb.debian.org/debian bullseye/main amd64 Packages [8182 kB]
Get:5 http://deb.debian.org/debian-security bullseye-security/main amd64 Packages [176 kB]
Get:6 http://deb.debian.org/debian bullseye-updates/main amd64 Packages [2592 B]
Fetched 8569 kB in 2s (3606 kB/s)               
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
3 packages can be upgraded. Run 'apt list --upgradable' to see them.

root@60a7e1b64b25:/# apt list --upgradable
Listing... Done
libgnutls30/stable-security 3.7.1-5+deb11u2 amd64 [upgradable from: 3.7.1-5+deb11u1]
libtirpc-common/stable-security 1.3.1-1+deb11u1 all [upgradable from: 1.3.1-1]
libtirpc3/stable-security 1.3.1-1+deb11u1 amd64 [upgradable from: 1.3.1-1]

Step 9: Install the Ping Utility Package by using the below command

root@2e5931fc803a:/# apt install inetutils-ping
Reading package lists... Done

Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  netbase
The following NEW packages will be installed:
  inetutils-ping netbase
0 upgraded, 2 newly installed, 0 to remove and 3 not upgraded.
Need to get 264 kB of archives.
After this operation, 415 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://deb.debian.org/debian bullseye/main amd64 netbase all 6.3 [19.9 kB]
Get:2 http://deb.debian.org/debian bullseye/main amd64 inetutils-ping amd64 2:2.0-1 [245 kB]
Fetched 264 kB in 0s (995 kB/s)        
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package netbase.
(Reading database ... 6661 files and directories currently installed.)
Preparing to unpack .../archives/netbase_6.3_all.deb ...
Unpacking netbase (6.3) ...
Selecting previously unselected package inetutils-ping.
Preparing to unpack .../inetutils-ping_2%3a2.0-1_amd64.deb ...
Unpacking inetutils-ping (2:2.0-1) ...
Setting up netbase (6.3) ...
Setting up inetutils-ping (2:2.0-1) ...

Step 10: Ping the Debian2 Container by using the below IP address

root@60a7e1b64b25:/# ping 192.168.2.11
PING 192.168.2.11 (192.168.2.11): 56 data bytes
64 bytes from 192.168.2.11: icmp_seq=0 ttl=64 time=0.180 ms
64 bytes from 192.168.2.11: icmp_seq=1 ttl=64 time=0.085 ms
64 bytes from 192.168.2.11: icmp_seq=2 ttl=64 time=0.186 ms
64 bytes from 192.168.2.11: icmp_seq=3 ttl=64 time=0.081 ms
64 bytes from 192.168.2.11: icmp_seq=4 ttl=64 time=0.181 ms
^C--- 192.168.2.11 ping statistics ---
16 packets transmitted, 16 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.081/0.180/0.533/0.115 ms

Step 11: Exit from the Container

root@60a7e1b64b25:/# exit
exit

Step 12: Login to the Debian2 container’s Shell by using the below command

root@linuxhelp:~# docker exec -it debian2 bash

Step 13: Update the APT source List by using the below command

root@8d83b44f634d:/# apt update
Get:1 http://deb.debian.org/debian bullseye InRelease [116 kB]
Get:2 http://deb.debian.org/debian-security bullseye-security InRelease [48.4 kB]
Get:3 http://deb.debian.org/debian bullseye-updates InRelease [44.1 kB]
Get:4 http://deb.debian.org/debian bullseye/main amd64 Packages [8182 kB]

Get:5 http://deb.debian.org/debian-security bullseye-security/main amd64 Packages [176 kB]
Get:6 http://deb.debian.org/debian bullseye-updates/main amd64 Packages [2592 B]
Fetched 8569 kB in 2s (3994 kB/s)                         
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
3 packages can be upgraded. Run 'apt list --upgradable' to see them.

root@8d83b44f634d:/# apt list --upgradable
Listing... Done
libgnutls30/stable-security 3.7.1-5+deb11u2 amd64 [upgradable from: 3.7.1-5+deb11u1]
libtirpc-common/stable-security 1.3.1-1+deb11u1 all [upgradable from: 1.3.1-1]
libtirpc3/stable-security 1.3.1-1+deb11u1 amd64 [upgradable from: 1.3.1-1]

Step 14: Install the Ping Utility Package by using the below command

root@8d83b44f634d:/# apt install inetutils-ping
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  netbase
The following NEW packages will be installed:
  inetutils-ping netbase

0 upgraded, 2 newly installed, 0 to remove and 3 not upgraded.
Need to get 264 kB of archives.
After this operation, 415 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://deb.debian.org/debian bullseye/main amd64 netbase all 6.3 [19.9 kB]
Get:2 http://deb.debian.org/debian bullseye/main amd64 inetutils-ping amd64 2:2.0-1 [245 kB]
Fetched 264 kB in 0s (1034 kB/s)    
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package netbase.
(Reading database ... 6661 files and directories currently installed.)
Preparing to unpack .../archives/netbase_6.3_all.deb ...
Unpacking netbase (6.3) ...
Selecting previously unselected package inetutils-ping.
Preparing to unpack .../inetutils-ping_2%3a2.0-1_amd64.deb ...
Unpacking inetutils-ping (2:2.0-1) ...
Setting up netbase (6.3) ...
Setting up inetutils-ping (2:2.0-1) ...

Step 15: Ping the Ubuntu2 Container by IP address by using the below command

root@8d83b44f634d:/# ping 192.168.2.10
PING 192.168.2.10 (192.168.2.10): 56 data bytes
64 bytes from 192.168.2.10: icmp_seq=0 ttl=64 time=0.085 ms
64 bytes from 192.168.2.10: icmp_seq=1 ttl=64 time=0.181 ms

64 bytes from 192.168.2.10: icmp_seq=2 ttl=64 time=0.348 ms
64 bytes from 192.168.2.10: icmp_seq=3 ttl=64 time=0.247 ms
64 bytes from 192.168.2.10: icmp_seq=4 ttl=64 time=0.183 ms
64 bytes from 192.168.2.10: icmp_seq=5 ttl=64 time=0.233 ms
^C--- 192.168.2.10 ping statistics ---
6 packets transmitted, 6 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.085/0.213/0.348/0.080 ms

Step 16: Exit from the Container

root@8d83b44f634d:/# exit
exit

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to run Multiple Docker Containers in the same Network on Debian 11.3. Your feedback is much welcome.

FAQ
Q
What is the default Docker network?
A
When Docker is installed, a default bridge network named docker0 is created. Each new Docker container is automatically attached to this network unless a custom network is specified.
Q
How to create a Docker Network?
A
Docker network create \
>--subnet \
>--ip-range \
>--gateway \
>network name
Q
What does IP 0.0 0.0 mean Docker?
A
0.0.0.0 means all available interfaces which do include localhost but also others e.g. 192.168.0.123.
Q
What is the Docker IP address?
A
By default, the container is assigned an IP address for every Docker network it connects to. The IP address is assigned from the pool assigned to the network, so the Docker daemon effectively acts as a DHCP server for each container. Each network also has a default subnet mask and gateway.
Q
What is a Docker network?
A
Docker networking allows you to attach a container to as many networks as you like. You can also attach an already-running container.