DEV Community

konsongong
konsongong

Posted on Originally published at vpsfolio.com

Shared PostgreSQL Container vs Per-App Databases in Docker Compose (Measured)

Measured Sandbox Benchmark

Idle RAM Startup Containers Recommended VPS
401.5 MB 113.0s 4 1 GB VPS

Per-container RAM: gitea 180.4 MB · umami 150.5 MB · caddy 42.5 MB · db 28.1 MB (sandbox verified 2026-11-24)

View the interactive benchmark plate and full VPS sizing comparison or compare all measured stacks on VPSFolio Benchmarks.


Architecture and why these pieces fit together

Two apps, one database engine. Gitea and Umami both speak PostgreSQL, both are happy on 16, and both sit idle most of the day. On a 1 GB VPS the question is not whether you can run two Postgres containers — you can — but whether the extra ~30 MB per idle instance is worth the operational isolation you give up.

This stack puts a single postgres:16-alpine container behind both apps. An inline init script creates two databases (gitea, umami) and two least-privilege roles (gitea_user, umami_user) with separate passwords pulled from the environment. Caddy fronts both apps on 127.0.0.1:18080 and routes by Host header, so git.local hits Gitea on :3000 and analytics.local hits Umami on :3000.

Why this shape:

  • One engine, two tenants. Postgres handles multi-database isolation natively. Separate roles mean a compromised Umami credential cannot read Gitea's tables.
  • No host bind mounts. The init SQL lives in a top-level configs: block and is mounted at /docker-entrypoint-initdb.d/01-init-apps.sql. The Caddyfile is inline too. The whole stack is one compose.yml plus .env.
  • Caddy only on loopback. Backend HTTP ports stay off the host. Caddy is the single ingress on 127.0.0.1:${CADDY_HTTP_PORT:-18080}.
  • Grounded images. Gitea uses gitea/gitea:1.22 with the same GITEA__* env names as the single-app Gitea guide. Umami uses ghcr.io/umami-software/umami:latest with the same DATABASE_URL/APP_SECRET contract as the single-app Umami guide. The only change is the database host and credentials.

The trade-off is real: both apps now share a Postgres major version. If Umami someday requires 17 and Gitea pins 16, you either fork the container or migrate one app out. That is the cost of the RAM savings. The measured numbers below make the decision concrete.

Prerequisites, ports, and VPS sizing

  • Docker Engine 24+ with the Compose v2 plugin.
  • A 1 GB VPS is enough. This stack is designed for it.
  • Ports: 18080/tcp on loopback for Caddy. Gitea SSH is not published in this stack — add 2222:22 if you need it.
  • DNS for local testing: add 127.0.0.1 git.local analytics.local to /etc/hosts on the machine running curl.

Generate secrets before first boot:

openssl rand -hex 32   # GITEA_SECRET_KEY
openssl rand -hex 32   # GITEA_INTERNAL_TOKEN
openssl rand -hex 32   # GITEA_JWT_SECRET
openssl rand -hex 32   # GITEA_DB_PASSWORD
openssl rand -hex 32   # UMAMI_DB_PASSWORD
openssl rand -hex 32   # APP_SECRET
openssl rand -hex 32   # TWO_FACTOR_ENCRYPTION_KEY
openssl rand -hex 32   # POSTGRES_SUPERUSER_PASSWORD
Enter fullscreen mode Exit fullscreen mode

Sizing note: in our sandbox run, the single shared postgres:16-alpine container idled at 28.1 MB RSS serving both Gitea and Umami. Compare that against our verified solo guides, where separate PostgreSQL containers consumed 70.6 MB combined (24.2 MB for Gitea's postgres:16-alpine plus 46.4 MB for Umami's postgres:15-alpine). On a 1 GB VPS, saving ~42.5 MB (roughly 4% of RAM) is modest for two apps, though it compounds if you pack four or more Postgres-backed services onto one box.

Single-file Docker Compose setup

Write compose.yml and .env exactly as below. The init SQL is embedded in configs: — no ./init.sql on the host.

