4

I'm trying to get docker-compose to run an NGINX reverse-proxy and I'm running into an issue. I know that what I am attempting appears possible as it is outlined here:

https://dev.to/domysee/setting-up-a-reverse-proxy-with-nginx-and-docker-compose-29jg

and here:

https://www.digitalocean.com/community/tutorials/how-to-secure-a-containerized-node-js-application-with-nginx-let-s-encrypt-and-docker-compose#step-2-%E2%80%94-defining-the-web-server-configuration

My application is very simple - it has a front end and a back end (nextjs and nodejs), which I've put in docker-compose along with an nginx instance.

Here is the docker-compose file:

version: '3'

services:
  nodejs:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8000:8000"
    container_name: nodejs
    restart: unless-stopped
  nextjs:
    build:
      context: ../.
      dockerfile: Dockerfile
    ports:
      - "3000:3000"
    container_name: nextjs
    restart: unless-stopped
  webserver:
    image: nginx:mainline-alpine
    container_name: webserver
    restart: unless-stopped
    ports:
      - "80:80"
    volumes:
      - web-root:/var/www/html
      - ./nginx-conf:/etc/nginx/conf.d
    depends_on:
      - nodejs
      - nextjs
    networks:
      - app-network


volumes:
  certbot-etc:
  certbot-var:
  web-root:
    driver: local
    driver_opts:
      type: none
      device: /
      o: bind

networks:
  app-network:
    driver: bridge  

And here is the nginx file:

server {
    listen 80;
    listen [::]:80;

    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;

    server_name patientplatypus.com www.patientplatypus.com localhost;

    location /back {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;

      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";

      proxy_pass http://nodejs:8000;
    }

    location / {
      proxy_pass http://nextjs:3000;
    }

    location ~ /.well-known/acme-challenge {
      allow all;
      root /var/www/html;
    }
}

Both of these are very similar to the digitalOcean example and I can't think of how they would be different enough to cause errors. I run it with a simple docker-compose up -d --build.

When I go to localhost:80 I get page could not be found, and here is the result of my docker logs -

patientplatypus:~/Documents/patientplatypus.com/forum/back:10:03:32$docker ps
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS                          PORTS                    NAMES
9c2e4e25e6d9        nginx:mainline-alpine   "nginx -g 'daemon of…"   2 minutes ago       Restarting (1) 14 seconds ago                            webserver
213e73495381        back_nodejs             "/docker-entrypoint.…"   2 minutes ago       Up 2 minutes                    0.0.0.0:8000->8000/tcp   nodejs
03b6ae8f0ad4        back_nextjs             "npm start"              2 minutes ago       Up 2 minutes                    0.0.0.0:3000->3000/tcp   nextjs
patientplatypus:~/Documents/patientplatypus.com/forum/back:10:05:41$docker logs 9c2e4e25e6d9
2019/04/10 15:03:32 [emerg] 1#1: host not found in upstream "nodejs" in /etc/nginx/conf.d/nginx.conf:20

I'm pretty lost as to what could be going wrong. If anyone has any ideas please let me know. Thank you.

EDIT: SEE SOLUTION BELOW

Peter Weyand
  • 2,159
  • 9
  • 40
  • 72

1 Answers1

8

The nginx webserver is on the network app-network which is a different network than the other two services which don't have a network defined. When no network is defined docker-compose will create a default network for them to share.

Either copy the network setting to both of the other services or remove the network setting from the webserver service.

Shawn C.
  • 6,446
  • 3
  • 34
  • 38