Skip to main content

Communication Between Docker Containers

Sometimes between docker containers, we need connect container B from container A to do fast test, etc. For most well-known example, we have FastAPI app container connect to postgres db container. In addition, it's likely to do some sql test on PostgreSQL db container in a third container.

Here some methods we can use.

Start a postgres db container:

docker run --rm --name postgres-db --detach -e POSTGRES_PASSWORD=mysecretpassword postgres

Run a postgres client container to connect the db container with user postgres and password mysecretpassword:

docker run -it --rm --link postgres-db:db postgres psql -h db -U postgres
psql (14.3)
Type "help" for help.

postgres=# SELECT 1;
?column?
----------
1
(1 row)

Or run a utility container:

docker run -it --rm --link postgres-db:db busybox sh
# in `busybox`
ping db

Using the default network

If you are running your container without specifying attached network, it will use the docker default bridge network.

However The default bridge network allows container-to-container communication by IP address only. To use hostname or alias name in connecting rather than IP address, see the following methods.

So before connecting, we need get the container IP address by docker inspect.

Start a postgres db container:

docker run --rm --name postgres-db --detach -e POSTGRES_PASSWORD=mysecretpassword postgres

Get the IP address of the postgres db container:

docker inspect mynginx | grep IPAddress
"IPAddress": "172.17.0.2",

Run a postgres client container to connect the db container:

docker run -it --rm postgres psql -h "172.17.0.2" -U postgres
psql (14.3)
Type "help" for help.

postgres=# SELECT 1;
?column?
----------
1
(1 row)

Using the private network

Creating private bridge network will give you more privacy that it only allows only containers belonging to it can talk to each other.

Moreover, you can use hostname or alias name to connect without regard of IP address changing due to re-start.

Create a private bridge network:

docker network create postgres-net

Start a postgres db container:

docker run --rm --net postgres-net --name postgres-db --detach -e POSTGRES_PASSWORD=mysecretpassword postgres

Run a postgres client container to connect the db container:

docker run -it --rm --net postgres-net postgres psql -h postgres-db -U postgres
psql (14.3)
Type "help" for help.

postgres=# SELECT 1;
?column?
----------
1
(1 row)

Use Case in Docker Compose

Actually, docker compose will create its private bridge network, and when it start containers, containers will be attached to that network in default.

docker-compose-postgres.yml
# Use below credentials to access in `adminer` web to access `db`,
# server: db (db1, db2 are also available!)
# user: postgres
# password: example
version: '3.1'

services:

db:
image: postgres
restart: always
environment:
# POSTGRES_USER: postgres # `postgres` in default.
POSTGRES_PASSWORD: example
networks:
default:
aliases:
- db1
- db2
adminer:
image: adminer
restart: always
ports:
- 8080:8080