Deploy Forgejo on Docker: Lightweight Self-Hosted Git for Your HomeLab

HomeLab tutorial - IT technology blog
HomeLab tutorial - IT technology blog

It was 2:15 AM on a Sunday when my phone buzzed with alert notifications. GitLab had chewed through its full 16GB memory reservation on my home server. The Linux kernel out-of-memory killer stepped in, terminating not just Git, but my reverse proxy and local DNS resolver alongside it. All of that chaos occurred just to version-control 18 Ansible playbooks and a few Docker Compose manifests.

That 3 AM cleanup session made one thing obvious: enterprise-tier suites are pure overkill for personal homelabs. I needed an independent, community-governed Git server that boots in under 3 seconds and idles below 100MB of RAM. That search led straight to Forgejo.

Quick Start (5 Min): Minimal Docker Deployment

Forgejo runs cleanly in an isolated container. A minimal deployment pairs the rootless application image with an embedded SQLite database. This lightweight setup handles small teams and single-developer homelabs with ease, consuming around 65MB to 80MB of resident memory.

Start by preparing a host directory structure for persistent data:

mkdir -p /opt/forgejo/{data,config}
cd /opt/forgejo

Save this production-ready docker-compose.yml file inside /opt/forgejo:

services:
  server:
    image: codeberg.org/forgejo/forgejo:7.0-rootless
    container_name: forgejo
    restart: always
    environment:
      - USER_UID=1000
      - USER_GID=1000
      - FORGEJO__database__DB_TYPE=sqlite3
      - FORGEJO__database__PATH=/var/lib/gitea/data/forgejo.db
      - FORGEJO__server__DOMAIN=git.homelab.local
      - FORGEJO__server__SSH_PORT=2222
      - FORGEJO__server__ROOT_URL=http://git.homelab.local:3000/
    ports:
      - "3000:3000"
      - "2222:2222"
    volumes:
      - /opt/forgejo/data:/var/lib/gitea
      - /etc/timezone:/etc/timezone:ro
      - /etc/localtime:/etc/localtime:ro

Because the container runs as an unprivileged user (UID 1000), adjust directory ownership before launching:

chown -R 1000:1000 /opt/forgejo/data
docker compose up -d

Within two seconds, the web service comes alive. Open http://<your-server-ip>:3000 in your browser. Complete the initial web wizard to register your administrator account. Once submitted, your server is ready—running comfortably on barely 75MB of RAM.

Deep Dive: Architecture, Storage, and Identity

Spinning up the container takes seconds. Operating it long-term requires knowing how Forgejo structures its data, processes, and permissions under the hood.

Why Forgejo Over Gitea or GitLab?

Forgejo is a community-driven fork of Gitea, created when Gitea’s governance shifted toward a for-profit commercial entity. It is fully copyleft-licensed (GPLv3+) and managed by Codeberg e.V. Rather than locking advanced capabilities behind paid tiers, Forgejo keeps every feature open. While GitLab’s Sidekiq queues and Ruby workers require 4 CPU cores and 8GB of memory to idle, Forgejo runs reliably on a modest 1-core VPS or a 1GB Raspberry Pi 4.

Filesystem Layout and Persistence

Stateful data lives inside /var/lib/gitea within the container, which maps directly to /opt/forgejo/data on the host:

  • /var/lib/gitea/git/repositories/: Raw bare Git repositories. If the database ever corrupts, your commit tree, tags, and commit history remain completely readable via the standard Git CLI.
  • /var/lib/gitea/data/: Contains user avatars, Git LFS objects, issue attachments, and the SQLite database file (forgejo.db).
  • /var/lib/gitea/custom/conf/app.ini: The core configuration file where all container environment variables are injected and preserved.

Rootless Container Security

Default Docker containers execute as root inside user namespaces. If an unpatched flaw hits the Git parsing engine, running as root makes breakout attacks much easier. The forgejo:*-rootless image forces the application to execute as UID 1000. Even if a process manages to escape containment, it drops onto the host with zero administrative permissions.

Advanced Usage: Production Upgrades

As repository counts climb and automated CI pipelines fire parallel webhooks, SQLite can hit file lock contention. Moving to PostgreSQL and configuring seamless SSH routing solves these bottlenecks.

Switching to PostgreSQL

For multi-user setups or heavy webhook automation, pair Forgejo with a lightweight PostgreSQL 16 container:

networks:
  forgejo-net:
    driver: bridge

