【问题标题】:How to expose services between containers with docker-compose如何使用 docker-compose 在容器之间公开服务
【发布时间】:2019-09-04 09:01:24
【问题描述】:

在 circleci 上,当我为一项工作声明多个 docker 时:

dockers:
   app: company/image
   selenium: selenium/image

app 将公开端口 4000,而 selenium 将公开端口 4444。 然后从 app 容器中,我可以通过 localhost:4444 访问 selenium 服务,在 selenium 容器上,我可以通过 localhost:4000 访问 app webserver。

docker-compose,然而,行为不同。我只允许我从 app 访问 selenium:4444,从 selenium 访问 app:4000。

我希望 docker-compose 的行为类似于 circleci,它允许我使用 localhost:port 访问其他服务。我该怎么做?

【问题讨论】:

  • docs.docker.com/compose ?您可以使用expose 将端口暴露给其他服务,而不是主机;使用ports 向主机公开端口。 It's all in the docs 并且有很多指南。
  • 是的,docker-compose 端口设置允许通过“container:port”而不是“localhost:port”从一个容器访问另一个容器。

标签: docker-compose circleci


【解决方案1】:

上面的实现方式是通过networking_mode:

我需要告诉 docker-compose 使用 networking_mode = "services:app" 运行 selenium,以便 selenium 监听的每个端口都可以使用 localhost:PORT 从应用程序访问(反之亦然)

这里有解释:Can docker-compose share an ip between services with discrete ports?

这里的 docker 网络模型解释了它工作的原因:https://codability.in/docker-networking-explained/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-10
    • 1970-01-01
    • 2022-11-13
    • 2020-10-10
    • 1970-01-01
    相关资源
    最近更新 更多