如何使用Docker在两个容器之间进行通信

编程入门 行业动态 更新时间:2024-10-27 06:28:47
本文介绍了如何使用Docker在两个容器之间进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在访问另一个集装箱中的一个集装箱路线时遇到问题。例如,我有两个名为 user-service 和 api-gateway 的微服务。我正在尝试访问 api网关中的用户服务路由。

I'm facing an issue to access one container route in another container. For example i have two micro services called user-service and api-gateway. I'm trying to access user-service route in api-gateway.

我的 api-gateway 文件可能如下所示

const userServiceProxy = httpProxy(localhost:8093); this.app.post('/admin/register', async(req, res) => { userServiceProxy(req, res); });

api-gateway 在端口8080上运行

api-gateway is running on port 8080

我的用户服务文件可能如下所示

app.post('/admin/register', function (req, res) { res.send('POST request') })

当我使用端口8080通过 api-gateway 访问路由时,我无法调用该路由,但是当我尝试使用端口8093我可以看到结果。

when i access route through api-gateway with the port 8080 i couldn't able to call the route but when i tried to access with the port 8093 i can able to see the result.

我的 docker-compose 文件可能如下所示

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"

任何帮助都会b非常感谢,谢谢!!

Any help would be greatly appreciated, Thanks in advance!

推荐答案

只需向您添加docker-compose文件的网络规范以使用自定义网桥网络。

just add a network specification to you docker-compose file to use a custom bridge network.

类似的内容可能对您有用

something like that might work for you

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中指定的端口和服务,现在可以进行以下连接:

according to your specified ports and services in your docker-compose the following connections are now possible:

  • api网关容器: user-service:8093
  • 用户服务容器: api-网关:8080
  • api-gateway container: user-service:8093
  • user-service container: api-gateway:8080

如果我做对了,您的api网关现在应该是:

if i get it right, your api-gateway would now be:

const userServiceProxy = httpProxy(user-service:8093); this.app.post('/admin/register', async(req, res) => { userServiceProxy(req, res); });

在docker网络内部,您可以直接从其他容器访问端口(无需指定端口-映射到您的主机)。因此,可能不需要您的端口映射之一。如果您仅通过api-gateway而不是直接访问用户服务,则可以在docker-compose文件(用户服务块)中删除端口规范。您的用户服务将只能通过api-gateway访问。可能正是您想要的。

inside a docker network you can access the ports from other containers directly (no need to specify a port-mapping to your host). probably one of your port-mappings is therefore unnecessary. if you are accessing the user-service only via api-gateway and not directly you may remove the port specification in your docker-compose files (user-service block). your user-service would then be accessible only using the api-gateway. which is probably what you want.

更多推荐

如何使用Docker在两个容器之间进行通信

本文发布于:2023-10-08 09:59:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1472296.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   容器   两个   通信   Docker

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!