Stephen Mayeux
Stephen Mayeux

Reputation: 76

Nginx container doesn't use nginx.conf unless I restart the container

As the title says, my nginx container is not working as expected unless I restart it. I have several services defined in a docker-compose.yml file that looks like this: reverseproxy is my nginx container, and service-a and service-b are Node.js servers.

version: "3.4"
services:
  reverseproxy:
    container_name: reverseproxy
    build:
      context: ./proxy
    ports:
      - "80:80"
  service-a:
    container_name: service-a
    build:
      context: ./service-a
    ports:
      - "3500:3500"
    command: ["yarn", "run", "watch-debug"]
  service-b:
    container_name: service-b
    build:
      context: ./service-b
    ports:
      - "3501:3501"
    command: ["yarn", "run", "watch-debug"]

The Dockerfile used to build my reverseproxy service simply removes the default.conf file and then copies the nginx.conf file from my host to the image:

FROM nginx:alpine
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/nginx.conf

And my nginx.conf file that gets copied into the image looks like this:

worker_processes 1;

events { worker_connections 1024; }

http {

    sendfile on;

    server {
        listen 80;

        location /api/customers {
            proxy_pass         http://service-a:3500;
            proxy_redirect     off;
            proxy_set_header   Host $http_host;
            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;
        }

        location /api/products {
            proxy_pass         http://service-b:3501;
            proxy_redirect     off;
            proxy_set_header   Host $http_host;
            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;
        }
    }
}

When I docker-compose up everything spins up fine, but when I POST to one of my endpoints (for example, localhost:80/api/customers) then nginx responds with a 502. But if I docker container stop reverseproxy and then docker container start reverseproxy, then everything works as expected and I'm able to hit my endpoints with localhost:80.

I was able to docker exec -it reverseproxy /bin/sh and was able to verify that default.conf is gone and nginx.conf was copied over from my host as expected. I have followed the sample configuration from the nginx page on Docker Hub and most tutorials online show a nearly identical set up.

What may be causing this? How could make my nginx revereproxy service work as expected without restarting the container?

Edit: I am using Postman to make my requests localhost:80

Upvotes: 2

Views: 996

Answers (1)

Stephen Mayeux
Stephen Mayeux

Reputation: 76

@DavidMaze had the correct solution -- thank you!

  reverseproxy:
    container_name: reverseproxy
    build:
      context: ./proxy
    ports:
      - "80:80"
    depends_on:
      - "service-a"
      - "service-b"

Makes sense that it only works as expected on a restarted because the other services were available by then. Just tried it out and it works as expected.

Upvotes: 1

Related Questions