2

I am attempting to use an NGINX container to host a static web application. This container should also redirect certain requests (i.e. www.example.com/api/) to another container on the same network.

I am getting the "host not found in upstream" issue when calling docker-compose build, even though I am enforcing that the NGINX container is the last to be built.

I have tried the following solutions:

I am running on a Docker for Windows machine that is using a mobylinux VM to run the relevant container(s). Is there something I am missing? It isn't obvious to me that the "http://webapi" address should resolve correctly, as the images are built but not running when you are calling 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;
        }
    }
}

docker-compose:

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
José Maia
  • 310
  • 5
  • 21

1 Answers1

2

You issue is below

RUN service nginx start

You never run service command inside docker, because there is no init system. Also RUN commands are run during build time.

nginx original image has everything you need for nginx to start fine. So just remove the line and it would work.

By default nginx image has below CMD instruction

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

You can easily find that out by running the below command

docker history --no-trunc nginx | grep CMD
Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265