composebuilder

Docker Compose: Gitea Git Server with PostgreSQL 16

Self-hosted Gitea with PostgreSQL 16 in Docker Compose: web Git hosting, SSH on port 2222, a healthcheck-gated database start and persistent volumes.

When to use this stack

Gitea is a lightweight, self-hosted Git service with repositories, pull requests, issues, packages and a web interface similar to larger forges. It is written in Go, runs as a single binary, and can store its data in PostgreSQL, which handles concurrent users and large repositories better than an embedded database. This stack runs Gitea and PostgreSQL 16 as two containers. The database starts first and Gitea waits for it to report healthy, so the application does not fail on an empty database.

Use this stack when you want private repositories on your own server for a small team, or when you want a Git host that works with the Woodpecker CI stack in this series. It is also a sensible place to store infrastructure code that you do not want on a public forge. Do not use it when a single developer only needs a local repository, because a plain Git remote is simpler. Do not use it without HTTPS in front of the web interface, since passwords and tokens travel in the requests. Also note that the SSH port needs its own firewall rule, and Gitea holds source code, so back it up with the same care as the database.

docker-compose.yml

services:
  db:
    image: postgres:16-alpine
    restart: unless-stopped
    environment:
      POSTGRES_DB: ${POSTGRES_DB}
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    volumes:
      - gitea_db:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 20s

  gitea:
    image: gitea/gitea:1.22
    restart: unless-stopped
    environment:
      GITEA__database__DB_TYPE: postgres
      GITEA__database__HOST: db:5432
      GITEA__database__NAME: ${POSTGRES_DB}
      GITEA__database__USER: ${POSTGRES_USER}
      GITEA__database__PASSWD: ${POSTGRES_PASSWORD}
      GITEA__server__DOMAIN: ${GITEA_DOMAIN}
      GITEA__server__ROOT_URL: ${GITEA_ROOT_URL}
      GITEA__server__SSH_PORT: "${GITEA_SSH_PORT}"
      GITEA__security__INSTALL_LOCK: "true"
      GITEA__security__SECRET_KEY: ${GITEA_SECRET_KEY}
      GITEA__service__DISABLE_REGISTRATION: "true"
    ports:
      - "127.0.0.1:${GITEA_HTTP_PORT}:3000"
      - "${GITEA_SSH_PORT}:22"
    volumes:
      - gitea_data:/data
    depends_on:
      db:
        condition: service_healthy

volumes:
  gitea_db:
  gitea_data:

.env

.env
# PostgreSQL database, user and password used by both services
POSTGRES_DB=gitea
POSTGRES_USER=gitea
POSTGRES_PASSWORD=CHANGE_ME_long_random_db_password

# Gitea public names and secret. Generate the secret with:
# docker run --rm gitea/gitea:1.22 gitea generate secret SECRET_KEY
GITEA_DOMAIN=CHANGE_ME_git.example.com
GITEA_ROOT_URL=https://CHANGE_ME_git.example.com/
GITEA_SECRET_KEY=CHANGE_ME_generated_secret_key

# Host ports. HTTP stays on 127.0.0.1, SSH is published for git over ssh.
GITEA_HTTP_PORT=3000
GITEA_SSH_PORT=2222

Start and verify

Shell
# Validate the file and .env, then start the stack
docker compose config --quiet
docker compose up -d

# db should report "healthy" before gitea starts
docker compose ps
docker compose logs --tail=50 gitea

# Verify the web interface answers on localhost
curl -sI http://127.0.0.1:3000 | head -n 1

# Create the first administrator
docker compose exec -u git gitea gitea admin user create \
  --admin --username CHANGE_ME_admin \
  --password 'CHANGE_ME_long_password' \
  --email [email protected]

# Verify SSH on the published port (after adding a key in the web UI)
ssh -p 2222 -T git@CHANGE_ME_git.example.com

