0
votes

I'm trying to follow this guide to setting up a reverse proxy for a docker container (serving a static file), using another container with an instance of nginx as a reverse proxy.

I expect to see my page served on /, but I am blocked in the build with the error message:

container_nginx_1  | 2020/05/10 16:54:12 [emerg] 1#1: host not found in upstream "container1:8001" in /etc/nginx/conf.d/sites-enabled/virtual.conf:2
container_nginx_1  | nginx: [emerg] host not found in upstream "container1:8001" in /etc/nginx/conf.d/sites-enabled/virtual.conf:2
nginx_docker_test_container_nginx_1 exited with code 1

I have tried many variations on the following virtual.conf file, and this is the current, based on the example given and various other pages:

upstream cont {
    server container1:8001;
}

server {
    listen        80;

    location / {
        proxy_pass http://cont/;
    }
}

If you are willing to look at a 3rd party site, I've made a minimal repo here, otherwise the most relevant files are below.

My docker-compose file looks like this:

version: '3'

services:
    container1:
        hostname: container1
        restart: always
        image: danjellz/http-server
        ports:
            - "8001:8001"
        volumes:
            - ./proj1:/public
        command: "http-server . -p 8001"
        depends_on:
            - container_nginx
        networks:
            - app-network

    container_nginx:
        build:
            context: .
            dockerfile: docker/Dockerfile_nginx
        ports:
            - 8080:8080
        networks:
            - app-network

networks:
    app-network:
        driver: bridge  

and the Dockerfile

# docker/Dockerfile_nginx

FROM nginx:latest

# add nginx config files to sites-available & sites-enabled
RUN mkdir /etc/nginx/conf.d/sites-available
RUN mkdir /etc/nginx/conf.d/sites-enabled
ADD projnginx/conf.d/sites-available/virtual.conf /etc/nginx/conf.d/sites-available/virtual.conf
RUN cp /etc/nginx/conf.d/sites-available/virtual.conf /etc/nginx/conf.d/sites-enabled/virtual.conf

# Replace the standard nginx conf
RUN sed -i 's|include /etc/nginx/conf.d/\*.conf;|include /etc/nginx/conf.d/sites-enabled/*.conf;|' /etc/nginx/nginx.conf

WORKDIR /

I'm running this using docker-compose up.

Similar: react - docker host not found in upstream

1

1 Answers

1
votes

The problem is if the hostname can not be resolved in upstream blocks, nginx will not start. Here you have defined service container1 to be dependent on container_nginx . But nginx container is never up due to the fact the container1 hostname is not resolved (because container1 is not yet started) Don't you think it should be reverse? Nginx container should be dependent on the app container.

Additionally in your nginx port binding you have mapped 8080:8080 while in nginx conf you have 80 listening.