Node.js 如何使用docker在两个容器之间进行通信
我面临一个问题,无法在另一个容器中访问一个容器路径。例如,我有两个名为用户服务和api网关的微服务。我正在尝试访问api网关中的用户服务路由 我的api网关文件可能如下所示Node.js 如何使用docker在两个容器之间进行通信,node.js,docker,Node.js,Docker,我面临一个问题,无法在另一个容器中访问一个容器路径。例如,我有两个名为用户服务和api网关的微服务。我正在尝试访问api网关中的用户服务路由 我的api网关文件可能如下所示 const userServiceProxy = httpProxy(http://localhost:8093); this.app.post('/admin/register', async(req, res) => { userServiceProxy(req, res); });
const userServiceProxy = httpProxy(http://localhost:8093);
this.app.post('/admin/register', async(req, res) => {
userServiceProxy(req, res);
});
app.post('/admin/register', function (req, res) {
res.send('POST request')
})
version: '3'
services:
api-gateway:
container_name: api-gateway
build: './api-gateway'
ports:
- "8080:8080"
links:
- user-service
user-service:
build: ./user-service
container_name: user-service
ports:
- "8093:8093"
api网关正在端口8080上运行
我的用户服务文件可能如下所示
const userServiceProxy = httpProxy(http://localhost:8093);
this.app.post('/admin/register', async(req, res) => {
userServiceProxy(req, res);
});
app.post('/admin/register', function (req, res) {
res.send('POST request')
})
version: '3'
services:
api-gateway:
container_name: api-gateway
build: './api-gateway'
ports:
- "8080:8080"
links:
- user-service
user-service:
build: ./user-service
container_name: user-service
ports:
- "8093:8093"
当我使用端口8080通过api网关访问路由时,我无法调用路由,但当我尝试使用端口8093访问路由时,我可以看到结果
我的docker compose文件可能如下所示
const userServiceProxy = httpProxy(http://localhost:8093);
this.app.post('/admin/register', async(req, res) => {
userServiceProxy(req, res);
});
app.post('/admin/register', function (req, res) {
res.send('POST request')
})
version: '3'
services:
api-gateway:
container_name: api-gateway
build: './api-gateway'
ports:
- "8080:8080"
links:
- user-service
user-service:
build: ./user-service
container_name: user-service
ports:
- "8093:8093"
任何帮助都将不胜感激,提前谢谢
localhost
指容器内的本地主机,而不是主机系统
使用localhost
并将其替换为服务名称,如api网关
如果容器位于同一网络中,则地址http://api-gateway:8093
应该可以
另一种方法是在网络模式下运行容器
host
。这是较少的隔离,但是地址localhost
起作用,因为容器现在运行在docker deamon的接口上还有一个鲜为人知的选项,可以将两个或多个容器连接在一起:
version: '3'
services:
test1:
image: alpine
command: nc -lp 1337
test2:
image: alpine
command: nc -lp 1337
network_mode: service:test1
这类似于Kubernetes吊舱的内部网络——以这种方式链接的容器共享相同的内部地址(发布的端口在本地主机上可见,并且可能相互冲突)。因此,上述配置将无法工作,因为将发生端口冲突:
➜ docker-pod-test docker-compose up
Creating docker-pod-test_test1_1 ... done
Creating docker-pod-test_test2_1 ... done
Attaching to docker-pod-test_test1_1, docker-pod-test_test2_1
test2_1 | nc: bind: Address in use
docker-pod-test_test2_1 exited with code 1
只需在docker compose文件中添加网络规范,即可使用自定义网桥网络 像这样的东西可能对你有用
version: '3'
services:
api-gateway:
container_name: api-gateway
build: './api-gateway'
ports:
- "8080:8080"
networks:
- mynet
user-service:
build: ./user-service
container_name: user-service
ports:
- "8093:8093"
networks:
- mynet
networks:
mynet:
driver: bridge
ipam:
driver: default
根据docker compose中指定的端口和服务,现在可以进行以下连接:
- api网关容器:
用户服务:8093
- 用户服务容器:
api网关:8080
const userServiceProxy = httpProxy(http://user-service:8093);
this.app.post('/admin/register', async(req, res) => {
userServiceProxy(req, res);
});
在docker网络中,您可以直接从其他容器访问端口(无需指定到主机的端口映射)。因此,您的一个端口映射可能是不必要的。如果您仅通过api网关而不是直接访问用户服务,则可以删除docker compose文件(用户服务块)中的端口规范。您的用户服务将只能使用api网关访问。这可能就是您想要的。最简单的方法是使用docker标志
--net=host
,这样您的docker容器将使用主机的网络,而不是它们自己的名称空间网络。e、 g
docker run -d --network host -p 80:80 nginx
您可以查看nginx欢迎页面。什么是
httpProxy
对象?当用户尝试使用端口8080访问路由时,它通常重定向到端口8093上运行的用户服务路由。顺便说一句,我使用了ExpressHTTP代理,谢谢你的回答@Wie!你能建议我如何使用Docker Networks吗?很抱歉,我离开几天,@shaped的例子是正确的。非常感谢你@shaped!这就是我一直在寻找的,它像冠军一样工作。