Deploying Forgejo with PostgreSQL and optional Traefik

Explore deploying Forgejo, an open-source Git hosting alternative to Gitea, with Docker. Includes secure setups with Traefik and PostgreSQL. Ideal for developers seeking control and community-driven projects.

Deploying Forgejo with PostgreSQL and optional Traefik
Forgejo

Forgejo stands out in the Git hosting landscape for its commitment to open-source principles and a community-first approach. Operated under a not-for-profit model by Codeberg e.V., it offers a transparent and collaborative environment for developers. This guide delves into deploying Forgejo using Docker, with options for both automated SSL/TLS encryption using Traefik and a simpler method without Traefik. With Gitea's recent transition to a for-profit model, understanding Forgejo's values and deployment options is relevant for those prioritizing community-driven projects.

Prerequisites

  • Docker and Docker Compose installed on your server.
  • Understanding of Docker, containerization, and network security.
  • A valid domain name for Traefik setups.

Forgejo and Traefik Deployment

Traefik streamlines the SSL/TLS certificate process, acting as a reverse proxy. Below is the Docker Compose setup for integrating Forgejo with Traefik and PostgreSQL:

version: '3'

services:
  traefik:
    image: traefik:v2.5
    command:
      - "--log.level=INFO"
      - "--providers.docker=true"
      - "--providers.docker.exposedByDefault=false"
      - "--entrypoints.web.address=:80"
      - "--entrypoints.websecure.address=:443"
      - "--certificatesresolvers.myresolver.acme.email=your-email@example.com"
      - "--certificatesresolvers.myresolver.acme.storage=/letsencrypt/acme.json"
      - "--certificatesresolvers.myresolver.acme.tlschallenge=true"
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"
      - "./letsencrypt:/letsencrypt"

  forgejo:
    image: codeberg.org/forgejo/forgejo:1.21.7-0
    environment:
      - DB_TYPE=postgres
      - DB_HOST=postgres:5432
      - DB_NAME=forgejo
      - DB_USER=forgejo
      - DB_PASSWD=forgejo
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.forgejo.rule=Host(`yourdomain.com`)"
      - "traefik.http.routers.forgejo.entrypoints=websecure"
      - "traefik.http.routers.forgejo.tls.certresolver=myresolver"
    depends_on:
      - postgres
    volumes:
      - ./forgejo:/data

  postgres:
    image: postgres:16-alpine
    environment:
      POSTGRES_USER: forgejo
      POSTGRES_PASSWORD: forgejo
      POSTGRES_DB: forgejo
    volumes:
      - ./postgres:/var/lib/postgresql/data
    restart: always

After deploying with docker-compose up -d, access Forgejo at https://yourdomain.com, where Traefik has secured your connection with SSL/TLS.

Non-Traefik Forgejo Deployment

For setups bypassing Traefik, the following configuration focuses on simplicity, directly setting up Forgejo and PostgreSQL:

version: '3'

services:
  forgejo:
    image: codeberg.org/forgejo/forgejo:1.21.7-0
    environment:
      - DB_TYPE=postgres
      - DB_HOST=postgres:5432
      - DB_NAME=forgejo
      - DB_USER=forgejo
      - DB_PASSWD=forgejo
    ports:
      - "3000:3000"
      - "222:22"
    depends_on:
      - postgres
    volumes:
      - ./forgejo:/data

  postgres:
    image: postgres:16-alpine
    environment:
      POSTGRES_USER: forgejo
      POSTGRES_PASSWORD: forgejo
      POSTGRES_DB: forgejo
    volumes:
      - ./postgres:/var/lib/postgresql/data
    restart: always

Deploy using docker-compose up -d and access your Forgejo instance at http://localhost:3000 or your server's IP address on port 3000 for initial setup.


Forgejo vs. Gitea

The move by Gitea to a for-profit model under a newly formed company has stirred the community, highlighting differences in project governance and objectives. Forgejo, in contrast, is managed by Codeberg e.V., a not-for-profit organization dedicated to supporting open-source projects without commercial interests. This distinction is important for users and organizations valuing transparency, community control, and the long-term sustainability of their Git hosting platform without the influence of profit-driven motives.


Additional Tips

  • Regularly backup your PostgreSQL database to prevent data loss.
  • Explore Traefik's documentation for advanced features like load balancing and HTTP middlewares.
  • Consider monitoring your Docker containers and services for uptime and performance.