How to connect two docker instances

Mar. 12, 2022

This is one of the issue I see beginners struggle with. So I am just going to explain how to make two docker containers talk to each other.

There are essentially two methods:

  1. Make both containers in same docker network
  2. Expose the port to the host machine and connect using the host machine on same port

Let’s elaborate the two cases. For both cases, I will include docker-compose way and docker-cli way.

1. Make both containers in same docker network:

This is straight forward way to do it. All you have to do it create a docker network and assign the network to both the containers.

docker-image

1.1 docker-compose way

We will consider connecting a database to an app service. For this, you will generally have docker-compose to manage them both easily.

Let’s say you have the initial version as follows.

version 3:
services:
  cachedb:
    image: redis:latest
    volume:
      - ./app:/app
  app:
    image: myapp:latest

Here, cachedb is the redis database. app is our server.

After adding docker network:

version 3:
services:
  cachedb:
    image: redis:latest
    volume:
      - ./app:/app
    network:
      mynetwork
  app:
    image: myapp:latest
    network:
      mynetwork
networks:
  mynetwork:
    driver: bridge

Notice, we have defined a bridge network and called it mynetwork. And then, assigned it to both docker services.

1.2 Docker CLI version

docker network create mynetwork
docker run redis --network=mynetwork --name cachedb -d
docker run app --network=mynetworkl --name app  -d

Since both the containers share the same docker network, to connect the app to the cachedb we will refer the redis instance with the alias name and docker will be able to resolve the alias name to the IP address of the container.

For example to connect the redis instance, we will use the following:

REDIS_URL = "redis://cachedb:6379"

Note: instead of localhost we are instead referring it by the alias name. This means you don’t need to worry about keeping track of containers’ IP addresses, which can frequently change.

2. Through the host machine

docker-image-2 Know that the above mentioned method of getting the containers in the same network is the ideal way to connect two containers. But if you want to connect two containers in different networks, you can do it by exposing the port to the host machine and then connecting to the host machine.

Step 1: Grab the IP address of the host machine using the following command.

ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'

Step 2: Run the redis container while exposing the port to the host machine.

docker run -d -p 6379:6379 redis

Step 3: Since the redis instance is running on the host machine, we can connect to it using the host machine IP address.

docker run -p 8001:8001 --env REDIS_URL="redis://<HostIP>:6379" app

This is how you can connect two docker containers. I hope this helps you.