docker-compose可以在具有离散端口的服务之间共享ip吗? [英] Can docker-compose share an ip between services with discrete ports?

查看:206
本文介绍了docker-compose可以在具有离散端口的服务之间共享ip吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们目前拥有使用supervisor进行复杂构建的docker容器,因此我们可以将服务分组在一起。例如,nginx和ssh。

We currently have docker containers with complex builds using supervisord so that we can group services together. For example, nginx and ssh.

我正在尝试通过共享卷链接的更多服务驱动的隔离来重建它们。但是,没有将IP映射到主机,即使端口可能是离散的,我似乎也找不到找到共享IP地址的方法。

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.

我要做的是这样的:

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

...其中 public 是预定义的docker macvlan网络。

...where public is a predefined docker macvlan network.

尝试此操作时,出现错误:

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.

是否可以将docker-compose / docker-networking配置为通过

推荐答案


是否可以将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提供了一个网络模式选项,称为 service:service-name

Yes we can(familiar? -_-!). There is an option of network mode presented by Docker, called service:service-name.

当我们执行 docker run 时,我们可以添加-network = service:service -name 标志。这意味着当前容器使用 service:service-name 的相同网络命名空间。 更多信息此处参考。

When we execute docker run, we could add --network=service:service-name flag. It means that current container uses the same network namespace of 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

这篇关于docker-compose可以在具有离散端口的服务之间共享ip吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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