Log inSign up

Forgejo in containers with Caddy and PostgreSQL

Write a Compose file with the codeberg.org/forgejo/forgejo image and a PostgreSQL container, pass settings such as ROOT_URL and the database through FORGEJO__section__KEY variables, publish the web port on 127.0.0.1:3000 and SSH on 2222, then let Caddy serve https://code.example.net. Create the first admin from the command line inside the container.

Topic: Self-hosted apps · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13 · Updated 2026-12-03

What is Forgejo and what does it need?

Forgejo is a community-governed Git forge that grew out of Gitea and keeps a similar interface, with repositories, issues, pull requests, packages and its own Actions runner. Releases come as a stable line plus a long-term support line, and the container image is the most common way to run it.

  • Ubuntu 24.04 or Debian 12/13 with Docker and the Compose plugin. The Docker app image has both ready, or see Docker Engine install.
  • DNS for code.example.net.
  • 1 GB of memory for a handful of users with PostgreSQL alongside; more if runners share the machine.
docker --version
docker compose version
sudo mkdir -p /opt/forgejo && cd /opt/forgejo

How do you write the Compose file?

Two services are enough: Forgejo itself and PostgreSQL on a private network, with settings injected as environment variables so no installer screen is needed.

services:
  server:
    image: codeberg.org/forgejo/forgejo:15
    container_name: forgejo
    restart: unless-stopped
    depends_on: [db]
    environment:
      USER_UID: "1000"
      USER_GID: "1000"
      FORGEJO__database__DB_TYPE: postgres
      FORGEJO__database__HOST: db:5432
      FORGEJO__database__NAME: forgejo
      FORGEJO__database__USER: forgejo
      FORGEJO__database__PASSWD: ${DB_PASSWORD}
      FORGEJO__server__ROOT_URL: https://code.example.net/
      FORGEJO__server__DOMAIN: code.example.net
      FORGEJO__server__SSH_DOMAIN: code.example.net
      FORGEJO__server__SSH_PORT: "2222"
      FORGEJO__security__INSTALL_LOCK: "true"
      FORGEJO__service__DISABLE_REGISTRATION: "true"
    volumes:
      - ./data:/data
      - /etc/localtime:/etc/localtime:ro
    ports:
      - "127.0.0.1:3000:3000"
      - "2222:22"
  db:
    image: postgres:17
    restart: unless-stopped
    environment:
      POSTGRES_USER: forgejo
      POSTGRES_PASSWORD: ${DB_PASSWORD}
      POSTGRES_DB: forgejo
    volumes:
      - ./postgres:/var/lib/postgresql/data

The tag 15 is an example of pinning a major release. Check the Forgejo releases page and choose the current LTS line for a quiet life or the current stable line for new features. Generate the database password into a private .env file next to the Compose file:

printf 'DB_PASSWORD=%s\n' "$(openssl rand -hex 24)" | sudo tee /opt/forgejo/.env
sudo chmod 600 /opt/forgejo/.env

How do you start Forgejo and create the admin?

Bring the stack up, wait for the database migration in the log, then create the first administrator with the built-in CLI as the git user inside the container.

cd /opt/forgejo
sudo docker compose up -d
sudo docker compose logs -f server
sudo docker exec -u git forgejo forgejo admin user create \
  --admin --username deploy --email deploy@example.net --random-password
curl -s http://127.0.0.1:3000/api/healthz

The create command prints a random password; sign in with it and set your own at the first login. The health endpoint answers with a JSON status when the server and database are both reachable. Because INSTALL_LOCK is true, nobody can reach the web installer and overwrite the setup.

How do you serve Forgejo over HTTPS with Caddy?

Install Caddy from its official repository and add a site block that proxies to the loopback port; certificates are handled automatically.

sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update && sudo apt install -y caddy
code.example.net {
    reverse_proxy 127.0.0.1:3000
}
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw allow 2222/tcp
sudo ufw enable
sudo systemctl reload caddy
curl -I https://code.example.net

Docker publishes 2222 on its own, ufw or not, but the explicit rule documents the intent. Caddy imposes no body size limit by default, so large pushes over HTTPS work without extra settings. If you would rather keep Nginx, the Nginx reverse proxy guide applies unchanged.

How does SSH cloning work on port 2222?

