0

I have an existing Django app where Django runs in a Docker container (served through uvicorn), and Postgres runs in another Docker container.

In my settings.py I have

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": <name>,
        "USER": <user>,
        "PASSWORD": <pw>,
        "HOST": "db",
        "PORT": "5432",
   }
}

For postgres I just have FROM postgres:12.4-alpine in the Dockerfile. Then my docker-compose.yml is

services:
  db:
    image: <postgres>
    ...
  site:
    image: <django>
    ...
    depends_on:
      - db

This works and I'm trying to add PgBouncer on top of this, where PgBouncer runs in another container, but all the tutorials I find assume you're doing this through Heroku. Either the site can't connect to the DB, or the site connects directly to the database and skips the PgBouncer. What I have right now is

PgBouncer Dockerfile

FROM edoburu/pgbouncer:1.12.0
COPY pgbouncer.ini userlist.txt /etc/pgbouncer/
EXPOSE 6432

pgbouncer.ini

[databases]
<name> = host=db port=5432 dbname=<name>

[pgbouncer]
listen_addr = *
listen_post = 6432
auth_type = md5
auth_file = /etc/pgbouncer/userlist.txt
unix_socker_dir = /var/run/postgresql
pool_mode = transaction
max_client_conn = 100
default_pool_size = 20

settings.py

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": "<name>",
        "USER": "<user>",
        "PASSWORD": "<pw>",
        "HOST": "db",
        'PORT': '', # https://stackoverflow.com/questions/27418264/ideal-settings-for-pgbouncer-with-djangos-conn-max-age suggests this should be empty string instead of 6432?
    }
}

docker-compose

services:
  db:
    image: <postgres>
    ...
  pgbouncer:
    image: <pgbouncer>
    depends_on:
    - db
  site:
    image: <django>
    depends_on:
    - db
    - pgbouncer
Titandrake
  • 101
  • 1

0 Answers0