简体   繁体   English

docker-compose可以在具有离散端口的服务之间共享ip吗?

[英]Can docker-compose share an ip between services with discrete ports?

We currently have docker containers with complex builds using supervisord so that we can group services together. 当前,我们具有使用supervisor进行复杂构建的docker容器,以便我们可以将服务分组在一起。 For example, nginx and ssh. 例如,nginx和ssh。

I'm attempting to rebuild these with more service-driven isolation linked by shared volumes. 我试图通过共享卷链接更多的服务驱动的隔离来重建它们。 However, without mapping the IP to the host , I can't seem to find a way to allow IP addresses to be shared even though the ports may be discrete. 但是, 如果不将IP映射到主机 ,即使端口可能是离散的,我似乎也找不到找到共享IP地址的方法。

What I'm trying to do is something like this: 我想做的是这样的:

version: '2'
services:
  web:
    image: nginx
    volumes:
    - /data/web:/var/www
    networks:
      public:
        ipv4_address: 10.0.0.1
    ports:
    - "10.0.0.1:80:80"
  ssh:
    image: alpine-sshd
    volumes:
    - /data/web:/var/www
    networks:
      public:
        ipv4_address: 10.0.0.1
    ports:
    - "10.0.0.1:22:22"
networks:
  public:
    external: true

...where public is a predefined docker macvlan network. ...其中public是预定义的docker macvlan网络。

When I try this, I get the error: 当我尝试这个时,我得到了错误:

ERROR: for ssh  Cannot start service ssh: Address already in use

I'm aware that another solution to this is to introduce a third service to work as a proxy. 我知道解决此问题的另一种方法是引入第三项服务作为代理。 However, I thought this would be a simple enough case not to need it. 但是,我认为这是一个足够简单的案例,不需要它。

Is it possible to configure docker-compose/docker-networking to route by the port to allow the same IP address to be used for different containers? 是否可以将docker-compose / docker-networking配置为通过端口路由,以允许将相同的IP地址用于不同的容器?

Is it possible to configure docker-compose/docker-networking to route by the port to allow the same IP address to be used for different containers? 是否可以将docker-compose / docker-networking配置为通过端口路由,以允许将相同的IP地址用于不同的容器?

Yes we can(familiar? -_-!). 是的,我们可以(熟悉吗?-_-!)。 There is an option of network mode presented by Docker, called service:service-name . Docker提供了一种网络模式选项,称为service:service-name

When we execute docker run , we could add --network=service:service-name flag. 当我们执行--network=service:service-name docker run ,我们可以添加--network=service:service-name标志。 It means that current container uses the same network namespace of service:service-name . 这意味着当前容器使用service:service-name的相同网络命名空间。 More information reference here. 更多信息请参考这里。

Try the following compose file below. 请尝试下面的以下撰写文件。 I've tested it, which works well. 我已经测试过了,效果很好。

version: '2'
services:
  web:
    image: nginx
    networks:
      public:
        ipv4_address: 10.0.0.2
    ports:
      - "8880:80"
      - "2220:22"

  ssh:
    image: panubo/sshd
    network_mode: "service:web"
    depends_on:
      - web
networks:
  public:
    external: true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM