Connection refused while connecting to upstream

I’m having trouble getting a docker-compose/nginx reverse proxy app going. Here’s the setup:

docker-compose.yaml

version: '3'
services:
  webserver1:
    image: nginx
    ports:
      - '8080:80'
    volumes:
      - ./html:/usr/share/nginx/html
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf

  grafana:
    image: grafana/grafana-enterprise
    container_name: grafana
    environment:
      - GF_SERVER_DOMAIN=https://localhost/grafana/
      - GF_SERVER_ROOT_URL=https://localhost/grafana/
    restart: unless-stopped
    ports:
      - '3000:3000'
    volumes:
      - ./grafana/custom.ini:/etc/grafana/custom.ini

nginx.conf

# This is required to proxy Grafana Live WebSocket connections.

events{
}

http {
  map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
  }

  upstream grafana {
    server localhost:3000;
  }

  server {
    listen 80;
    #root /usr/share/nginx/html;
    #index index.html index.htm;

    location / {
      proxy_set_header Host $host;
      proxy_pass http://grafana;
    }

    # Proxy Grafana Live WebSocket connections.
    location /api/live/ {
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection $connection_upgrade;
      proxy_set_header Host $host;
      proxy_pass http://grafana;
    }
  }
}

custom.ini

[server]
protocol = http
http_addr = localhost
http_port = 3000
domain = localhost
root_url = %(protocol)s://%(http_addr)s:%(http_port)s/

Also, when I curl to 8080, I get a 502 error. I get the following error message in the docker compose logs:

webserver1_1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
webserver1_1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
webserver1_1  | /docker-entrypoint.sh: Configuration complete; ready for start up
webserver1_1  | 2025/08/14 02:25:11 [error] 29#29: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.22.0.1, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:3000/", host: "localhost:8080"

Please advise.

welcome to forum @jhturner900

Were you using the following as the basis for your setup?

Use dependency in your dockerfile, so Grafana container runs before nginx container. Be familoar with docker networking - by default each container has own network namespace = own localhost. So 127.0.0.1:3000 in nginx container is not 127.0.0.1:3000 im grafana container.

yes, I was.

check this repo out