cat <<'EOF' > compose.yml
services:
  caddy:
    image: caddy:2-alpine
    restart: unless-stopped
    ports:
      - "127.0.0.1:${CADDY_HTTP_PORT:-18080}:80"
    configs:
      - source: caddyfile
        target: /etc/caddy/Caddyfile
    depends_on:
      - gitea
      - umami

  gitea:
    image: gitea/gitea:1.22
    restart: unless-stopped
    environment:
      USER_UID: "1000"
      USER_GID: "1000"
      TZ: ${TZ:-UTC}
      GITEA__database__DB_TYPE: postgres
      GITEA__database__HOST: db:5432
      GITEA__database__NAME: gitea
      GITEA__database__USER: gitea_user
      GITEA__database__PASSWD: ${GITEA_DB_PASSWORD}
      GITEA__server__DOMAIN: ${GITEA_DOMAIN:-git.local}
      GITEA__server__ROOT_URL: http://${GITEA_DOMAIN:-git.local}/
      GITEA__server__SSH_DOMAIN: ${GITEA_DOMAIN:-git.local}
      GITEA__server__SSH_PORT: ${GITEA_SSH_PORT:-2222}
      GITEA__server__HTTP_PORT: "3000"
      GITEA__server__PROTOCOL: http
      GITEA__security__SECRET_KEY: ${GITEA_SECRET_KEY}
      GITEA__security__INTERNAL_TOKEN: ${GITEA_INTERNAL_TOKEN}
      GITEA__oauth2__JWT_SECRET: ${GITEA_JWT_SECRET}
      GITEA__service__DISABLE_REGISTRATION: "true"
    volumes:
      - gitea_data:/data
    depends_on:
      db:
        condition: service_healthy

  umami:
    image: ghcr.io/umami-software/umami:latest
    restart: unless-stopped
    init: true
    environment:
      DATABASE_URL: postgresql://umami_user:${UMAMI_DB_PASSWORD}@db:5432/umami
      APP_SECRET: ${APP_SECRET}
      TWO_FACTOR_ENCRYPTION_KEY: ${TWO_FACTOR_ENCRYPTION_KEY}
      TZ: ${TZ:-UTC}
    depends_on:
      db:
        condition: service_healthy
    healthcheck:
      test: ["CMD-SHELL", "curl -fsS http://localhost:3000/api/heartbeat || exit 1"]
      interval: 10s
      timeout: 5s
      retries: 5

  db:
    image: postgres:16-alpine
    restart: unless-stopped
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: ${POSTGRES_SUPERUSER_PASSWORD}
      POSTGRES_DB: postgres
      GITEA_DB_PASSWORD: ${GITEA_DB_PASSWORD}
      UMAMI_DB_PASSWORD: ${UMAMI_DB_PASSWORD}
      TZ: ${TZ:-UTC}
    volumes:
      - pg_data:/var/lib/postgresql/data
    configs:
      - source: init_apps_sql
        target: /docker-entrypoint-initdb.d/01-init-apps.sql
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres -d postgres"]
      interval: 10s
      timeout: 5s
      retries: 10

configs:
  caddyfile:
    content: |
      {
        auto_https off
      }
      http://git.local {
        reverse_proxy gitea:3000
      }
      http://analytics.local {
        reverse_proxy umami:3000
      }
  init_apps_sql:
    content: |
      -- Runs once on first init of the pg_data volume.
      -- Passwords are injected by the entrypoint via psql variables.
      \set gitea_pw `printf '%s' "$GITEA_DB_PASSWORD"`
      \set umami_pw `printf '%s' "$UMAMI_DB_PASSWORD"`

      CREATE ROLE gitea_user LOGIN PASSWORD :'gitea_pw';
      CREATE ROLE umami_user LOGIN PASSWORD :'umami_pw';

      CREATE DATABASE gitea OWNER gitea_user;
      CREATE DATABASE umami OWNER umami_user;

      REVOKE ALL ON DATABASE gitea FROM PUBLIC;
      REVOKE ALL ON DATABASE umami FROM PUBLIC;

      GRANT ALL PRIVILEGES ON DATABASE gitea TO gitea_user;
      GRANT ALL PRIVILEGES ON DATABASE umami TO umami_user;

