Postgres and Valkey on the server

Docker mode with Postgres and Valkey on the server. Each change and its undo.

This page is for Docker mode with --postgres host or --valkey host. The Zerodot containers use the Postgres and Valkey that run on the server, not their own containers.

sudo bash install.sh zerodot.example.com --docker --postgres host --valkey host

The script asks this question itself when it finds Postgres 13+ or Valkey on the server.

Why changes are necessary

The containers reach the server at host.docker.internal. This name points to the Docker bridge address of the server, usually 172.17.0.1. The containers come from the Docker network of the install, for example 172.18.0.0/16.

Postgres and Valkey usually listen on localhost only. Thus they must also:

  • listen on the bridge address,
  • let the Docker network of the install log in,
  • get through the firewall.

The bridge address is not on the internet.

What the script changes

Before it changes anything, the script shows the list and asks. If you say no, it prints the list and stops. With --yes, it stops unless you add --allow-host-changes.

Change Service Restart
Make a user and a database Postgres No
Add the bridge address to listen_addresses Postgres Yes. Other apps lose their connections for some seconds.
Add one line to pg_hba.conf Postgres No (reload)
Add the bridge address to bind Valkey No
Set protected-mode no. Only when Valkey has no password. Valkey No
Set maxmemory-policy noeviction Valkey No. Only when Valkey evicts keys and has a maxmemory.
Allow the Docker network to the port ufw or firewalld No
Start Postgres and Valkey after Docker systemd No

Before the app starts, the script tests the connection from a container.

In the steps below, the bridge address is 172.17.0.1. The network of the install is 172.18.0.0/16. Find your values first:

ip -4 addr show docker0
cd /var/www/zerodot
docker compose up --no-start
docker network inspect -f '{{range .IPAM.Config}}{{.Subnet}}{{end}}' zerodot_default

Postgres

1. Make the user and the database

PASS=$(openssl rand -hex 24)
sudo -u postgres psql -c "create role zerodot login password '$PASS'"
sudo -u postgres psql -c "create database zerodot owner zerodot"
echo "$PASS"

Put the URL in .env of the install:

DATABASE_URL=postgres://zerodot:<password>@host.docker.internal:5432/zerodot

2. Listen on the bridge address

Show the current value:

sudo -u postgres psql -Atc 'show listen_addresses'

Add the bridge address to it. Example for the value localhost:

sudo -u postgres psql -c "alter system set listen_addresses = 'localhost,172.17.0.1'"
sudo systemctl restart postgresql

ALTER SYSTEM writes the value to postgresql.auto.conf. This file wins over postgresql.conf. A change to listen_addresses needs a restart.

If the value is *, Postgres listens on all addresses. Skip this step.

Undo:

sudo -u postgres psql -c "alter system reset listen_addresses"
sudo systemctl restart postgresql

3. Add a line to pg_hba.conf

Find the file:

sudo -u postgres psql -Atc 'show hba_file'

Find the method. If password_encryption is md5, use md5. Else use scram-sha-256:

sudo -u postgres psql -Atc 'show password_encryption'

Make a copy of the file. Then add this line before the first rule. Postgres uses the first line that matches.

# Added by Zerodot install.sh (zerodot)
host zerodot zerodot 172.18.0.0/16 scram-sha-256

The line lets only this user, only this database, and only the Docker network of the install in. Reload Postgres:

sudo -u postgres psql -c 'select pg_reload_conf()'

Undo: remove the two lines. Or put back the copy that the script made (pg_hba.conf.bak-zerodot-<time>). Then reload Postgres.

4. Check max_connections

sudo -u postgres psql -Atc 'show max_connections'

One worker uses about 30 connections, web and api 10 each. Other apps share the limit. With less than 150, the script shows a warning. See Connections.

Valkey or Redis

1. Pick a database number

Use an empty database number from 1 to 15. Other apps usually use 0.

for n in $(seq 1 15); do echo "$n $(valkey-cli -n $n dbsize)"; done

Use redis-cli if the server has Redis. Put the URL in .env of the install:

VALKEY_URL=redis://host.docker.internal:6379/1

If Valkey needs a password, set VALKEY_URL=redis://:<password>@127.0.0.1:6379/1 before you run the script. The script changes 127.0.0.1 to host.docker.internal.

2. Listen on the bridge address (Valkey)

Show the current value:

valkey-cli config get bind

Valkey and Redis 7 or newer can change bind while they run. Add the bridge address with a - in front. The - makes the address optional: Valkey does not fail when the bridge is missing at boot. Example for the value 127.0.0.1 -::1:

valkey-cli config set bind "127.0.0.1 -::1 -172.17.0.1"
valkey-cli config rewrite

CONFIG REWRITE saves the value in the config file.

Redis 6 cannot change bind while it runs. Edit the config file by hand:

  1. Open /etc/redis/redis.conf (or /etc/redis.conf).
  2. Add 172.17.0.1 to the bind line.
  3. Restart Redis.
  4. Run the script again.

If bind is * or 0.0.0.0, Valkey listens on all addresses. Skip this step.

Undo: set bind to the old value, then run config rewrite.

3. Protected mode

Valkey and Redis 7+ turn on protected mode by default. In protected mode, a server without a password refuses connections from other addresses. The test from the container then shows -DENIED.

Check the setting:

valkey-cli config get protected-mode

If it is yes and Valkey has no password, the script adds "protected-mode yes -> no" to its list of changes and asks. A password is the better choice. Do one of these:

  • Give Valkey a password, and put it in VALKEY_URL (recommended). Other apps on the server then also need the password.

  • Turn protected mode off. Then bind and the firewall are the only protection. Keep bind on local and bridge addresses only.

    valkey-cli config set protected-mode no
    valkey-cli config rewrite
    

4. Eviction

valkey-cli config get maxmemory-policy
valkey-cli config get maxmemory

The queue needs noeviction. If the policy is another value and maxmemory is not 0, set it:

valkey-cli config set maxmemory-policy noeviction
valkey-cli config rewrite

Other apps that use this Valkey as a cache then get errors when the memory is full. If they need eviction, use the Valkey container (--valkey docker).

Undo: set the old policy, then run config rewrite.

Firewall

Let the Docker network reach the port on the bridge address.

ufw:

sudo ufw allow from 172.18.0.0/16 to 172.17.0.1 port 5432 proto tcp
sudo ufw allow from 172.18.0.0/16 to 172.17.0.1 port 6379 proto tcp

Undo:

sudo ufw delete allow from 172.18.0.0/16 to 172.17.0.1 port 5432 proto tcp
sudo ufw delete allow from 172.18.0.0/16 to 172.17.0.1 port 6379 proto tcp

firewalld (the zone docker, or the default zone if docker does not exist):

RULE='rule family=ipv4 source address=172.18.0.0/16 destination address=172.17.0.1 port port=5432 protocol=tcp accept'
sudo firewall-cmd --permanent --zone=docker --add-rich-rule="$RULE"
sudo firewall-cmd --zone=docker --add-rich-rule="$RULE"

Do the same for port 6379. Do not run firewall-cmd --reload. On some Docker versions, a reload removes the rules of Docker.

Undo: run the same commands with --remove-rich-rule.

Start order at boot

The bridge address exists only after Docker starts. Postgres and Valkey must start after Docker. Else they cannot listen on the bridge address.

The script adds a systemd drop-in file to each service:

sudo mkdir -p /etc/systemd/system/postgresql.service.d
sudo tee /etc/systemd/system/postgresql.service.d/zerodot-docker.conf >/dev/null <<'UNIT'
[Unit]
After=docker.service
UNIT
sudo systemctl daemon-reload

On Debian and Ubuntu, also add it to postgresql@.service.d. For Valkey, use the unit of the server: valkey-server, valkey, redis-server or redis.

The file only sets the order. Postgres and Valkey do not need Docker to start.

Undo: remove the zerodot-docker.conf files and run sudo systemctl daemon-reload.

The compose override

The script writes docker-compose.override.yml. For --postgres host --valkey host, it looks like this:

services:
  proxy:
    ports: !override
      - "127.0.0.1:3000:3000"
  postgres:
    profiles: [bundled-postgres]
  valkey:
    profiles: [bundled-valkey]
  web:
    extra_hosts: ["host.docker.internal:host-gateway"]
    environment:
      DATABASE_URL: ${DATABASE_URL:?set DATABASE_URL}
      VALKEY_URL: ${VALKEY_URL:?set VALKEY_URL}
    depends_on: !reset []
  api:
    extra_hosts: ["host.docker.internal:host-gateway"]
    environment:
      DATABASE_URL: ${DATABASE_URL:?set DATABASE_URL}
      VALKEY_URL: ${VALKEY_URL:?set VALKEY_URL}
    depends_on: !reset []
  worker:
    extra_hosts: ["host.docker.internal:host-gateway"]
    environment:
      DATABASE_URL: ${DATABASE_URL:?set DATABASE_URL}
      VALKEY_URL: ${VALKEY_URL:?set VALKEY_URL}
    depends_on: !override
      sandbox: { condition: service_started }
  • A profile that nobody turns on stops the postgres and valkey containers from being made.
  • host-gateway points host.docker.internal to the bridge address.

Test the connection

cd /var/www/zerodot
docker compose run --rm --no-deps -T --entrypoint node api -e \
  "require('net').connect(5432,'host.docker.internal').on('connect',()=>{console.log('OK');process.exit(0)}).on('error',e=>{console.log(e.code);process.exit(1)})"

OK means that the container reaches Postgres. Use port 6379 for Valkey.

Change this page on GitHub