The container runs its own SSH daemon on port 22, which Compose maps to 2222 on the host, so clone URLs carry that port and your normal server SSH on 22 is untouched.

git clone ssh://git@code.example.net:2222/deploy/site.git

Add your public key under Settings, SSH / GPG Keys first. To keep the shorter URL form on your own computer, add a host entry to ~/.ssh/config:

Host code.example.net
    Port 2222
    User git

After that, git clone code.example.net:deploy/site.git works. Because SSH_PORT is set to 2222, the clone box in the web interface already shows the right address.

How do you set up mail and CI runners?

Add mailer variables that point at an authenticated relay on port 587, and attach a Forgejo Runner (or another CI system) for workflows.

      FORGEJO__mailer__ENABLED: "true"
      FORGEJO__mailer__PROTOCOL: smtp+starttls
      FORGEJO__mailer__SMTP_ADDR: smtp.example.net
      FORGEJO__mailer__SMTP_PORT: "587"
      FORGEJO__mailer__FROM: code@example.net
      FORGEJO__mailer__USER: code@example.net
      FORGEJO__mailer__PASSWD: ${SMTP_PASSWORD}

Mail covers notifications, password resets and email confirmations. Port 25 is not promised open on a VPS, so ask support in a ticket before planning direct sending, and use a relay as above, with SMTP_PASSWORD added to the private .env file; SMTP relay for apps walks through it. Recreate the container with sudo docker compose up -d and send a test from Site administration.

For CI, enable Actions under Site administration and register a Forgejo Runner with a token from the repository or organisation settings. Running jobs on a second server keeps builds from slowing the forge. Woodpecker CI integrates with Forgejo as an alternative.

How do you back up and upgrade Forgejo?

Dump the database, archive the data folder, and upgrade by changing the image tag and recreating the container.

cd /opt/forgejo
sudo docker compose exec -T db pg_dump -U forgejo forgejo | gzip > /root/forgejo-db-$(date +%F).sql.gz
sudo tar -czf /root/forgejo-data-$(date +%F).tar.gz -C /opt/forgejo data
# upgrade: edit the image tag, then
sudo docker compose pull
sudo docker compose up -d
sudo docker exec forgejo forgejo --version

Forgejo migrates the database on start, and downgrades are not supported, so keep the backup from before the upgrade until the new version runs cleanly. Read the release notes for breaking changes before a major jump. Copy the archives off the server; the daily backup add-on gives you a second, disk-level layer.

What should you check when it does not work?

Look at the container log first; Forgejo prints the config path and any database or permission error on start.

  • Permission denied in /data: USER_UID and USER_GID do not match the owner of ./data.
  • Links in mail point to localhost: ROOT_URL is unset or wrong.
  • SSH clone times out: port 2222 is blocked upstream of Docker or the URL lacks the port.
  • Settings changes ignored: values in data/gitea/conf/app.ini are rewritten from the environment at each start, so change the variable, not the file.
sudo docker compose logs --tail 100 server
sudo docker compose ps

Plans that fit this guide

Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.

All prices
PlanFits becauseSizeMonthlyOrder from
A1-1personal forge1 vCPU, 1 GB RAM, 25 GB NVMe$3.60/mo$43.20 for 12 months
A1-2small team with PostgreSQL1 vCPU, 2 GB RAM, 50 GB NVMe$7.20/mo$43.20 for 6 months
A2-4team plus a local runner2 vCPU, 4 GB RAM, 100 GB NVMe$14.40/mo$43.20 for 3 months

Questions about Forgejo Git server

Is Forgejo compatible with Gitea?

It began as a fork and shares much of its design, but the projects have diverged. Moving from older Gitea versions is documented; check the Forgejo upgrade notes for your exact versions.

Should I pick the LTS or the stable release?

LTS suits a server you want to touch rarely. The stable line gets new features sooner but needs upgrades more often.

Can I install Forgejo without Docker?

Yes, a binary is published. The steps mirror the Gitea binary install: a git user, a systemd unit and a reverse proxy.

How much memory does it need?

About 1 GB with PostgreSQL for a few users. Runners, mirrors and the code search indexer are what raise the need.

Can I use SQLite?

Yes, set DB_TYPE to sqlite3 and drop the db service. It suits a personal forge; PostgreSQL is better once several people push at once.