How to deploy Gitea 1.18.0 in docker swarm behind Caddy v2.6.2

How to deploy Gitea 1.18.0 in docker swarm behind Caddy v2.6.2

Git with a cup of tea. Gitea is a painless self-hosted git service. Gitea is a community managed lightweight code hosting solution written in Go. It is published under the MIT license.

In this post, I am going to show you how to deploy Gitea 1.18.0+rc1 in our Docker Swarm Cluster using the Docker Compose tool behind Caddy 2.6.2

Gitea is a community managed lightweight code hosting solution written in Go. It is published under the MIT license.

Git with a cup of tea, painless self-hosted Git service

If you want to learn more about Gitea, please go through the below links.

  1. Gitea website
  2. Official documentation
  3. GitHub repository

Let’s start with actual deployment…

Prerequisites

Please make sure you should fulfill the below requirements before proceeding to the actual deployment.

  1. Docker Swarm Cluster with GlusterFS as persistent tool.
  2. Caddy as reverse proxy to expose micro-services to external.

Introduction

Gitea is similar to GitHub, Gitlab and Bitbucket. Gitea is a fork of Gogs.

The goal of the Gitea project is to provide the easiest, fastest, and most painless way of setting up a self-hosted Git service. With Go, this can be done platform-independently across all platforms which Go supports, including Linux, macOS, and Windows, on x86, amd64, ARM and PowerPC architectures.

Gitea Features

Gitea has below features

LightweightCross-platformEasy to Install

  • Gitea has low minimal requirements
  • We can run it on an inexpensive Raspberry Pi
  • Save your machine energy
  • Gitea is written in Go
  • It runs on all the platforms and architectures that are supported by Go like Windows, macOS, Linux, ARM, etc
  • Choose the platform you love! and start using it
  • Simply run the binary of your platform
  • Use the Docker Image
  • Install using the packages based on your OS

Persist Gitea Data

Containers are fast to deploy and make efficient use of system resources. Developers get application portability and programmable image management and the operations team gets standard run time units of deployment and management.

With all the known benefits of containers, there is one common misperception that the containers are ephemeral, which means if we restart the container or in case of any issues with it, we lose all the data for that particular container. They are only good for stateless micro-service applications and that it’s not possible to containerize stateful applications.

I am going to use GlusterFS to overcome the ephemeral behavior of Containers.

I already set up a replicated GlusterFS volume to have data replicated throughout the cluster if I would like to have some persistent data.

The below diagram explains how the replicated volume works.

GlusterFS Replicated Volume

Volume will be mounted on all the nodes, and when a file is written to the /mnt partition, data will be replicated to all the nodes in the Cluster

Note

In case of any one of the nodes fails, the application automatically starts on other node without loosing any data and that’s the beauty of the replicated volume.

Persistent application state or data needs to survive application restarts and outages. We are storing the data or state in GlusterFS and had periodic backups performed on it.

Gitea will be available if something goes wrong with any of the nodes on our Docker Swarm Cluster. The data will be available to all the nodes in the cluster because of GlusterFS Replicated Volume.

I am going to create a folder giteadata in /mnt directory to map container volume /data

cd /mnt
sudo mkdir -p giteadata

Tip

Please watch the below video for the GlusterFS Replicated Volume Setup.

Prepare Gitea Environment

I am going to use docker-compose to prepare the environment file for deploying Mattermost. The compose file is known as YAML ( YAML stands for Yet Another Markup Language) and has extension .yml or .yaml

I am going to create application folders in /opt directory on manager node in our docker swarm cluster to store configuration files, nothing but docker compose files (.yml or .yaml).

Also, I am going to use the caddy overlay network created in the previous Caddy post.

Now it’s time to create a folder, gitea in /opt directory to place configuration file, i.e, .yml file for Gitea.

Use the below commands to create the folder.

Go to /opt directory by typing cd /opt in Ubuntu console

make a folder, gitea in /opt with sudo mkdir -p gitea

Let’s get into gitea folder by typing cd gitea

Now create a docker-compose file inside the gitea folder using sudo touch gitea.yml

Open gitea.yml docker-compose file with nano editor using sudo nano gitea.yml and copy and paste the below code in it.

Gitea Docker Compose

Here is the docker-compose file for Gitea. I am going to utilize SQLite as a back-end database for it.

version: "3.7"

services:
  gitea:
    image: gitea/gitea:latest
    volumes:
      - /mnt/gitea:/data
      - /etc/timezone:/etc/timezone:ro
      - /etc/localtime:/etc/localtime:ro
    environment:
      - APP_NAME="Rajasekhar's Gitea"
      - RUN_MODE=prod
      - DOMAIN=gitea.example.com
      - SSH_DOMAIN=gitea.example.com
      - SSH_PORT=2222
      - SSH_LISTERN_PORT=22
      - DISABLE_SSH=false
      - HTTP_PORT=3000
      - ROOT_URL=https://gitea.example.com
      - LFS_START_SERVER=false
      - DB_TYPE=sqlite3
      - DISABLE_REGISTRATION=true
    networks:
      - caddy
    ports:
      - "3000:3000"
      - "2222:22"
    deploy:
      placement:
        constraints: [node.role == worker]
      replicas: 1
      update_config:
        parallelism: 2
        delay: 10s
      restart_policy:
        condition: on-failure
