Search code examples
dockernginxreverse-proxypgadmin

Cannot reach dockerized pgAdmin via nginx


I am trying to dockerize a pern stack with docker and nginx, and I'm facing issues configuring nginx, even locally. I would like to be able to access my frontend container and my pgadmin container with one URL. So far I managed to access the front, but I cant access pgadmin (bad gateway). Here is my nginx.conf file :

    events {
  worker_connections 1024;
}

http {

  sendfile on;

  upstream app {
    server app:3000;
  }

  upstream pgadmin {
    server pgadmin:5050;
  }

  server {

    listen 80;

    location / {
      proxy_pass http://app;
    }

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


}

Here is my docker-compose file :

    version: "3.8"

services:
  db:
    container_name: db
    image: postgres:latest
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=db
    volumes:
      - ./api/data:/data/db
    ports:
      - "5432:5432"
    expose:
      - 5432
    networks:
      - db
    restart: unless-stopped
  pgadmin:
    container_name: pgadmin
    image: dpage/pgadmin4
    ports:
      - "5050:80"
    environment:
      - PGADMIN_DEFAULT_EMAIL=admin@local.com
      - PGADMIN_DEFAULT_PASSWORD=admin
    volumes:
      - ./pgadmin:/var/lib/pgadmin
    depends_on:
      - db
    networks:
      - db
      - pgadmin
    restart: unless-stopped
  api:
    tty: true
    container_name: api
    build:
      context: .
      dockerfile: ./api/Dockerfile.dev # on move to prod, update this
    volumes:
      - /api/node_modules
      - ./api:/api
    ports:
      - "3001:3001"
    depends_on:
      - db
    networks:
      - app
      - db
    restart: unless-stopped
  app:
    container_name: app
    build:
      context: .
      dockerfile: ./app/Dockerfile.dev # on move to prod, update this
    volumes:
      - /app/node_modules
      - ./app:/app
    ports:
      - "3000:3000"
    restart: always
    depends_on:
      - api
    networks:
      - app
  nginx:
    depends_on:
      - app
      - pgadmin
    restart: unless-stopped
    build:
      dockerfile: Dockerfile.dev
      context: ./nginx
    ports:
      - "3050:80"
    volumes:
      - ./nginx:/etc/nginx/conf.d
    networks:
      - app
      - pgadmin
networks:
  app:
  db:
  pgadmin:
    driver: bridge

Am I doing something wrong ?

Thanks


Solution

  • Port publishing only impacts access from outside of the container network. When you have a container access a service in another container, all that matters is the port the service is actually listening on.

    In this case, pgAdmin is listening on port 80, not on port 5050, so you need:

      upstream pgadmin {
        server pgadmin:80;
      }
    

    In fact, you probably want to remove the ports section for those services that are sitting "behind" your nginx proxy; there's no need to expose those directly.