容器之间的 Docker HTTP 请求 [英] Docker HTTP-requests between containers
问题描述
我正处于学习如何使用 Docker 的第一阶段,所以我正在尝试基本的东西.我创建了两个需要通过 HTTP 请求交换数据的 Node Express 服务.
我的docker-compose.yml
文件
网络:隔离网络:司机:桥服务:service1-nodejs:建造:上下文:./service1/dockerfile: .docker/node.dockerfile端口:- 10000:9000"- 10001:5858"env_file: ./service1/.docker/env/app.${APP_ENV}.env网络:- 隔离网络service2-nodejs:建造:上下文:./service2/dockerfile: .docker/node.dockerfile端口:- 10010:9000"- 10011:5858"env_file: ./service2/.docker/env/app.${APP_ENV}.env网络:- 隔离网络
service1
使用请求模块向<代码>服务2代码>.
request({ url: "http://service2:10010/api/",方法:POST",标头:{内容类型":应用程序/json"},json:是的,身体: { ... },时间:真实}, 函数 (err, res, body) {如果 (!err && res.statusCode == 200) {//成功}//失败的});
这次调用的结果是:
<块引用>{ 错误:连接 ECONNREFUSED 172.18.0.3:10010}
使用邮递员,我可以在 http://localhost:10010/api/
测试 service2
,我可以确认它们实际上可以访问并按预期工作.>
我遗漏了一些东西,但无法弄清楚.这里出了什么问题?
查看文档.端口 10010 是主机端口,但不是容器端口.直接访问service2
容器时应该使用9000.
所以只需将 "http://service2:10010/api/"
更改为 "http://service2:9000/api/"
就可以了.
I'm at the first stage in learning how to use Docker so I'm trying basic things. I've created two Node Express services that need to exchange data via HTTP-requests.
My docker-compose.yml
file
networks:
isolation-network:
driver: bridge
services:
service1-nodejs:
build:
context: ./service1/
dockerfile: .docker/node.dockerfile
ports:
- "10000:9000"
- "10001:5858"
env_file: ./service1/.docker/env/app.${APP_ENV}.env
networks:
- isolation-network
service2-nodejs:
build:
context: ./service2/
dockerfile: .docker/node.dockerfile
ports:
- "10010:9000"
- "10011:5858"
env_file: ./service2/.docker/env/app.${APP_ENV}.env
networks:
- isolation-network
service1
uses the request module to make a POST-request to service 2
.
request({ url: "http://service2:10010/api/",
method: "POST",
headers: { "Content-Type": "application/json" },
json: true,
body: { ... },
time: true
}, function (err, res, body) {
if (!err && res.statusCode == 200) {
// success
}
// failed
});
The result of this call is:
{ Error: connect ECONNREFUSED 172.18.0.3:10010}
Using postman I can test service2
at http://localhost:10010/api/
and I can confirm they actually can be reached and work as expected.
I'm missing something but can't figure it out. What is going wrong here?
See the document. The port 10010 is a host port but not container port. You should use 9000 when you access service2
container directly.
So just change "http://service2:10010/api/"
to "http://service2:9000/api/"
and it will work.
这篇关于容器之间的 Docker HTTP 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!