volumes:
  gitea_data:
  pg_data:
EOF

cat <<'EOF' > .env
TZ=UTC
CADDY_HTTP_PORT=18080
GITEA_DOMAIN=git.local
GITEA_SSH_PORT=2222
GITEA_SECRET_KEY=replace-me-with-openssl-rand-hex-32
GITEA_INTERNAL_TOKEN=replace-me-with-openssl-rand-hex-32
GITEA_JWT_SECRET=replace-me-with-openssl-rand-hex-32
GITEA_DB_PASSWORD=replace-me-with-openssl-rand-hex-32
UMAMI_DB_PASSWORD=replace-me-with-openssl-rand-hex-32
APP_SECRET=replace-me-with-openssl-rand-hex-32
TWO_FACTOR_ENCRYPTION_KEY=replace-me-with-openssl-rand-hex-32
POSTGRES_SUPERUSER_PASSWORD=replace-me-with-openssl-rand-hex-32
EOF
Enter fullscreen mode Exit fullscreen mode

Bring it up:

docker compose up -d
docker compose ps
Enter fullscreen mode Exit fullscreen mode

The init SQL only runs when pg_data is empty. If you change it later, you must recreate the volume — which wipes both apps. Plan accordingly.

Local verification before going live

Add the hostnames to /etc/hosts on the machine you are curling from:

printf '127.0.0.1 git.local analytics.local\n' | sudo tee -a /etc/hosts
Enter fullscreen mode Exit fullscreen mode

Probe both apps through Caddy:

curl -i -H "Host: git.local" http://127.0.0.1:18080/
curl -i -H "Host: analytics.local" http://127.0.0.1:18080/
Enter fullscreen mode Exit fullscreen mode

Expect 200 from Gitea (the install page) and 200 from Umami (the login page). If you get 502, Caddy is up but the backend is not ready yet — check docker compose logs gitea umami.

Confirm the databases and roles exist:

docker compose exec db psql -U postgres -c '\l'
docker compose exec db psql -U postgres -c '\du'
Enter fullscreen mode Exit fullscreen mode

You should see gitea and umami databases, and gitea_user/umami_user roles. Verify isolation by trying to connect as one role to the other's database — it must fail:

docker compose exec db psql -U gitea_user -d umami -c 'select 1;'
# FATAL: permission denied for database "umami"
Enter fullscreen mode Exit fullscreen mode

Check the healthchecks:

docker compose ps
docker inspect --format '{{.State.Health.Status}}' $(docker compose ps -q db)
Enter fullscreen mode Exit fullscreen mode

Production cutover (HTTPS / remote backups)

When you point real domains at the VPS, three things change.

1. Caddyfile. Remove auto_https off, replace the .local hosts with real domains, and let Caddy issue certificates:

 configs:
   caddyfile:
     content: |
-      {
-        auto_https off
-      }
-      http://git.local {
+      git.example.com {
         reverse_proxy gitea:3000
       }
-      http://analytics.local {
+      analytics.example.com {
         reverse_proxy umami:3000
       }
Enter fullscreen mode Exit fullscreen mode

2. Ports. Publish 80 and 443 on Caddy instead of the loopback-only 18080:

   caddy:
     image: caddy:2-alpine
     restart: unless-stopped
     ports:
-      - "127.0.0.1:${CADDY_HTTP_PORT:-18080}:80"
+      - "80:80"
+      - "443:443"
Enter fullscreen mode Exit fullscreen mode

3. Gitea URLs and Caddy cert persistence. Set GITEA_DOMAIN=git.example.com in .env, change GITEA__server__ROOT_URL in compose.yml from http:// to https://${GITEA_DOMAIN}/, and add named volumes caddy_data:/data and caddy_config:/config to caddy (plus top-level volumes:) so Let's Encrypt certificates persist across container recreates.

