是否可以将docker集装箱A上的端口XXXX绑定到docker集装箱B上的端口XXXX或YYYY

假设我有一个docker-compose文件,如下所示:

version: '2' services: # The php-fpm container app: build: context: ./ dockerfile: app.dev.dockerfile working_dir: /var/www volumes: - ./:/var/www expose: - 8003 # The Web Server web: build: context: ./ dockerfile: web.dockerfile working_dir: /var/www volumes_from: - app links: - app:app ports: - 80:80 - 8004:8003 

所以我喜欢从Web容器中的端口8004在App容器上提供端口8003。 但是我无法得到结果。 所以我的猜测是Web容器上的8004:8003的Web端口映射仅限于Web容器。 或者它实际上映射到应用程序容器上的公开的8003端口?

如果是这样,我怎么testingWeb端口8004实际上是映射到应用程序容器上的端口8003?

有没有办法让我创build一个单向绑定(或甚至更好的双向绑定)或应用程序容器上的端口8003映射到Web容器上的端口8004?

如果我的主机正在运行一些应用程序和泊坞窗服务(包括这两个容器),并且应用程序试图在Web容器中侦听端口8004,他实际上正在与App容器进行通信。

容器旨在保持每个过程独立。 每个进程/容器都有自己独立的networking堆栈

如果存在用于转发请求的用户空间进程,则从一个容器到另一个容器所描述的“映射”的唯一方式就是实现。 像Apache http或Nginx一样将HTTP请求转发到FastCGI PHP进程。

对于debugging,将端口直接映射到app容器。 如果无法连接, app容器中的debugging设置可能有问题。

  # The php-fpm container app: build: context: ./ dockerfile: app.dev.dockerfile working_dir: /var/www volumes: - './:/var/www' ports: - '8003:8003' # The Web Server web: build: context: ./ dockerfile: web.dockerfile working_dir: /var/www volumes_from: - app ports: - 80:80 

顺便说一下,版本2+连接不需要links 。 撰写将默认创build一个用户定义的networking,允许您的容器之间访问。