Operation and Maintenance
Docker
How to connect two Docker containers on the same network? (Bridge networking)
How to connect two Docker containers on the same network? (Bridge networking)
A custom bridge network (such as mynet) must be used to implement container name resolution. Docker's built-in DNS supports automatic IP update; disable --link and host port mapping, and containers communicate directly with container names.

Use docker network create to create a custom bridge network between containers
The default bridge network does not support container name resolution, and ping container_name directly will fail. You must build a user-defined bridge network yourself so that Docker will automatically enable the DNS service so that containers can discover each other by name.
- Run
docker network create mynet, don’t use the defaultbridge - Explicitly specify the network when starting the container:
docker run --network mynet --name db ...anddocker run --network mynet --name app ... - The two containers must be in the same custom network. Cross-networks (even if they are bridged) are not accessible by default.
Use the container name as the host name to access the container, not the IP address
In a custom network, Docker's built-in DNS will map the container name to the corresponding IP and support automatic updates - for example, if the IP changes after restarting the container, the name will still be available. Hard-writing IP addresses such as 172.18.0.2 is an anti-pattern and is prone to disconnection.
- When the application connects to the database, the address is
db:5432, not172.18.0.2:5432 - Use
ping dbornslookup dbto verify whether the parsing is successful in theappcontainer. - If
nslookup dbreturnsserver can't find db: NXDOMAIN, it means that it is not on the same network or the container has not started.
--link is obsolete, don't use it again
Docker 1.13 starts marking --link as an obsolete function. It only does one-way environment variable injection and /etc/hosts modification. It does not resolve DNS, does not support dynamic IP, is not compatible with Swarm, and cannot be linked across networks.
- Writing
docker run --link db:db ...is an outdated practice and is now completely redundant. - Even if
--linkis added, if the two containers are not in the same custom network, ping will still fail. - If you see
--linkin a new project, you should delete it and replace it with--networkcontainer name for direct connection.
The port does not need to be exposed to the host, and internal communication goes through the Docker intranet.
Inter-container calls do not require -p 5432:5432 port mapping. Host port mapping only affects external access, but increases the attack surface, wastes port resources, and may also cause startup failure due to port conflicts.
- The database container only needs to listen to
0.0.0.0:5432(or127.0.0.1:5432) without adding-p - The application container is directly connected using
db:5432, and the traffic goes through the Docker virtual bridge and does not go through the host iptables. - Adding
-pmay also trigger firewall rules or SELinux restrictions, which will increase the cost of troubleshooting.
The above is the detailed content of How to connect two Docker containers on the same network? (Bridge networking). For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undresser.AI Undress
AI-powered app for creating realistic nude photos
ArtGPT
AI image generator for creative art from text prompts.
Stock Market GPT
AI powered investment research for smarter decisions
Hot Article
Popular tool
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
20606
7
13699
4