services:
  db:
    image: postgres:16-alpine
    container_name: forgejo-db
    restart: always
    environment:
      - POSTGRES_USER=forgejo
      - POSTGRES_PASSWORD=UltraSecurePasswordHere
      - POSTGRES_DB=forgejodb
    volumes:
      - /opt/forgejo/postgres:/var/lib/postgresql/data
    networks:
      - forgejo-net

  server:
    image: codeberg.org/forgejo/forgejo:7.0-rootless
    container_name: forgejo
    restart: always
    depends_on:
      - db
    environment:
      - USER_UID=1000
      - USER_GID=1000
      - FORGEJO__database__DB_TYPE=postgres
      - FORGEJO__database__HOST=db:5432
      - FORGEJO__database__NAME=forgejodb
      - FORGEJO__database__USER=forgejo
      - FORGEJO__database__PASSWD=UltraSecurePasswordHere
      - FORGEJO__server__DOMAIN=git.homelab.local
      - FORGEJO__server__SSH_PORT=22
      - FORGEJO__server__ROOT_URL=https://git.homelab.local/
    ports:
      - "3000:3000"
      - "2222:2222"
    volumes:
      - /opt/forgejo/data:/var/lib/gitea
    networks:
      - forgejo-net

SSH Port 22 Passthrough

Typing git clone ssh://git@server:2222/repo.git gets tedious quickly. Standard Git traffic belongs on port 22. Because your host OS already runs OpenSSH on port 22, forward incoming Git traffic transparently without moving your host SSH daemon.

Create a dedicated git user on the host system:

sudo adduser --system --shell /bin/sh --gecos 'Git Version Control' --group --disabled-password --home /home/git git

Generate an internal keypair so the host git user can authenticate against the container on port 2222:

sudo -u git ssh-keygen -t ed25519 -N '' -f /home/git/.ssh/id_ed25519
sudo -u git cat /home/git/.ssh/id_ed25519.pub | sudo tee -a /opt/forgejo/data/.ssh/authorized_keys

Link the container’s public key registry to the host git user, ensuring keys registered in the Forgejo web UI authorize connections on the host:

sudo ln -sf /opt/forgejo/data/.ssh/authorized_keys /home/git/.ssh/authorized_keys

Forgejo triggers internal commands pointing to /usr/local/bin/forgejo or /app/gitea/gitea when an SSH key connects. Create a host shim script to forward these calls over 127.0.0.1:2222:

sudo cat << 'EOF' | sudo tee /usr/local/bin/forgejo
#!/bin/sh
ssh -p 2222 -i /home/git/.ssh/id_ed25519 -o StrictHostKeyChecking=no [email protected] "SSH_ORIGINAL_COMMAND=\"$SSH_ORIGINAL_COMMAND\" $0 $@"
EOF

sudo chmod +x /usr/local/bin/forgejo
sudo mkdir -p /app/gitea
sudo ln -sf /usr/local/bin/forgejo /app/gitea/gitea

Now standard URLs like git clone [email protected]:user/ansible-ops.git hit port 22 and route straight into your container.

Practical Tips: Operating Without Breakdowns

Running services is straightforward. Keeping them resilient when storage controllers fail or power glitches occur is where real engineering matters.

Automating Headless Backups

A slick web interface will not rescue your repositories when an SSD dies. Forgejo ships with an internal CLI utility that packages database records, repositories, hooks, and configuration into a unified zip file.

Trigger a non-interactive dump using Docker:

docker exec -u 1000 forgejo forgejo dump --file /var/lib/gitea/data/forgejo-dump.zip

Add a nightly cron task to dump state, rotate archives to an external mount, and remove backups older than 7 days:

0 3 * * * docker exec -u 1000 forgejo forgejo dump --file /var/lib/gitea/data/backup-$(date +\%F).zip && mv /opt/forgejo/data/backup-*.zip /mnt/external-backups/ && find /mnt/external-backups/ -name "backup-*.zip" -mtime +7 -delete

Hardening app.ini for Production

Out of the box, default configurations often leave user registration open. Close self-registration and enforce authentication by editing /opt/forgejo/data/custom/conf/app.ini:

[service]
DISABLE_REGISTRATION = true
ENABLE_CAPTCHA = false
REQUIRE_SIGNIN_VIEW = true

[session]
PROVIDER = memory

[cron.repo-health-check]
ENABLED = true
SCHEDULE = @midnight

Restart the application to load the new settings: docker compose restart server. Anonymous visitors are now forced to sign in, account creation is invite-only, and repository consistency checks run automatically every night at midnight.

Share: