HUB
Sign inSign up

donuk/pgbouncer

By donuk

•Updated about 15 hours ago

Run pgbouncer using config from environment variables

Image
0

1.5K

donuk/pgbouncer repository overview

⁠donuk/pgbouncer

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.

⁠Features

  • PGBOUNCER_<OPTION>: Any [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 / LISTEN_PORT: Shorthand for 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.
  • On container startup, the entrypoint script rewrites pgbouncer.ini so these values match the environment. Environment variables win over any existing definition of the same option in the file.

⁠Usage

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

⁠Environment Variables

  • 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.

⁠TLS to the backend (e.g. RDS)

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.

VariableMeaning
SERVER_TLS_CA_PEMOne 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_URLOne or more whitespace-separated URLs to download PEM bundles from.
SERVER_TLS_CA_SHA256Required 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:

  • The container refuses to start if a checksum is missing or does not match, a download fails, or no valid certificate is found. There is no option to skip verification.
  • AWS republishes the RDS bundle from time to time, which changes its checksum. Calculate it yourself with curl -s <url> | sha256sum and update SERVER_TLS_CA_SHA256 when you choose to accept a new bundle.
  • These variables don't use the PGBOUNCER_ prefix, so they aren't written into pgbouncer.ini. Setting PGBOUNCER_SERVER_TLS_CA_FILE together with them is an error.
  • A warning is logged if a CA is configured but server_tls_sslmode isn't verify-ca or verify-full. Set it with PGBOUNCER_SERVER_TLS_SSLMODE.
  • With 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 download needs outbound access to the URL when the container starts.

⁠TLS for incoming client connections

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.

VariableMeaning
CLIENT_TLS_CERT_PEMPgBouncer's own certificate (client_tls_cert_file). May include intermediates after the leaf.
CLIENT_TLS_KEY_PEMIts 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_SHA256CAs 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:

  • The container refuses to start if the key doesn't match the certificate, a PEM value can't be parsed, or the key is passphrase-protected.
  • Setting PGBOUNCER_CLIENT_TLS_CERT_FILE, PGBOUNCER_CLIENT_TLS_KEY_FILE or PGBOUNCER_CLIENT_TLS_CA_FILE together with the matching variable is an error.
  • A warning is logged if a certificate is installed but 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.
  • The private key is visible to anyone who can run docker inspect on the container, so prefer your orchestrator's secret handling for passing it.
  • Values are read once at startup, so changing a certificate needs a container restart.

⁠Reducing load on the backend server

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
OptionEffect
PGBOUNCER_POOL_MODEtransaction lets many clients share a few server connections.
PGBOUNCER_DEFAULT_POOL_SIZEServer connections per user/database pair.
PGBOUNCER_MAX_DB_CONNECTIONSHard cap on server connections per database.
PGBOUNCER_MAX_USER_CONNECTIONSHard cap on server connections per user.
PGBOUNCER_RESERVE_POOL_SIZEExtra burst connections; 0 keeps memory predictable.
PGBOUNCER_SERVER_IDLE_TIMEOUTClose idle server connections sooner.
PGBOUNCER_SERVER_LIFETIMERecycle long-lived backends, whose caches grow over time.
PGBOUNCER_MAX_PREPARED_STATEMENTSLimits prepared statements tracked in transaction mode.
PGBOUNCER_MAX_CLIENT_CONNLimits clients connecting to PgBouncer itself.
PGBOUNCER_QUERY_WAIT_TIMEOUTFail clients that wait too long for a connection instead of queueing forever.
PGBOUNCER_QUERY_TIMEOUT / PGBOUNCER_IDLE_TRANSACTION_TIMEOUTStop 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.

⁠How it Works

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:

ScriptWhat it does
10-env-options.shCollects LISTEN_ADDR, LISTEN_PORT and every PGBOUNCER_<OPTION> variable. Option names are lower-cased.
20-tls.shBuilds 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.shRemoves 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.shPrints the resulting configuration, without blank lines and comments.

A script that exits non-zero aborts startup.

⁠Adding your own steps

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).

⁠Credits

⁠License

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).

Tag summary
Recent tags
Content type
Image
Digest
sha256:aecf2910d…
Size
233.7 MB
Last updated
about 15 hours ago
docker pull donuk/pgbouncer:latest