Run pgbouncer using config from environment variables
1.5K
Also on docker hub https://hub.docker.com/repository/docker/donuk/pgbouncer/
A container for running PgBouncer, aimed at allowing full configuration via environment variables. This image wraps the official percona/percona-pgbouncer image and lets you set any [pgbouncer] option in pgbouncer.ini through environment variables.
[pgbouncer] option can be set by upper-casing its name and prefixing it with PGBOUNCER_. For example PGBOUNCER_POOL_MODE=transaction sets pool_mode = transaction.listen_addr and listen_port, defaulting to 0.0.0.0 and 6432. PGBOUNCER_LISTEN_ADDR / PGBOUNCER_LISTEN_PORT take precedence if both are set.pgbouncer.ini so these values match the environment. Environment variables win over any existing definition of the same option in the file.Run the container as you would the original image, but you can now override the listening address and port entirely via environment variables:
docker run -e LISTEN_ADDR=0.0.0.0 -e LISTEN_PORT=6543 \
-v /your/pgbouncer.ini:/etc/pgbouncer/pgbouncer.ini \
-p 6543:6543 \
donuk/pgbouncer
LISTEN_ADDR (optional): The bind address for PgBouncer (listen_addr). Default: 0.0.0.0.LISTEN_PORT (optional): The port for PgBouncer (listen_port). Default: 6432.PGBOUNCER_<OPTION> (optional): Any other [pgbouncer] option, e.g. PGBOUNCER_MAX_CLIENT_CONN=500. See the PgBouncer config reference for the available options.The names PGBOUNCER_PORT, PGBOUNCER_PORT_*, PGBOUNCER_SERVICE_HOST and PGBOUNCER_SERVICE_PORT* are ignored, because Kubernetes injects them for a Service called pgbouncer. Use LISTEN_PORT or PGBOUNCER_LISTEN_PORT to set the port.
The upstream image's own AUTH_TYPE variable still works. PGBOUNCER_AUTH_TYPE overrides it if both are set.
PgBouncer verifies the backend's certificate against server_tls_ca_file, which defaults to the system CA bundle. That bundle does not include the RDS CAs, so to use verify-full against RDS you need to supply them. This image can build the CA bundle at startup from inline PEM and/or pinned downloads, so nothing has to be baked into the image or mounted.
| Variable | Meaning |
|---|---|
SERVER_TLS_CA_PEM | One or more PEM certificates, inline. A literal \n is accepted in place of newlines for env sources that can't hold them. |
SERVER_TLS_CA_URL | One or more whitespace-separated URLs to download PEM bundles from. |
SERVER_TLS_CA_SHA256 | Required with SERVER_TLS_CA_URL: the SHA-256 of each downloaded file, in the same order as the URLs. |
The sources are concatenated into /etc/pgbouncer/server-ca.pem, and server_tls_ca_file is pointed at it. Because this file replaces the system bundle, only the CAs you list are trusted for backend connections.
docker run \
-e SERVER_TLS_CA_URL=https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem \
-e SERVER_TLS_CA_SHA256=<sha256 of that file> \
-e PGBOUNCER_SERVER_TLS_SSLMODE=verify-full \
donuk/pgbouncer
Several sources can be combined:
-e SERVER_TLS_CA_URL="https://example.com/a.pem https://example.com/b.pem" \
-e SERVER_TLS_CA_SHA256="<sha256 of a.pem> <sha256 of b.pem>" \
-e SERVER_TLS_CA_PEM="$(cat my-private-ca.pem)"
Notes:
curl -s <url> | sha256sum and update SERVER_TLS_CA_SHA256 when you choose to accept a new bundle.PGBOUNCER_ prefix, so they aren't written into pgbouncer.ini. Setting PGBOUNCER_SERVER_TLS_CA_FILE together with them is an error.server_tls_sslmode isn't verify-ca or verify-full. Set it with PGBOUNCER_SERVER_TLS_SSLMODE.verify-full, the host in [databases] must match the certificate, so use the RDS endpoint name rather than an IP address or a custom CNAME.The certificate PgBouncer presents to clients, and the CAs it uses to verify client certificates, can also be supplied through the environment, so no files need to be mounted.
| Variable | Meaning |
|---|---|
CLIENT_TLS_CERT_PEM | PgBouncer's own certificate (client_tls_cert_file). May include intermediates after the leaf. |
CLIENT_TLS_KEY_PEM | Its private key (client_tls_key_file). Must be unencrypted and must match the certificate. |
CLIENT_TLS_CA_PEM / CLIENT_TLS_CA_URL / CLIENT_TLS_CA_SHA256 | CAs used to verify client certificates (client_tls_ca_file). Same format and rules as the SERVER_TLS_CA_* variables above. |
The certificate and key must be given together. Files are written to /etc/pgbouncer/ (the key with mode 600) and the matching client_tls_* options are set automatically. You still choose the mode with PGBOUNCER_CLIENT_TLS_SSLMODE.
TLS only, clients verify PgBouncer:
docker run \
-e "CLIENT_TLS_CERT_PEM=$(cat pgbouncer.crt)" \
-e "CLIENT_TLS_KEY_PEM=$(cat pgbouncer.key)" \
-e PGBOUNCER_CLIENT_TLS_SSLMODE=require \
donuk/pgbouncer
Mutual TLS, where clients must also present a certificate whose CN is their database user:
docker run \
-e "CLIENT_TLS_CERT_PEM=$(cat pgbouncer.crt)" \
-e "CLIENT_TLS_KEY_PEM=$(cat pgbouncer.key)" \
-e "CLIENT_TLS_CA_PEM=$(cat clients-ca.pem)" \
-e PGBOUNCER_CLIENT_TLS_SSLMODE=verify-full \
-e PGBOUNCER_AUTH_TYPE=cert \
-e AUTH_TYPE=plain \
donuk/pgbouncer
Here AUTH_TYPE=plain is the upstream image's setting for how PgBouncer stores the password it uses to log in to the backend, and PGBOUNCER_AUTH_TYPE=cert overrides how clients authenticate to PgBouncer.
Notes:
PGBOUNCER_CLIENT_TLS_CERT_FILE, PGBOUNCER_CLIENT_TLS_KEY_FILE or PGBOUNCER_CLIENT_TLS_CA_FILE together with the matching variable is an error.client_tls_sslmode doesn't require TLS, or if a client CA is installed but client_tls_sslmode isn't verify-ca or verify-full.docker inspect on the container, so prefer your orchestrator's secret handling for passing it.Postgres memory use is driven mainly by the number of server connections, so the useful options are those that cap or recycle them:
docker run \
-e PGBOUNCER_POOL_MODE=transaction \
-e PGBOUNCER_DEFAULT_POOL_SIZE=10 \
-e PGBOUNCER_MAX_DB_CONNECTIONS=20 \
-e PGBOUNCER_RESERVE_POOL_SIZE=0 \
-e PGBOUNCER_SERVER_IDLE_TIMEOUT=60 \
-e PGBOUNCER_SERVER_LIFETIME=1800 \
-e PGBOUNCER_QUERY_WAIT_TIMEOUT=30 \
donuk/pgbouncer
| Option | Effect |
|---|---|
PGBOUNCER_POOL_MODE | transaction lets many clients share a few server connections. |
PGBOUNCER_DEFAULT_POOL_SIZE | Server connections per user/database pair. |
PGBOUNCER_MAX_DB_CONNECTIONS | Hard cap on server connections per database. |
PGBOUNCER_MAX_USER_CONNECTIONS | Hard cap on server connections per user. |
PGBOUNCER_RESERVE_POOL_SIZE | Extra burst connections; 0 keeps memory predictable. |
PGBOUNCER_SERVER_IDLE_TIMEOUT | Close idle server connections sooner. |
PGBOUNCER_SERVER_LIFETIME | Recycle long-lived backends, whose caches grow over time. |
PGBOUNCER_MAX_PREPARED_STATEMENTS | Limits prepared statements tracked in transaction mode. |
PGBOUNCER_MAX_CLIENT_CONN | Limits clients connecting to PgBouncer itself. |
PGBOUNCER_QUERY_WAIT_TIMEOUT | Fail clients that wait too long for a connection instead of queueing forever. |
PGBOUNCER_QUERY_TIMEOUT / PGBOUNCER_IDLE_TRANSACTION_TIMEOUT | Stop long or stuck work from holding backends. |
Per-database settings such as pool_size live in the [databases] section, which is not touched by this image. Set them in your mounted pgbouncer.ini.
On startup /entrypoint-wrapper.sh runs the upstream entrypoint, then each executable /entrypoint.d/*.sh script in name order, and finally starts PgBouncer. The upstream entrypoint has to go first because it rewrites auth_type from AUTH_TYPE, which would discard PGBOUNCER_AUTH_TYPE. The built-in steps are:
| Script | What it does |
|---|---|
10-env-options.sh | Collects LISTEN_ADDR, LISTEN_PORT and every PGBOUNCER_<OPTION> variable. Option names are lower-cased. |
20-tls.sh | Builds the CA bundles and client certificate and key from the *_TLS_* variables, sets the matching *_tls_*_file options, and logs warnings about likely misconfiguration. |
90-apply-overrides.sh | Removes any existing definition of the collected options from the [pgbouncer] section of /etc/pgbouncer/pgbouncer.ini and inserts the new values directly under it, adding the section if it is missing. |
99-print-config.sh | Prints the resulting configuration, without blank lines and comments. |
A script that exits non-zero aborts startup.
Scripts configure PgBouncer by appending option=value lines to the file named by $ENTRYPOINT_OVERRIDES. If an option appears more than once, the last line wins, so a script numbered after 10-env-options.sh overrides the environment variables for the options it sets. The path of the configuration file is in $ENTRYPOINT_INI. Scripts must have the executable bit set; non-executable *.sh files are skipped with a notice.
To add a step, copy a script into /entrypoint.d/ in a derived image, choosing a number that puts it where you need it (anything below 90 runs before the overrides are applied):
FROM donuk/pgbouncer
COPY --chmod=755 15-my-step.sh /entrypoint.d/15-my-step.sh
ENTRYPOINT_* is used for these variables because PGBOUNCER_* is reserved for pgbouncer.ini options.
All other configuration remains unchanged, so you can continue to mount your own pgbouncer.ini and other files as needed. The file is rewritten in place, so mount it so that it is writable and replaceable (for example, mount a directory rather than a single file).
The scripts and documentation in this repository are released under the MIT License. The image also contains software from percona/percona-pgbouncer, which remains under its own licenses (see /licenses in the image).
docker pull donuk/pgbouncer:latest