【问题标题】:nginx: Docker container fails to startnginx:Docker 容器无法启动
【发布时间】:2020-04-17 02:31:58
【问题描述】:

在尝试将应用程序 Docker 化到四个容器中时,Nginx 容器因错误而失败。特别是当我运行docker-compose up 时,它会显示:

nginx_1   | 2019/12/25 23:00:50 [emerg] 1#1: host not found in upstream "client:8080" in /etc/nginx/conf.d/default.conf:2
nginx_1   | nginx: [emerg] host not found in upstream "client:8080" in /etc/nginx/conf.d/default.conf:2

docker-compose.yml 和 nginx 配置以及 [ETA] nginx 容器的 Dockerfile.dev 包括在下面。我尝试过的事情:

  • docker-compose.yml中,将nginx容器设置为
depends_on: 
  - client
  • default.conf 中,将resolver 127.0.0.11; 添加到服务器块的location / 部分

我对 nginx 和 Docker 有点陌生。因此,我将不胜感激任何帮助或见解。


docker-compose.yml

version: '3'
services:
  db:
    image: 'postgres:latest'
  nginx:
    build:
      dockerfile: Dockerfile.dev
      context: ../nginx-glen
    restart: always
    ports:
      - '3050:80'
  api:
    build:
      dockerfile: Dockerfile.dev
      context: ../cornish-glen
    volumes:
      - /app/node_modules
      - ../cornish-glen:/app
    depends_on:
      - db
    environment:
      - PGUSER=postgres
      - PGHOST=postgres
      - PGDATABASE=turnip_glen
      - PGPASSWORD=********
      - PGPORT=5432
  client:
    build:
      dockerfile: Dockerfile.dev
      context: .
    depends_on:
      - api
    volumes:
      - /app/node_modules
      - .:/app
    environment:
      - GRAPHQL_ORIGIN=http://api:3099

default.conf

upstream client {
  server client:8080;
}

upstream api {
  server api:3099;
}

server {
  listen 80;

  location / {
    proxy_pass http://client;
  }

  location /sockjs-node {
    proxy_pass http://client;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
  }

  location /graphql {
    proxy_pass http://api;
  }
}

预计到达时间 nginx-glen/Dockerfile.dev

FROM nginx
COPY ./default.conf /etc/nginx/conf.d/default.conf

【问题讨论】:

  • ../nginx-glen/Dockerfile.dev 中有什么内容?
  • 您可以尝试为客户端和api服务添加端口映射。还要检查您是否能够从 nginx 容器中获取客户端:8080 以进一步调试。
  • @DavidMaze 添加到底部。
  • @asolanki 我按照你的建议添加了端口映射(例如ports:\n - '8080:8080')。在 nginx 容器中遇到了同样的错误。然后我尝试了wget 无济于事;请参阅此处复制的输出。 $ docker exec -it turnip-glen_nginx_1 bashroot@7a758732fce0:/# wget client:8080--2019-12-27 19:44:56-- http://client:8080/Resolving client (client)... 172.22.0.4Connecting to client (client)|172.22.0.4|:8080... failed: Connection refused.
  • 这可能是 webpack 的问题。 webpack 开发服务器在localhost:8080 上提供服务。我认为它需要在 nginx 为client 设置的 IP 地址上提供服务。所以我可能不得不重写这个问题。

标签: docker nginx webpack docker-compose webpack-dev-server


【解决方案1】:

答案在于webpack开发服务器,需要通过以下方式来指示它对主机的灵活处理:

// Only showing the devServer object here:
  devServer: {
    // These two lines are the relevant ones:
    // ---
    host: '0.0.0.0',
    disableHostCheck: true, // TODO: insecure, but probably fine for dev environment
    // ---
    contentBase: "./public",
    index: ''
  }

我还要指出,Docker 日志中 nginx 容器的 [emerg] 消息似乎并没有阻止应用程序运行。

【讨论】:

    猜你喜欢
    • 2017-02-04
    • 2021-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-24
    • 2018-06-24
    相关资源
    最近更新 更多