【问题标题】:Docker and NGINX - host not found in upstream when building with docker-composeDocker 和 NGINX - 使用 docker-compose 构建时在上游找不到主机
【发布时间】:2018-02-06 00:06:34
【问题描述】:

我正在尝试使用 NGINX 容器来托管静态 Web 应用程序。此容器还应将某些请求(即 www.example.com/api/)重定向到同一网络上的另一个容器。

我在调用 docker-compose build 时遇到了“在上游找不到主机”的问题,尽管我强制要求 NGINX 容器是最后一个构建的。

我尝试了以下解决方案:

我正在使用 mobylinux VM 运行相关容器的 Docker for Windows 机器上运行。有什么我想念的吗?对我来说,“http://webapi”地址应该正确解析并不明显,因为当您调用 docker-compose 时图像已构建但未运行。

nginx.conf:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;


    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    upstream docker-webapi {
        server webapi:80;
    }

    server {
        listen       80;
        server_name  localhost;

        location / {
            root /wwwroot/;
            try_files $uri $uri/ /index.html;
        }

        location /api {
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_pass http://docker-webapi;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
        }

        error_page   500 502 503 504  /50x.html;
            location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }
}

码头工人撰写:

version: '3'

services:
  webapi:
    image: webapi
    build:
      context: ./src/Api/WebApi
      dockerfile: Dockerfile    
    volumes:
        - /etc/example/secrets/:/app/secrets/
    ports:
      - "61219:80"

  model.api:
    image: model.api
    build:
      context: ./src/Services/Model/Model.API
      dockerfile: Dockerfile
    volumes:
        - /etc/example/secrets/:/app/secrets/
    ports:
      - "61218:80"

  webapp:
    image: webapp
    build:
      context: ./src/Web/WebApp/
      dockerfile: Dockerfile
    ports:
      - "80:80"
    depends_on:
        - webapi

Dockerfile:

FROM nginx

RUN mkdir /wwwroot
COPY nginx.conf /etc/nginx/nginx.conf
COPY wwwroot ./wwwroot/
EXPOSE 80
RUN service nginx start

【问题讨论】:

  • 您有nginx -t 来验证您构建中的配置吗?如果是这样,你不应该拥有它。这些只有在环境启动时才有用
  • 你真的在构建时得到那个错误吗????
  • @TarunLalwani 不,没有明确说明。我在帖子中添加了 Dockerfile。
  • @whites11 是的,在调用 docker-compose build 或 docker-compose up --build 命令后,会立即构建前两个镜像,而 webapp 镜像在最后一步失败。
  • 你能发布docker-compose build的输出吗?

标签: docker nginx docker-compose


【解决方案1】:

你的问题在下面

RUN service nginx start

你永远不会在 docker 中运行service 命令,因为没有初始化系统。 RUN 命令也在构建期间运行。

nginx original image 包含了 nginx 正常启动所需的一切。因此,只需删除该行,它就会起作用。

默认情况下,nginx 镜像下面有CMD 指令

CMD ["nginx" "-g" "daemon off;"]

您可以通过运行以下命令轻松找到它

docker history --no-trunc nginx | grep CMD

【讨论】:

    猜你喜欢
    • 2019-09-01
    • 2020-01-27
    • 2018-12-28
    • 2021-02-26
    • 2020-07-08
    • 2018-03-03
    • 2020-06-15
    • 1970-01-01
    • 2016-02-11
    相关资源
    最近更新 更多