Docker和NGINX - 使用docker-compose

时间:2017-08-28 16:44:08

标签: docker nginx docker-compose

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

我得到了#34;主机未在上游发现"调用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

1 个答案:

答案 0 :(得分:2)

您的问题在

之下
RUN service nginx start

你永远不会在docker中运行service命令,因为没有init系统。此外,RUN命令在构建期间运行。

nginx原始图片包含nginx启动正常所需的一切。所以只需删除该行即可。

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

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

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

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