Nginx proxy manager can't connect to docker containers (502 Bad gateway)

Hi,

I’m quite new to docker and i’m trying to setup a server hosting Nginx proxy manager and a few websites/services.
For example now running nextcloud and wordpress.
I want nginx to redirect cloud.domain.com to nextcloud and domain.com to wordpress. Done this in the past on other machines, but docker is’t working as expected or i’m missing something.
Right now nextcloud is available on the public ip on port 8081 and wordpress on port 2080.
I would expect that if i set the destination to localhost:8081 or localhost:2080 that would do the trick.
As far as i’ve come now, the result is Nginx trying 8081 and 2080 on it’s own container and not the host.
I’ve tried to forward to the hostname: nextcloud:80 (nextcloud container is using port 80 internally) but that gives a bad gateway too.

I suspect i need to do a bit more of networking settings, but i’m a bit lost…

I put the docker compose of nginx and nextcloud down here.

version: '3'
services:
  app:
    image: 'jc21/nginx-proxy-manager:latest'
    ports:
      - '80:80'
      - '81:81'
      - '443:443'
    environment:
      DB_MYSQL_HOST: "db"
      DB_MYSQL_PORT: 3306
      DB_MYSQL_USER: "nginxpm"
      DB_MYSQL_PASSWORD: ***
      DB_MYSQL_NAME: "nginxpm"
    restart: unless-stopped
    volumes:
      - ./data:/data
      - ./letsencrypt:/etc/letsencrypt



  db:
    image: 'jc21/mariadb-aria:latest'
    environment:
      MYSQL_ROOT_PASSWORD: ***
      MYSQL_DATABASE: 'nginxpm'
      MYSQL_USER: 'nginxpm'
      MYSQL_PASSWORD: ***
    restart: unless-stopped
    volumes:
      - ./mysql:/var/lib/mysql
version: '3'

services:
  nextcloud:
    image: nextcloud
    container_name: nextcloud
    restart: unless-stopped
    networks:
      - cloud
    depends_on:
      - nextclouddb
      - redis
    ports:
      - 8081:80
    volumes:
      - ./html:/var/www/html
      - ./custom_apps:/var/www/html/custom_apps
      - ./config:/var/www/html/config
      - ./data:/var/www/html/data
    environment:
      - PUID=1000
      - PGID=1000
      - TZ=Europe/Amsterdam
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud
      - MYSQL_PASSWORD=***
      - MYSQL_HOST=nextclouddb
      - REDIS_HOST=redis

  nextclouddb:
    image: mariadb
    container_name: nextcloud-db
    restart: unless-stopped
    command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW
    networks:
      - cloud
    volumes:
      - ./nextclouddb:/var/lib/mysql
    environment:
      - PUID=1000
      - PGID=1000
      - TZ=America/Los_Angeles
      - MYSQL_RANDOM_ROOT_PASSWORD=true
      - MYSQL_PASSWORD=***
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud

  collabora:
    image: collabora/code
    container_name: collabora
    restart: unless-stopped
    networks:
      - cloud
    environment:
      - PUID=1000
      - PGID=1000
      - TZ=Europe/Amsterdam
      - password=***
      - username=nextcloud
      - domain=all-about.nl
      - extra_params=--o:ssl.enable=true
    ports:
      - 9980:9980

  redis:
    image: redis:alpine
    container_name: redis
    restart: unless-stopped
    volumes:
      - ./redis:/data
    networks:
      - cloud

networks:
  cloud:
    name: cloud
    driver: bridge

Containers are about isolation. If you want to connect multiple, use a Docker network and attach the containers to it.

Can you help me a bit more?

I’ve edited the docker-compose of my nginx, but now when i try to login, nginx webui gives me a bad gateway error.

Edit, little update: I’ve fixed the bad gateway error in the webui. Tuns out that the nginx database needs to be in the same network as well.
Only issue i still have is nginx forward doesn’t work with hostname:port. If i use docker inspect network to find the containers ip-adress in the network and use ip:port the nginx forward works as it should.
Question remains, why doesn’t it work with the hostname?

version: '3'
services:
  app:
    image: 'jc21/nginx-proxy-manager:latest'
    ports:
      - '80:80'
      - '81:81'
      - '443:443'
    environment:
      DB_MYSQL_HOST: "db"
      DB_MYSQL_PORT: 3306
      DB_MYSQL_USER: "nginxpm"
      DB_MYSQL_PASSWORD: ***
      DB_MYSQL_NAME: "nginxpm"
    restart: unless-stopped
    volumes:
      - ./data:/data
      - ./letsencrypt:/etc/letsencrypt
    networks:
      - cloud


  db:
    image: 'jc21/mariadb-aria:latest'
    environment:
      MYSQL_ROOT_PASSWORD: ***
      MYSQL_DATABASE: 'nginxpm'
      MYSQL_USER: 'nginxpm'
      MYSQL_PASSWORD: ***
    restart: unless-stopped
    volumes:
      - ./mysql:/var/lib/mysql

networks:
  cloud:
    name: cloud
    driver: bridge

What do you consider a hostname? localhost inside a container is not localhost on host.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.