在docker-compose中的不同docker服务之间进行通信 [英] Communicating between different docker services in docker-compose
问题描述
我刚刚开始使用docker-compose,目前正在努力处理不同服务之间的通信。
I just started working with docker-compose and am currently struggling with communication between the different services.
我有2个服务, alice
和 bob
。我希望它们能够相互发送http请求。据我了解,服务应该能够通过使用服务名作为主机名来相互访问。
I have 2 services, alice
and bob
. I want these to be able to send http requests to each other. As far as I understood, services should be able to reach each other by using the servicename as hostname.
不幸的是, alice
在我的示例中无法通过 http:// bob到达 bob
:5557 ,而 bob
无法通过 http:// alice:5556 。
Unfortunately, alice
in my example is not able to reach bob
on http://bob:5557, and bob
is not able to reach alice
on http://alice:5556.
我无法正确理解什么?
What am I not understanding correctly? Is it even possible to make http requests between services?
这是我的 docker-compose.yml 文件:
version: '3'
services:
alice:
build: blockchain
ports:
- "5556:5000"
environment:
NAME: Alice
bob:
build: blockchain
ports:
- "5557:5000"
environment:
NAME: Bob
推荐答案
使用CONTAINER_PORT进行联网的服务到服务通信
Networked service-to-service communication use the CONTAINER_PORT
因此,您应该使用容器端口在容器之间进行通信。
http:// bob:5000
和 http:// alice:5000
。
Thus you should use the container ports to communicate between the containers.
http://bob:5000
and http://alice:5000
.
这篇关于在docker-compose中的不同docker服务之间进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!