pass_proxy not listening to containers using nginx in docker

November 23, 2019 96 views
Docker

I’m preparing a docker based dev environment for node and nginx but for some reason the nginx pass_proxy is not pointing to the container using their service name.

Here are the configuration files:

default.conf

server {
    listen 80;
    error_log /var/log/nginx/error.log;
    location /api {
        proxy_pass http://web:3000;
    }
}

nginx Dockerfile

FROM nginx:1.17-alpine
COPY ./default.conf /etc/nginx/conf.d/default.conf
CMD ["/bin/sh", "-c", "exec nginx -g 'daemon off;'"]

docker-compose.yml

version: '3.3'

services:
  web:
    build: ../backend
    container_name: backend
    restart: always
    volumes:
      - ../backend:/usr/src/app
      - /usr/src/app/node_modules
    ports:
      - 6060:3000
  server:
    build: .
    restart: always
    container_name: nginx_server
    volumes:
      - ./log-nginx:/var/log/nginx/
    depends_on:
      - web
    ports:
      - 6000:80

I have exposed the web as well and it works when i visit localhost:6060 but if i go to the nginx endpoint by going to this address localhost:6000/api it shows an error page in the browser:

This site can’t be reached

Note that in the default.conf file i have tried using different variations for the pass_proxycontainer name, for example:

  • proxy_pass http://web;
  • proxy_pass http://web:3000;
  • proxy_pass web:3000;
  • proxy_pass web;

but it seems like non of them are working for nginx to shine. The logs file of nginx are also empty! what I might be missing here? Any help would be really helpful :)
One more thing, I am using the alpine based official images.

1 Answer

Hello,

I think that you would need to create a Docker Network and attach the 2 containers to that Network. That way you would be able to connect from container 1 to container 2 by name.

You could take a look at this answer here on how to do that:

https://www.digitalocean.com/community/questions/how-to-ping-docker-container-from-another-container-by-name

Hope that this helps!
Regards,
Bobby

Have another answer? Share your knowledge.