For remote backups, add a pg_dump sidecar or a cron job on the host. A minimal dump of both databases:

docker compose exec -T db pg_dump -U postgres -Fc gitea > gitea-$(date +%F).dump
docker compose exec -T db pg_dump -U postgres -Fc umami > umami-$(date +%F).dump
Enter fullscreen mode Exit fullscreen mode

Ship those to object storage with rclone or restic. Do not rely on the volume alone.

Day-2 operations, backups, and trade-offs

Upgrades. Postgres major upgrades are the sharp edge here. pg_upgrade works, but you are upgrading both apps at once. Test on a copy of the volume first. Gitea and Umami both tolerate 16 today; if either pins a different major later, split the database out for that app and accept the extra ~30 MB.

Blast radius. One Postgres means one failure domain. A corrupted pg_data takes down both apps. Mitigate with pg_dump on a schedule and a tested restore path. Restore into a scratch container before you trust the backup:

docker run --rm -v pg_data_restore:/var/lib/postgresql/data postgres:16-alpine true
docker compose exec -T db pg_restore -U postgres -d gitea < gitea-2025-01-01.dump
Enter fullscreen mode Exit fullscreen mode

Connection limits. Default max_connections is 100. Gitea and Umami together use a handful. If you add more apps, watch pg_stat_activity and raise the limit in postgresql.conf via command: in the compose file rather than editing the volume.

Measured RAM: shared vs. per-app PostgreSQL. Here is the side-by-side comparison from our disposable Docker sandbox runs:

Setup App Containers PostgreSQL Container(s) Reverse Proxy Measured Total
Separate DBs (Gitea + Umami) gitea 173.7 MB + umami 165.9 MB = 339.6 MB postgres:16 24.2 MB + postgres:15 46.4 MB = 70.6 MB None 410.2 MB
Shared PostgreSQL 16 (this stack) gitea 180.4 MB + umami 150.5 MB = 330.9 MB 1× postgres:16 = 28.1 MB caddy 42.5 MB 401.5 MB (359.0 MB excl. Caddy)

Consolidating two PostgreSQL instances into one saved 42.5 MB on the database tier (from 70.6 MB down to 28.1 MB) — coincidentally matching the 42.5 MB idle footprint of Caddy itself in this run, so the complete 4-container proxied stack (401.5 MB) still sat 8.7 MB below the two unproxied separate-DB stacks (410.2 MB). Even if both apps already ran postgres:16-alpine (~24–28 MB each), sharing only frees ~20–25 MB (~2% of a 1 GB VPS). For two apps, separate Postgres containers are usually worth that small tax; sharing starts paying real dividends once you run four or more Postgres-backed services on one small VPS.

When to split. Split when: the apps need different Postgres majors, one app's load starves the other, or compliance requires separate database instances. Until then, one container with two roles is the simpler operational story on a 1 GB VPS.


Measured in our sandbox run: total memory 401.5 MB (sum of per-container peaks over a 30s idle window after startup) across 4 containers; largest container 180.4 MB; published port(s) 18080; HTTP probe returned 200, 200. The run took 113.0s including the image download. Required secret(s) APP_SECRET, GITEA_DB_PASSWORD, GITEA_INTERNAL_TOKEN, GITEA_JWT_SECRET, GITEA_SECRET_KEY, POSTGRES_SUPERUSER_PASSWORD, TWO_FACTOR_ENCRYPTION_KEY, UMAMI_DB_PASSWORD were generated for the test run, as the tutorial instructs. Container breakdown: gitea 180.4 MB, umami 150.5 MB, caddy 42.5 MB, db 28.1 MB. Verified routes: Gitea on shared Postgres (HTTP 200), Umami on shared Postgres (HTTP 200).


Verification: we ran this docker-compose stack in a disposable Docker environment and recorded the measurements above. They describe that run, not a guarantee for your host.


Originally published with full interactive benchmark metrics and server sizing notes at https://vpsfolio.com/stacks/shared-postgres-container-vs-separate-databases-docker/.

Top comments (0)