docker-compose 与其他图像之间的通信
Communication between docker-compose and other images
我有这样的架构:
- 1 docker 组件 运行 nginx 在主机的 80 端口
- 具有 2 项服务的应用程序:一个节点和一个 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
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://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
然后你可以 link 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;
}
}
您可以 link 容器名称和 nginx 配置,而不是 nginx 中容器的 IP,docker 将为您解析这些 IP。
我有这样的架构: - 1 docker 组件 运行 nginx 在主机的 80 端口 - 具有 2 项服务的应用程序:一个节点和一个 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
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://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
然后你可以 link 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;
}
}
您可以 link 容器名称和 nginx 配置,而不是 nginx 中容器的 IP,docker 将为您解析这些 IP。