How to Connect One Container to Another Container in Linux

Docker is a open-source platform. We can create, modify, push & pull the images. It is used for developing, shipping, and running applications.

It is a platform that enables developers to build, deploy, run, update, and stop containers using simple commands.

Install Docker & Create a Docker Image

Connect one docker image to another docker images.

Create two containers:

docker run -d --name container-name1  -p 8001:80 image1
docker run -d --name container-name2  -p 8002:80 image2

We can also link one contianer to another container.

docker run --name container-name2 --link container-name1 image2

Create a new network:

docker network create network-name

Connect Both containers to the network:

docker network connect network-name container-name1
docker network connect network-name container-name2

Check if both containers are part of the new network:

docker network inspect network-name

Before  try to make the ping, we should install iputils-ping in the container to make it work.

Update the system.

apt-get update

Install iputils-ping

apt-get install iputils-ping

Now Run the ping command for test the connection:

docker exec -ti container-name1 ping container-name2
or
docker exec -it image_name1 ping image_name2

 

Leave a Reply

Your email address will not be published. Required fields are marked *