Docker使用端点组成内部通信 [英] Docker-compose internal communication using endpoints

查看:88
本文介绍了Docker使用端点组成内部通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

致力于在单个 docker-compose.yml 中运行两个不同的服务以进行w通信。彼此在 docker-compose 之内。

Working on getting two different services running inside a single docker-compose.yml to communicate w. each other within docker-compose.

这两个服务都是常规的NodeJS服务器( app1 & app2 )。 app1 从外部来源接收 POST 请求,然后应将请求发送到另一个NodeJS服务器 app2 w。基于初始 POST 请求的信息。

The two services are regular NodeJS servers (app1 & app2). app1 receives POST requests from an external source, and should then send a request to the other NodeJS server, app2 w. information based on the initial POST request.

我面临的挑战是如何制作两个NodeJS容器通信w。彼此不对特定的容器名称进行硬编码。我可以使两个容器当前进行通信的唯一方法是对URL进行硬编码,例如: http:// myproject_app1_1 ,然后它将引导 app1 app2 的POST 请求正确,但是由于Docker递增容器名称的方式,它的伸缩性不太好,也不支持潜在的容器崩溃等。

The challenge that I'm facing is how to make the two NodeJS containers communicate w. each other w/o hardcoding a specific container name. The only way I can get the two containers to communicate currently, is to hardcode a url like: http://myproject_app1_1, which will then direct the POST request from app1 to app2 correctly, but due to the way Docker increments container names, it doesn't scale very well nor support potential container crashing etc.

相反,我希望发送 POST 请求类似于 http:// app2 的内容或类似的方法来处理和命名多个容器,无论<$有多少实例c $ c> app2 容器存在Docker将通过运行的 app2 容器之一传递请求。

Instead I'd prefer to send the POST request to something along the lines of http://app2 or a similar way to handle and alias a number of containers, and no matter how many instances of the app2 container exists Docker will pass the request one of the running app2 containers.

这是我的 docker-compose.yml 文件的示例:

version: '2'

services:

  app1:
    image: 'mhart/alpine-node:6.3.0'
    container_name: app1
    command: npm start

  app2:
    image: 'mhart/alpine-node:6.3.0'
    container_name: app2
    command: npm start

  # databases [...]

谢谢。

推荐答案

当您从一个撰写文件运行两个容器时,泊坞窗会自动设置一个内部dns,该内部dns可以通过在撰写文件中定义的 service 名称来引用其他容器(假设他们在同一个网络中)。因此,这在从第一个服务中引用 http:// app2 时应该起作用。

When you run two containers from one compose file, docker automatically sets up an "internal dns" that allows to reference other containers by their service name defined in the compose file (assuming they are in the same network). So this should work when referencing http://app2 from the first service.

请参见以下示例,从只需使用服务名称,即可将 proxy 到后端 whoamiapp

See this example proxying requests from proxy to the backend whoamiapp by just using the service name.

default.conf

default.conf

server {
    listen 80;

    location / {
        proxy_pass http://whoamiapp;
    }
}

docker-compose.yml

docker-compose.yml

version: "2"
services:
  proxy:
    image: nginx
    volumes:
        - ./default.conf:/etc/nginx/conf.d/default.conf:ro
    ports:
    - "80:80"

  whoamiapp:
    image: emilevauge/whoami

使用 docker-compose up运行它-d 并尝试运行 curl< dockerhost>

此示例使用docker-compose文件版本2的默认网络。您可以在此处阅读有关docker-compose网络工作原理的更多信息: https://docs.docker.com/compose/networking/

This sample uses the default network with docker-compose file version 2. You can read more about how networking with docker-compose works here: https://docs.docker.com/compose/networking/

可能您配置的 container_name 财产以某种方式干扰了这种行为?您不需要自己定义它。

Probably your configuration of the container_name property somehow interferes with this behaviour? You should not need to define this on your own.

这篇关于Docker使用端点组成内部通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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