volumes:
  gitea:
    driver: "local"
networks:
  caddy:
    external: true

Tip

Please watch the below video to deploy Gitea in Docker Swarm Cluster.

Caddyfile - Gitea

The Caddyfile is a convenient Caddy configuration format for humans.

Caddyfile is easy to write, easy to understand, and expressive enough for most use cases.

Please find Production-ready Caddyfile for Gitea.

Learn more about Caddyfile here to get familiar with it.

{
    email you@example.com
    default_sni gitea
    cert_issuer acme
    # Production acme directory
    acme_ca https://acme-v02.api.letsencrypt.org/directory
    # Staging acme directory
    #acme_ca https://acme-staging-v02.api.letsencrypt.org/directory
    servers {
        protocol {
            experimental_http3
            allow_h2c
            strict_sni_host
        }
        timeouts {
            read_body   10s
            read_header 10s
            write       10s
            idle        2m
        }
        max_header_size 16384
    }
}
gitea.example.com {
    log {
        output file /var/log/caddy/gitea.log {
        roll_size 20mb
        roll_keep 2
        roll_keep_for 6h
        }
        format console
        level error
    }
    encode gzip zstd
    reverse_proxy gitea:3000
}

Please go to Caddy Post to get more insight to deploy it in the docker swarm cluster.

Final Gitea Docker Compose (Including caddy server configuration)

Please find the full docker-compose file below. You can deploy as many sites as you want using it.

Don’t forget to map site data directories like /mnt/gitea:/data in Caddy configuration caddy.yml.

I already wrote an article Caddy in Docker Swarm. Please go through if you want to learn more.

version: "3.7"

services:
  caddy:
    image: tuneitme/caddy
    ports:
      - "80:80"
      - "443:443"
    networks:
      - caddy
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile
      - /mnt/caddydata:/data
      - /mnt/caddyconfig:/config
      - /mnt/caddylogs:/var/log/caddy
      - /mnt/gitea:/data
    deploy:
      placement:
        constraints:
          - node.role == manager
      replicas: 1
      update_config:
        parallelism: 2
        delay: 10s
      restart_policy:
        condition: on-failure
  gitea:
    image: gitea/gitea:latest
    volumes:
      - /mnt/gitea:/data
      - /etc/timezone:/etc/timezone:ro
      - /etc/localtime:/etc/localtime:ro
    environment:
      - APP_NAME="Rajasekhar's Gitea"
      - RUN_MODE=prod
      - DOMAIN=gitea.example.com
      - SSH_DOMAIN=gitea.example.com
      - SSH_PORT=2222
      - SSH_LISTERN_PORT=22
      - DISABLE_SSH=false
      - HTTP_PORT=3000
      - ROOT_URL=https://gitea.example.com
      - LFS_START_SERVER=false
      - DB_TYPE=sqlite3
      - DISABLE_REGISTRATION=true
    networks:
      - caddy
    ports:
      - "3000:3000"
      - "2222:22"
    deploy:
      placement:
        constraints: [node.role == worker]
      replicas: 1
      update_config:
        parallelism: 2
        delay: 10s
      restart_policy:
        condition: on-failure
volumes:
  caddydata:
    driver: "local"
  caddyconfig:
    driver: "local"
  caddylogs:
    driver: "local"
  gitea:
    driver: "local"
networks:
  caddy:
    external: true

Here I used a custom Caddy docker container with plugins, like Cloudflare DNS, Caddy Auth Portal etc…

Please find the custom caddy docker image below.

Tuneit Caddy Docker Image

Deploy Gitea Stack using Docker Compose

Now it’s time to deploy our docker-compose file above, gitea.yml using the below command

docker stack deploy --compose-file gitea.yml gitea

In the above command, you have to replace gitea.yml with your docker-compose file name and Gitea with whatever name you want to call this particular application.

With docker compose in docker swarm what ever we are deploying is called as docker stack and it has multiple services in it as per the requirement.

As mentioned earlier I named my docker-compose as gitea.yml and named my application stack as gitea

Check the status of the stack by using docker stack ps gitea

Check caddy stack logs using docker service logs gitea_gitea

One thing we observe is that it automatically re-directs to https with Letsencrypt generated certificate. The information is stored in a data directory.

I will be using this caddy stack as a reverse proxy / load balancer for the applications I am going to deploy to Docker Swarm Cluster.

Also I use docker network caddy to access the applications externally.

Access / Install Gitea

Now open any browser and type gitea.example.com to access the site. it will automatically be redirected to https://gitea.example.com (Be sure to replace example.com with your actual domain name).

Warning

Make sure that you have DNS entry for your application (gitea.example.com) in your DNS Management Application.

Please find below images for your reference. Gitea Stack Logs Gitea Initial Configuration Gitea Admin Account Creation Gitea Login Page Gitea Login Page Gitea Home Page Gitea Repository

Deployment of Gitea behind Caddy in our Docker Swarm is successful

If you enjoyed this tutorial, please give your input/thought on it by commenting below. It would help me to bring more articles that focus on Open Source to self-host.

Stay tuned for other deployments in coming posts…🙄