- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
$ docker network create --driver bridge test_network 729f7d54a28063daaaef68693de94e5bd52fe9d65ec2c662021712be2d198511
Check if the new network “test_network” of driver type “bridge” is created or not.
$ docker network ls
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.
$ docker run -itd --name container1 --network test_network ubuntu:latest
Output
dbf034ade04f36fd30609552c22af78ef1be64e32c8629ec1a0737e9af0982b8
Step 3: Create a second Docker container
~$ docker run -itd --name container2 --network test_network ubuntu:latest
Output
ff0626f1459207871b60514bdd841957d18589e2299a68703f97ec075edcdfae
Step 4: Network check
Check if both containers are created under the “test_network” driver.
$ docker network inspect test_network
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.
$ docker exec -it container1 /bin/bash
Now we are inside the container1. But first, install the command.
root@dbf034ade04f:/# apt install iputils-ping
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.
root@dbf034ade04f:/# ping container2
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
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
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.
$ docker-compose up
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.
- Related Articles
- Connecting From Docker Containers to Resources in Host
- How to list containers in Docker?
- How to remove old Docker containers?
- How to run amd64 Docker images on the arm64 host platform?
- How to install and use docker and containers on centos 7
- How to copy files from host to Docker container?
- How to get a Docker container's IP address from the host?
- How to move Docker containers between different hosts?
- Copying files from Docker container to Host
- How do I pass environment variables to Docker containers?
- Setting ulimit Values on Docker Containers?
- How to link jQuery from my local machine?
- How to talk to my neighbors who keep their TV volume very high?
- How to import local json file data to my JavaScript variable?
- How to get programmatically android host information?