Production checklist

  • Put an HTTPS reverse proxy such as Caddy or Traefik in front of port 3000, and set GITEA_ROOT_URL to the HTTPS address the browser uses.
  • Keep the database unpublished. Only the gitea service reaches it, over the Docker network.
  • Generate GITEA_SECRET_KEY once and keep it. Changing it later breaks stored two-factor secrets and encrypted settings.
  • Leave DISABLE_REGISTRATION on, and create user accounts from the administrator panel.
  • Turn on two-factor authentication for every administrator account.
  • Restrict the SSH port with the host firewall to the networks that need it, and use key-based authentication only.
  • Back up both volumes together: gitea_db holds metadata and gitea_data holds the repositories. Use pg_dump for the database and copy gitea_data while the stack is stopped or with Gitea’s own backup command.
  • Pin gitea:1.22 and postgres:16-alpine. Major PostgreSQL upgrades need a dump and restore, not an in-place swap of the image.

Common problems

  • Symptom: Gitea exits with “connection refused” to db:5432 at startup. Cause: The database is not ready yet, or the healthcheck was removed. Fix: Run docker compose ps and confirm db shows “healthy”, then start Gitea again.
  • Symptom: Gitea logs “password authentication failed for user”. Cause: POSTGRES_PASSWORD was changed in .env after the volume was created, so the database still has the old password. Fix: Change the password inside PostgreSQL with ALTER USER, or recreate the database volume if the data is disposable.
  • Symptom: Clone URLs show the wrong domain or the wrong SSH port. Cause: GITEA_DOMAIN, GITEA_ROOT_URL or GITEA_SSH_PORT does not match the public address. Fix: Correct the values in .env, run docker compose up -d, and reload the page.
  • Symptom: Sign-in or forms fail with “invalid CSRF token”. Cause: The browser URL differs from GITEA_ROOT_URL, usually because of http versus https. Fix: Set GITEA_ROOT_URL to the exact address you open in the browser.
  • Symptom: git over SSH says permission denied. Cause: The public key is not added to the account, or the client uses port 22 instead of 2222. Fix: Add the key in the web UI and run ssh -p 2222 -T git@your-domain.

Variations

  • SQLite for one user: Remove the db service and set DB_TYPE to sqlite3 for a single-user instance with very few repositories. This lowers the moving parts but is not recommended for teams.
  • CI integration: Add the Woodpecker CI stack from this series, using a Gitea OAuth application, to run pipelines for pushed commits.
  • Object storage for attachments: Store LFS objects and attachments on S3-compatible storage through the Gitea storage settings when the disk grows large.

Questions

Why is PostgreSQL not published to the host?

Gitea reaches the database over the Docker network through the name db. Keeping the database off the host means no outside connection to it is possible.

How do I create the first administrator?

Run docker compose exec -u git gitea gitea admin user create with --admin, a username, a password and an email address. The installer page is skipped because INSTALL_LOCK is set.

Why does the clone URL show the wrong SSH port?

The SSH port Gitea advertises comes from GITEA_SSH_PORT, which must match the host port you publish. Change it in .env and restart Gitea.

Can I move Gitea to a different domain later?

Yes, but change GITEA_ROOT_URL and GITEA_DOMAIN first, then update any webhooks and saved remote URLs in your repositories.

Adapt this stack

Load these files into the builder to rename services, change ports, add a cache or a proxy, and generate fresh passwords for the .env.

Open in builder
  • Caddy 2 Reverse Proxy + Static Web Server

    Caddy 2 in Docker Compose: serve a static site, proxy /api to a backend and get automatic HTTPS on a real domain. Pinned images and .env configuration.

    • caddy
    • whoami
    Difficulty: Beginner
  • Loki, Promtail and Grafana Log Stack

    Collect Linux host logs with Loki and Promtail in Docker Compose and query them in Grafana. Pinned images, persistent volumes and a localhost-only Grafana port.

    • loki
    • promtail
    • grafana
    Difficulty: Intermediate
  • Portainer CE Web UI for Docker Management

    Run Portainer CE in Docker Compose as a web UI for containers, images and volumes. Bound to localhost on HTTPS 9443, with persistent data and a pinned image.

    • portainer
    Difficulty: Intermediate
  • Prometheus + Grafana + Node Exporter

    Monitor a Linux host with Prometheus, Node Exporter and Grafana in Docker Compose. Pinned images, .env secrets, retention settings and localhost-only ports.

    • prometheus
    • node-exporter
    • grafana
    Difficulty: Intermediate

Browse more Infrastructure & Monitoring stacks or the full library.