Allow two Docker containers to communicate with each other

To allow two Docker containers to communicate with each other, you can use a Docker network. Here's how to set it up:

Step 1: Create a Docker Network

First, create a user-defined network. This allows containers on the network to communicate with each other by their container names.

docker network create my_network

Step 2: Run Containers on the Same Network

When starting each container, specify the network you created.

For example:

docker run -d --name container1 --network my_network my_image1 docker run -d --name container2 --network my_network my_image2

Now, container1 and container2 are both on my_network and can communicate.

Step 3: Access Each Other by Container Name

Within container1, you can access container2 by its name, container2, and vice versa. For instance, you could use ping:

docker exec -it container1 ping container2

Or connect through a service port (e.g., for HTTP):

curl http://container2:port

Additional Tips:

  • Ports: If your containers expose services on specific ports (e.g., HTTP on port 80), make sure those services are listening on the correct port within the container.
  • DNS Resolution: Docker's network handles the DNS resolution automatically, so using container names as hostnames works seamlessly in user-defined networks.
  • Inspect Network : Run docker network inspect my_network to verify that both containers are connected.
相关推荐
小兔崽子去哪了6 小时前
Docker 删除镜像后磁盘空间没有释放?
后端·docker·容器
Zhu7587 小时前
在Docker环境部署ApacheGuacamole,对接PG数据库
数据库·docker·容器
少陽君8 小时前
Docker 部署 Loki + Promtail + Grafana 日志监控栈
docker·容器·grafana
独自归家的兔8 小时前
2026年7月2日 Docker 容器化部署 PaddleOCR-VL 实战(OpenAI 协议兼容 HTTP 调用)
运维·docker·容器
蜀道山老天师9 小时前
一文吃透 K8s:Deployment 滚动更新与版本回滚
云原生·容器·kubernetes
胖胖雕9 小时前
利用阿里云与docker部署Certimate
阿里云·docker·云计算
kylin-运维9 小时前
k8s可视化看板嵌入AI助手(悬浮对话球)
人工智能·容器·kubernetes·aicoding
会飞的土拨鼠呀10 小时前
Docker 容器维护手册
docker·容器·eureka
慕伏白10 小时前
【慕伏白】Docker 常用指令
docker·容器·eureka
java_logo10 小时前
Docker 部署 Trilium Notes 完整教程:自建大型知识库
docker·容器·trilium·trilium note·trilium部署·docker部署trilium·trilium部署文档