2017-08-28 59 views
1

我尝试使用NGINX容器来托管静态Web应用程序。此容器还应将某些请求(即www.example.com/api/)重定向到同一网络上的另一个容器。Docker和NGINX - 在Docker构建时未在上游找到主机

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

我曾尝试以下解决方案:

我在使用mobylinux虚拟机运行相关容器的Windows机器的Docker上运行。有什么我失踪?我不清楚“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 
+0

你有一个'nginx的-t'来验证配置你的构建?如果是这样,你不应该拥有它。这些只会在环境启动时才有用 –

+0

您是否真的在编译时遇到错误? – whites11

+0

@TarunLalwani不明确。我将Dockerfile添加到了帖子中。 –

回答

2

您的问题是低于

RUN service nginx start 

因为没有init系统,所以在docker中你永远不会运行service命令。还有RUN命令在编译期间运行。

nginx原始图像包含了您需要的所有内容,以便nginx能够正常运行。所以,只要删除线,它会工作。

默认情况下nginx的图像低于CMD指令

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

你可以很容易地找到了通过运行以下命令

docker history --no-trunc nginx | grep CMD 
相关问题