如何使用Docker在两个容器之间进行通信 [英] How to communicate between two containers using docker

查看:666
本文介绍了如何使用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(http://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(http://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在两个容器之间进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