这里是github repo:https://github.com/irahulsah/mutlicontainerapp请访问它以获取更多信息,请帮助我修复此错误。
我在本地运行一个带有docker compose的多docker容器,容器是React前端的"客户端"、Nodejs应用程序的"api"和位于两者前面的Nginx代理。我已经使用docker撰写设置如下一段时间了。
version: '3'
services:
client:
build:
dockerfile: Dockerfile.dev
context: ./client
volumes:
- /usr/app/node_modules
- ./client:/usr/app
api:
build:
dockerfile: Dockerfile.dev
context: ./server
volumes:
- /usr/app/node_modules
- ./server:/usr/app
nginx:
restart: always
build:
dockerfile: Dockerfile.dev
context: ./nginx
ports:
- '8080:80'
我的Nginx设置如下
upstream client {
server client:3000;
}
upstream api {
server api:5000;
}
server {
listen 80;
server_name _;
location / {
if ($http_x_forwarded_proto != 'https') {
return 301 https://$host$request_uri;
}
proxy_pass http://client;
}
location /api {
if ($http_x_forwarded_proto != 'https') {
return 301 https://$host$request_uri;
}
rewrite /api/(.*) /$1 break;
proxy_pass http://api;
}
}
最近,当我试图启动容器时,我得到了以下错误:
nginx_1 | 2019/08/08 18:11:12 [emerg] 1#1: host not found in upstream "client:3000" in /etc/nginx/conf.d/default.conf:2
nginx_1 | nginx: [emerg] host not found in upstream "client:3000" in /etc/nginx/conf.d/default.conf:2
知道为什么nginx找不到上游吗?
我已经尝试添加链接到nginx设置块如下:
nginx:
restart: always
build:
dockerfile: Dockerfile.dev
context: ./nginx
links:
- client:client
- api:api
ports:
- '8080:80'
我也尝试过"dependens_on",但也正在获得主机可以在客户端上找到:300错误,任何关于如何修复此错误的想法,都将不胜感激。
非常感谢任何帮助或指导!!!
我今天遇到了完全相同的问题。我通过在同一个docker虚拟网络中附加nginx上游引用的所有容器来解决这个问题。
此外,请确保明确定义容器名称。如果我没有错的话,docker-compose会在您的服务名称前面加上yaml文件名。
service_one:
networks:
- app-network # define the network
container_name: backend # the "backend" name must be used in the upstream directive in nginx configuration