docker - Nginx 在多容器 docker compose 设置中找不到上游主机,并且在客户端上找不到主机 :3000

标签 docker docker-compose dockerfile

这是 github 存储库:https://github.com/irahulsah/mutlicontainerapp 请访问它以获取更多信息,请帮助我修复此错误。

我正在使用 docker-compose 在本地运行一个多 Docker 容器,这些容器是 React 前端“客户端”、Nodejs 应用程序“api”,以及位于两个容器前面的 Nginx 代理。我已经使用 docker-compose 设置一段时间了。

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 设置 block ,如下所示:

  nginx:
    restart: always
    build:
      dockerfile: Dockerfile.dev
      context: ./nginx
    links:
      - client:client
      - api:api
    ports:
      - '8080:80'

我也尝试过“depends_on”,但也收到了“host can be find on client:300”错误,任何有关如何修复此错误的想法,将不胜感激。

非常感谢任何帮助或指导!!!

最佳答案

我今天遇到了完全相同的问题。我通过将 nginx 上游引用的所有容器附加到同一个 docker 虚拟网络中来解决这个问题。

此外,请确保显式定义容器名称。如果我没记错的话,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

关于docker - Nginx 在多容器 docker compose 设置中找不到上游主机,并且在客户端上找不到主机 :3000,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64112684/

相关文章:

angular - Google Cloud Build - firebase 部署错误 : "public directory ' dist/browser' does not exist, 无法将托管部署到站点 PROJECT-ID”

docker - 我应该将哪个 docker 容器用于 Hadoop?

docker - 找不到 Tensorboard 站点

docker - 合并两个docker run命令

haskell - yesod二进制文件应在哪里找到config文件夹?

docker - 如何从另一个 Dockerfile 生成 Dockerfile?

docker - Traefik可以与docker-compose连接,但不能与Windows上的swarm服务连接

docker - Docker-compose扩展找不到环境变量

docker - 多个 Dockerfile 在项目中重用共享库

visual-studio-code - 在 Visual Studio Code 中禁用 Dockerfile POP-UP 帮助