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.
相关推荐
伊成5 小时前
细说Docker命令
docker·容器·eureka
java_logo6 小时前
vllm-openai Docker 部署手册
运维·人工智能·docker·ai·容器
jiyuzzz7 小时前
Docker部署WordPress及相关配置
运维·docker·容器
Micro麦可乐7 小时前
Centos Stream 9 中Docker安装出现 download.docker.com:443 的问题解决
linux·docker·centos·podman
LeeZhao@7 小时前
【具身智能】具身机器人VLA算法入门及实战(一):具身智能系统及VLA
人工智能·docker·语言模型·机器人
m0_719759228 小时前
Docker 常用命令
docker
风清再凯9 小时前
05-k8s网络
网络·容器·kubernetes
西柚小萌新10 小时前
【Bug:docker】--Docker国内镜像源加载失败
docker·容器·bug
爱宇阳11 小时前
GitLab Docker Compose 迁移教程
docker·容器·gitlab
冲上云霄的Jayden13 小时前
ubuntu 22一步步 安装docker和配置使用国内源
linux·ubuntu·docker·国内源