Score:0

Access docker compose containers from host NGINX via reverse proxy

ng flag

I am facing this issues I am not able to solve by anything I´ve found on this site or anywhere else.

Lets assume I have a domain and Debian server with NGINX installed on it as main gateway for web server. On this domain (on root level) I have a traditional handling (like strippping www, redirecting everytime to https etc - basic stuff).

But now I´d like to have a set of services managed in docker-compose file that will be executed on docker start and these services will be mapped as subdomains to my main domain. Lets assume that domain has set up proper DNS records (these are not problem since I´ve been using subdomains for non docker services with no issues). For simplity take just one service- pgAdmin4- as example

Part of the NGINX configuration that solves this issue is pretty basic.

server {
    listen 80;
    server_name pg.example.com;

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

Docker compose is even more basic

version: '3.8'
services:
  pgadmin:
    container_name: pgadmin
    image: dpage/pgadmin4
    restart: always
    environment:
      PGADMIN_DEFAULT_EMAIL: [email protected]
      PGADMIN_DEFAULT_PASSWORD: pw
    ports:
      - "81:80"

When I try to sudo nginx -t to check the configuration, I am getting an error host not found in upstream "pgadmin"

When I try to replace name of container with host.docker.internal (with extra_hosts in compose set up like "host.docker.internal:host-gateway") or when I try to use 127.0.0.1:81 or simply localhost:81, I am getting (after restarting NGINX) 502 Bad Gateway whitelabel page error.

In logs I can see a lot of errors like

*2 no live upstreams while connecting to upstream, client: 78.136.141.1, server: pg.example.com, request: "GET / HTTP/1.1", upstream: "http://localhost/", host: "pg.example.com"

Can anyone tell me what am I doing wrong?
Score:0
cn flag

Move nginx into Docker (and expose 80/443) using say this container and then set env vars on the containers you want to expose that contain the hostname to map

e.g.

VIRTUAL_HOST=foo.example.com

There is even a companion container that will handle setting up letsencrypt certs on the fly

Johnczek avatar
ng flag
Thanks for answer. Situation is way more complicated and I need to have some host nginx on top and contacting docker containers if needed. There is really no way how to achieve what I need? As I see it, the only problem is how to contact docker container from host
cn flag
If you poke around that project it probably has all the parts you need, it will just be a bit of effort to get it to work, but you should still be able to host static stuff with the container
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.