SoFunction
Updated on 2025-04-11

The steps to implement mutual communication between multiple containers of docker

When running multiple Docker containers on the same host, communication can be achieved in the following ways:

1. Use Docker default network (Bridge network)

Docker will assign one to each container by defaultbridgeNetwork, containers can communicate with each other through IP addresses or container names.

Implementation steps

  • When creating a container, Docker will automatically connect it to the defaultbridgenetwork.
  • The containers can be passedContainer nameorIP addressCommunication.

Example

Start two containers:

docker run -d --name container1 nginx
docker run -d --name container2 nginx

existcontainer1Visitcontainer2

docker exec -it container1 ping container2

Things to note

  • The defaultbridgeThe network does not support direct communication through container names (need to use--link, but it is not recommended).
  • Custom networking is recommended (see below).

2. Use a custom Docker network

Docker allows users to create custom networks. After the container is connected to the same custom network, they can communicate directly through the container name.

Implementation steps

Create a custom network:

docker network create mynetwork

Start the container and connect to a custom network:

docker run -d --name container1 --network mynetwork nginx
docker run -d --name container2 --network mynetwork nginx

Communication between containers through container names:

existcontainer1Visitcontainer2

docker exec -it container1 ping container2

advantage

  • Supports direct communication through container names.
  • Good network isolation and high security.

3. Use--linkParameters (not recommended)

Docker early support passed--linkParameters connect containers together, but this method has been deprecated and it is recommended to use a custom network.

Example: Start the container and use--link

docker run -d --name container1 nginx
docker run -d --name container2 --link container1 nginx

existcontainer2Visitcontainer1

docker exec -it container2 ping container1

shortcoming

  • Limited features and dynamic updates are not supported.
  • No longer recommended.

4. Use the Host Network

Connect the container to the host's network stack, and the container directly uses the host's network interface.

Implementation steps

Used when starting the container--network host

docker run -d --name container1 --network host nginx
docker run -d --name container2 --network host nginx

Can be passed between containerslocalhostor the host's IP address communication.

Things to note

  • Containers share network stacks with hosts, which may cause port conflicts.
  • Low security and is not recommended for use in production environments.

5. Use Docker Compose

Docker Compose is an ideal tool for managing multi-container applications, which automatically creates custom networks for containers and enables container communication through service names.

Implementation steps

createdocument:

version: '3'
services:
  web:
    image: nginx
    ports:
      - "8080:80"
  db:
    image: mysql
    environment:
      MYSQL_ROOT_PASSWORD: example

Start the service:

docker-compose up -d

existwebAccess in the servicedbServe:

Use the service name directlydbAs hostname:

docker-compose exec web ping db

advantage

  • Automatically create a custom network, and communicate between containers through service names.
  • Simplify management of multi-container applications.

6. Using shared data volumes (indirect communication)

If data is needed to be shared between containers, indirect communication can be achieved by mounting the same data volume.

Implementation steps

Create a data volume:

docker volume create mydata

Start the container and mount the data volume:

docker run -d --name container1 -v mydata:/app/data nginx
docker run -d --name container2 -v mydata:/app/data nginx

Communication between containers through a shared file system.

Summarize

Communication method Implementation method advantage shortcoming
Default Bridge Network Containers communicate via IP or name Simple and easy to use Container name communication is not supported by default
Customize the network Create a custom network, containers communicate by name Support container name communication, good network isolation Need to create a network manually
–link (not recommended) use--linkConnect the container Early support for container name communication Limited function, deprecated
Host Network Container shared hosting network stack Directly use the host network Low security, which may lead to port conflicts
Docker Compose Automatically create a network, and the container communicates through the service name Simplify multi-container management and support service name communication Need to be writtendocument
Shared data volumes Containers communicate through shared file system Suitable for shared data scenarios Applicable to file system communication only

Recommended plan

  • Development Environment: Use Docker Compose to simplify multi-container management.
  • Production environment: Use a custom network to ensure network isolation and security.
  • Special scenes: If you need to share data, you can use a shared data volume.

This is the end of this article about the mutual communication between multiple docker containers. For more related docker container communication content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!