Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to get Docker containers to talk to each other while running on my local host?
Introduction
Connection in Docker container can be made using various ways. Some of these are explained in this article. Docker networking using the command line interface and docker-compose. This networking leads to communication between the containers.
Pre-requisites
Install the below prerequisites before executing the commands.
Docker Engine
Docker compose
Methods
These methods are simple and easy to implement on the local machine.
Using CLI commands
Using Docker Compose
Using Command Line Interface
Here we are going to create two different containers and connect them using the docker networking features.
Step 1: Create a Docker network
<div class="code-mirror language-docker" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;">$ docker network create --driver bridge test_network 729f7d54a28063daaaef68693de94e5bd52fe9d65ec2c662021712be2d198511 </div>
Check if the new network "test_network" of driver type "bridge" is created or not.
<div class="code-mirror language-docker" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;">$ docker network ls </div>
Output
NETWORK ID NAME DRIVER SCOPE 0bfc15d4d317 bridge bridge local db9d7d7d4e55 host host local 6f035e31161b minikube bridge local dd1341a489b9 mynetwork bridge local 574f05aae08a none null local 729f7d54a280 test_network bridge local
Step 2: Create the first Docker container
Here we created our first Docker container and connected it with the network "test_network" we created in the previous step.
<div class="code-mirror language-docker" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;">$ docker run -itd --name container1 --network test_network ubuntu:latest </div>
Output
dbf034ade04f36fd30609552c22af78ef1be64e32c8629ec1a0737e9af0982b8
Step 3: Create a second Docker container
<div class="code-mirror language-docker" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;">~$ docker run -itd --name container2 --network test_network ubuntu:latest </div>
Output
ff0626f1459207871b60514bdd841957d18589e2299a68703f97ec075edcdfae
Step 4: Network check
Check if both containers are created under the "test_network" driver.
<div class="code-mirror language-docker" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;">$ docker network inspect test_network </div>
Output
[
{
"Name": "test_network",
"Id": "729f7d54a28063daaaef68693de94e5bd52fe9d65ec2c662021712be2d198511",
"Created": "2022-12-22T15:45:53.424766976+05:30",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.19.0.0/16",
"Gateway": "172.19.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"dbf034ade04f36fd30609552c22af78ef1be64e32c8629ec1a0737e9af0982b8": {
"Name": "container1",
"EndpointID":
"530321af4739e56e5d243586aa8aaed9c1889405c6163ecf886e9f053b978dd4",
"MacAddress": "02:42:ac:13:00:02",
"IPv4Address": "172.19.0.2/16",
"IPv6Address": ""
},
"ff0626f1459207871b60514bdd841957d18589e2299a68703f97ec075edcdfae": {
"Name": "container2",
"EndpointID":
"6f3f02ded90f7e52301a4de7c23df14b4953b999d02e9232335d883ab64e4c1a",
"MacAddress": "02:42:ac:13:00:03",
"IPv4Address": "172.19.0.3/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
Hence "container1" and "container2" both are on the same network.
Step 5: Check the connectivity
Get inside one of the containers and try to ping the other container. If the output shows positive signs, then we are communicating with the other container successfully.
<div class="code-mirror language-docker" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;">$ docker exec -it container1 /bin/bash </div>
Now we are inside the container1. But first, install the command.
<div class="code-mirror language-docker" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;">root@dbf034ade04f:/# apt install iputils-ping </div>
Output
Reading package lists... Done Building dependency tree... Done Reading state information... Done The following additional packages will be installed: libcap2-bin libpam-cap The following NEW packages will be installed: iputils-ping libcap2-bin libpam-cap 0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded. Need to get 76.8 kB of archives. After this operation, 280 kB of additional disk space will be used. Do you want to continue? [Y/n] y
Now ping the container2.
<div class="code-mirror language-docker" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;">root@dbf034ade04f:/# ping container2 </div>
Output
PING container2 (172.19.0.3) 56(84) bytes of data. 64 bytes from container2.test_network (172.19.0.3): icmp_seq=1 ttl=64 time=59.1 ms 64 bytes from container2.test_network (172.19.0.3): icmp_seq=2 ttl=64 time=0.217 ms 64 bytes from container2.test_network (172.19.0.3): icmp_seq=3 ttl=64 time=0.104 ms 64 bytes from container2.test_network (172.19.0.3): icmp_seq=4 ttl=64 time=0.213 ms ^C --- container2 ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3010ms rtt min/avg/max/mdev = 0.104/14.896/59.051/25.492 ms
Successfully able to communicate to the container and no packet was lost. Instead of using the container name, you can even use the IP address of the containers. Similarly, go inside "container2" and ping "container1" to check from the other side.
Using the Docker Compose
Here we will create a Docker Compose file with two container services. These services will be linked to one another and this linking will help the containers to talk to each other.
Step 1: Create the Docker Compose file
<div class="code-mirror language-docker" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;">version: '3'
services:
busybox1:
image: busybox:latest
container_name: busybox_container_1
command: sh -c 'ping busybox2'
links:
- busybox2
busybox2:
image: busybox:latest
container_name: busybox_container_2
command: sleep infinity
</div>
The container "busybox_container_1" is linked to "busybox_container_2" using the links tag. In the command tag, we used ping to communicate with the other container.
Step 2: Initiate the containerization process
Here we will see if we get the ping output or not. Ping will connect to the other container and send/receive packets.
<div class="code-mirror language-docker" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;">$ docker-compose up </div>
Output
Creating network "test_default" with the default driver Creating busybox_container_2 ... done Creating busybox_container_1 ... done Attaching to busybox_container_2, busybox_container_1 busybox_container_1 | PING busybox2 (172.27.0.2): 56 data bytes busybox_container_1 | 64 bytes from 172.27.0.2: seq=0 ttl=64 time=0.162 ms busybox_container_1 | 64 bytes from 172.27.0.2: seq=1 ttl=64 time=0.137 ms busybox_container_1 | 64 bytes from 172.27.0.2: seq=2 ttl=64 time=0.212 ms busybox_container_1 | 64 bytes from 172.27.0.2: seq=3 ttl=64 time=0.104 ms busybox_container_1 | 64 bytes from 172.27.0.2: seq=4 ttl=64 time=0.231 ms busybox_container_1 | 64 bytes from 172.27.0.2: seq=5 ttl=64 time=0.170 ms busybox_container_1 | 64 bytes from 172.27.0.2: seq=6 ttl=64 time=0.313 ms busybox_container_1 | 64 bytes from 172.27.0.2: seq=7 ttl=64 time=0.277 ms ^CGracefully stopping... (press Ctrl+C again to force) Stopping busybox_container_1 ... done Stopping busybox_container_2 ... done
Hence the pinging is working fine and the container is connected. The output of the ping command can also be seen in the above Docker Compose output.
Conclusion
We were successfully able to connect and communicate to the Docker container present on the local host. These methods are the most used and preferred in the industry. Once the connection is established we can do multiple tasks with the connected containers.
