Unable to connect to Dockerized Postgres

1. Caddy version (latest):

2. How I run Caddy:

a. System environment:

docker compose

b. Command:

docker-compose up

c. Service/unit/compose file:

version: "3.8"
services:
  caddy:
    image: caddy:latest
    restart: unless-stopped
    ports:
      - 80:80
      - 443:443
      - 8080:8080
      - 5432:5432
    volumes:
      - $PWD/Caddyfile.local:/etc/caddy/Caddyfile
      - $PWD/site:/srv
      - caddy_data:/data
      - caddy_config:/config
    depends_on:
      - postgres
      - hasura

  postgres:
    image: postgres:12
    expose:
      - 5432
    restart: unless-stopped
    command:
      ["postgres", "-c", "log_connections=on", "-c", "log_disconnections=on"]
    volumes:
      - skrrrt-db:/var/lib/postgresql/data
    env_file:
      - ./.env.local

  hasura:
    image: hasura/graphql-engine:v1.3.0
    expose:
      - 8080
    depends_on:
      - postgres
    restart: unless-stopped
    env_file:
      - ./.env.local

volumes:
  skrrrt-db:
  caddy_data:
  caddy_config:

d. My complete Caddyfile or JSON config:

localhost:5432 {
  reverse_proxy * postgres:5432
}

localhost:8080 {
  reverse_proxy * hasura:8080
}

3. The problem I’m having:

I’m flat out unable to connect to the database. If I remove Caddy from the equation I can connect to the database fine (i.e. change expose: 5432 to ports: 5432:5432 in the docker-compose).I need my server also running in docker to be able to connect to the database, and I would like to be able to connect myself (locally - in prod I will whitelist my ip)

4. Error messages and/or full log output:

This is the error thrown when my server tries to connect:

TypeOrmModule] Unable to connect to the database. Retrying (6)... +3012ms
Error: Connection terminated unexpectedly

and when I try to connect through a database client

server closed the connection unexpectedly
	This probably means the server terminated abnormally
	before or while processing the request.

5. What I already tried:

I’ve spent hours trying to find similar docker-compose setups that might shed light on where I’m going wrong. Then again, if my Postgres is running in a docker container with only ports 80,443,8080 exposed, do I need SSL on 5432? I was thinking it would be wise, especially if I want to allow certain IPs to connect to the database. I’m a bit out of element here so I’d appreciate any advice. I’d love for caddy to be embedded in my docker-compose setup.

Side question - when running caddy from docker, is it possible for caddy to redirect traffic on 80/443 to 3000? i.e. a react application running entirely independent of docker?

Any advice would be greatly appreciated!

:thinking: I don’t think postgres has an HTTP server, unless I’m mistaken. Caddy proxies HTTP, not TCP/UDP.

Yeah I’m not sure that postgres uses HTTP. So an HTTP proxy is the wrong tool for the job.

TCP/UDP can be proxied using the layer4 module: GitHub - mholt/caddy-l4: Layer 4 (TCP/UDP) app for Caddy

This topic was automatically closed after 30 days. New replies are no longer allowed.