Thursday, 25 July 2024

communication between two docker container

 

To enable communication between two Docker containers, you can use Docker networks. Docker networks allow containers to discover and communicate with each other. Here are the steps to set up communication between two Docker containers:

1. Create a Docker Network

First, create a custom Docker network. This will allow your containers to communicate with each other using their container names as hostnames.

sh
docker network create my_network
 

2. Run Containers on the Same Network

Run your containers and attach them to the network you just created.

For example, if you have two containers named container1 and container2:

sh
docker run -d --name container1 --network my_network my_image1
docker run -d --name container2 --network my_network my_image2

3. Verify Network Connectivity

You can verify that the containers are on the same network by running:

docker network inspect my_network
This command will show you details about the network, including the containers attached to it.

 

4. Test Communication

To test the communication between the containers, you can use the docker exec command to enter one container and ping the other.

For example, from container1, you can ping container2:

docker exec -it container1 ping container2
 

 

version: '3'
services:
container1:
image: my_image1
networks:
- my_network

container2:
image: my_image2
networks:
- my_network

networks:
my_network:
driver: bridge
 

 

 

No comments:

Post a Comment