Run Gitea behind Traefik with a sub-path

Learn how to run Gitea with a sub-path using Traefik.

Run Gitea behind Traefik with a sub-path
Photo by David Mao / Unsplash

Running Gitea with a sub-path is not as straight forward as it should be. Since I've spent some time working this out, here is a complete docker-compose.yml file:

version: "3.9"

services:
  traefik:
    image: traefik:2.9
    container_name: traefik
    restart: unless-stopped
    command:
      - "--log.level=WARN"
      - "--accesslog=false"
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.web.address=:80"
      - "--entrypoints.ssh.address=:222"
    ports:
      - "80:80"
      - "8081:8080"
      - "222:222"
    networks:
      - proxy
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"

  gitea:
    labels:
      - "traefik.enable=true"
      # - "traefik.docker.network=proxy" # Uncomment for external networks
      - "traefik.http.routers.gitea.entrypoints=web"
      - "traefik.http.routers.gitea.rule=PathPrefix(`/gitea`)"
      - "traefik.http.services.gitea.loadbalancer.server.port=3000"
      - "traefik.http.routers.gitea.middlewares=gitea-sp@docker"
      - "traefik.http.middlewares.gitea-sp.stripprefix.prefixes=/gitea"
      - "traefik.tcp.routers.gitea.entrypoints=ssh"
      - "traefik.tcp.routers.gitea.rule=HostSNI(`*`)"
      - "traefik.tcp.services.gitea.loadbalancer.server.port=22"
    image: gitea/gitea:1
    container_name: gitea
    restart: unless-stopped
    environment:
      - USER_UID=1000
      - USER_GID=1000
      - DOMAIN=${HOSTNAME}
      - ROOT_URL=http://${HOSTNAME}/gitea
      - SSH_PORT=222
      - SSH_LISTEN_PORT=22
    networks:
      - proxy
    volumes:
      - ./gitea:/data
      - /etc/timezone:/etc/timezone:ro
      - /etc/localtime:/etc/localtime:ro

networks:
  proxy:
    external: false

The HOSTNAME needs to be defined in a corresponding .env file.

After spinning up the stack, Gitea is available on http://hostname/gitea. It's SSH-server will be reachable on port 222.