docker合成和其他图像之间的通信

我有这样的架构: – 在主机端口80上运行nginx的docker组件 – 有2个服务的app:一个节点和一个mongodb

docker合成文件:

version: '2' services: backend: build: ./back-end/ container_name: "app-back-end" volumes: - ./back-end/:/usr/src/dance-app-back - /usr/src/app-back/node_modules ports: - "3000:3050" links: - mongodb mongodb: image: mongo:3.2.15 ports: - "3100:27017" volumes: - ./data/mongodb:/data/db 

nginxconfiguration文件

 server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location /back_1 { #proxy_pass http://172.17.0.2:5050/; proxy_pass http://0.0.0.0:5050/; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } 

nginx容器似乎无法到达主机上的端口3000。 我在做什么错了?

如果您的主机上有端口3000,并且想要将其映射到您的容器端口,则需要在您的docker-compose文件中执行此操作

更改:

 backend: build: ./back-end/ container_name: "app-back-end" volumes: - ./back-end/:/usr/src/dance-app-back - /usr/src/app-back/node_modules ports: - "3000:3000" links: - mongodb 

然后,您可以将nginx链接到您想要代理传递给您的容器。

添加到您的撰写文件:

  nginx: restart: always image: nginx:1.13.1 ports: - "80:80" depends_on: - backend links: - backend:backend 

然后在你的nginx文件中提到你想要代理的应用程序容器的名称和打开的那个容器的端口。

 server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location /back_1 { proxy_pass http://backend:3000/; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } 

您可以将容器名称和nginxconfiguration链接起来,而不是将您的nginx中的容器的IP链接起来,而docker将为您parsing这些IP。