Deploying Gitea with PostgreSQL and optional Traefik

Learn to deploy a secure Gitea instance with Docker, featuring SSL/TLS via Traefik and PostgreSQL for database management. This guide provides a robust, scalable setup for your development projects, ensuring top-notch security and performance.

Deploying Gitea with PostgreSQL and optional Traefik
Gitea

Gitea is a popular, lightweight, open-source Git service renowned for its ease of setup and minimal resource requirements. This tutorial explores deploying Gitea using Docker, with two deployment strategies: leveraging Traefik for automatic SSL/TLS encryption and a simpler, non-Traefik approach to use internally or with another reverse proxy solution.

Prerequisites

  • Docker and Docker Compose installed on your server.
  • Basic familiarity with Docker, containerization, and network security.
  • A valid domain name for Traefik-based deployments to secure SSL/TLS certificates.

Gitea and Traefik Deployment

Traefik simplifies SSL/TLS management and provides a powerful reverse proxy solution. The configuration below sets up Gitea 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"

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

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

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

Non-Traefik Gitea Deployment

For environments where Traefik isn't preferred or necessary, here's a streamlined Gitea and PostgreSQL setup:

version: '3'

services:
  gitea:
    image: gitea/gitea:latest
    environment:
      - DB_TYPE=postgres
      - DB_HOST=postgres:5432
      - DB_NAME=gitea
      - DB_USER=gitea
      - DB_PASSWD=gitea
    ports:
      - "3000:3000"
      - "222:22"
    depends_on:
      - postgres
    volumes:
      - ./gitea:/data

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

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

Consider this: Gitea's Evolving Ownership

Gitea's transition to a for-profit model under a newly formed company has sparked discussions about its future direction, particularly regarding its open-source commitment and service pricing. This shift emphasizes the importance of understanding deployment options and ownership implications for those reliant on Gitea for their development workflows.

If this change in direction is of no concern for you, Gitea is still a great option. The ownership of the project and domains/IP has transferred to a company called Gitea Ltd. This company was created by the original developer of the Gitea project, Lunny Xiao. Gitea appears to have headed in this direction to be able to monetize the project and be able to work with corporations who want to use Gitea commercially. This is not necessarily bad in my opinion, but other opinions vary greatly.

See this article by Lunny for more details:

https://blog.gitea.com/a-message-from-lunny-on-gitea-ltd.-and-the-gitea-project/

Forgejo: A Community Driven Fork of Gitea

Forgejo is a fork of Gitea since 2022 and is supported by Codeberg e.V. Codeberg claims to be community driven and is a non-profit organization. Consider Forgejo if the new ownership structure of Gitea is of concern for you.

💡
See my very similar article around deploying Forgejo.
Deploying Forgejo with PostgreSQL and optional Traefik

Additional Tips and Troubleshooting

  • 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.