How to Install Gitea Self-hosted Git Service using Docker on Ubuntu 18.04

Gitea is a fork of Gogs, the easy to use self-hosted Git service. It is similar to GitHub, Bitbucket, and Gitlab. Gitea is lightweight code hosting solution written in Go, can run on minimal hardware requirements. It is a cross-platform application, can run anywhere Go can be compiled such as Windows, Linux, MacOS, ARM etc.

In this tutorial, I will show you step-by-step how to install and configure the lightweight Git service using Gitea. We will deploy the Gitea server using Docker and will be using the PostgreSQL database and Traefik Reverse proxy. For this guide, we will be using the latest Ubuntu 18.04 Server.

Prerequisites

  • Ubuntu 18.04
  • Root privileges

What we will do?

  1. Install Docker-CE
  2. Install Docker Compose
  3. Deploy Gitea as Container using Docker
  4. Gitea Post-Installation
  5. Create First Repository
  6. Testing First Commit

Step 1 – Install Docker-CE

The first step we will do for this guide is to install the Docker community edition to the Ubuntu 18.04 server. We will install the Docker CE package from the official Docker repository.

Add the Docker and Docker repository by running commands below.

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

Add Docker Ubuntu Repository

The ‘add-apt-repository’ command will automatically update the repository.

Now install the Docker CE package.

sudo apt policy docker-ce
sudo apt install docker-ce=18.06.1~ce~3-0~ubuntu

install the Docker CE package

When the installation is complete, start the docker service and enable it to launch everytime at system boot.

sudo systemctl start docker
sudo systemctl enable docker

Docker CE is up and running on the Ubuntu 18.04 server. Verify the installation by checking the docker version.

docker version

Check Docker version

or run the docker ‘hello-world’.

docker run hello-world

docker run hello-world

Step 2 – Install Docker Compose

Docker-Compose is a command line tool for defining and managing multi-container docker applications. It allows you to create a container as a service, great for your development, testing and staging environment.

Install Docker Compose by downloading the binary file and make it an executable.

sudo curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Now check the docker-compose command.

docker-compose version

Docker Compose is now installed.

Install Docker Compose

Step 3 – Deploy Gitea as Docker Container

In this step, we will create a new docker-compose script that contains all services configuration including PostgreSQL Database and Traefik reverse proxy. We will set up all deployment files on the directory named ‘deployment’.

Create Custom Docker Network

Before creating the deployment configuration, let’s create a new custom bridge network. It will be used for the external service traefik reverse proxy.

Check the list docker network.

docker network ls

Now create a new custom bridge network named ‘hakasenet’.

docker network create hakasenet

Check it again.

docker network ls

Now you will get ‘hakasenet’ network on the list.

Check docker network

Setup Deployment Directory and Files

Create a new directory named ‘deployment’ and the ‘docker-compose.yml’ script on it.

mkdir deployment; cd deployment
touch docker-compose.yml

Now create new directory ‘gitea’ and ‘postgres’ data volume.

mkdir -p gitea/ postgres/

Create a new file ‘acme.json’ and change the permission of the file. it will be used to store Letsencrypt data.

touch acme.json
chmod 600 acme.json

Now create the global traefik configuration ‘traefik.toml’ file.

touch traefik.toml

And below are files and directories we’ve for the Gitea installation using Docker.

tree

Setup Deployment Directory and Files

Setup PostgreSQL Service

The database service PostgreSQL is the first service that we want to configure. The database service will run only on the internal docker network.

And we will be using the Postgres 9.6 image, using ‘gitea’ as database name, user, and password, and set up the postgres data volume.

Edit the ‘docker-compose.yml’ file using vim.

vim docker-compose.yml

Paste configurations below.

version: "3"

networks:
 hakasenet:
 external: true
 internal:
 external: false

services:
 db:
 image: postgres:9.6
 restart: always
 environment:
 - POSTGRES_USER=gitea
 - POSTGRES_PASSWORD=gitea
 - POSTGRES_DB=gitea
 labels:
 - "traefik.enable=false"
 networks:
 - internal
 volumes:
 - ./postgres:/var/lib/postgresql/data

Save and exit.

Setup Traefik Reverse Proxy

Now we will configure the Traefik reverse proxy for our Gitea installation.

The traefik service will be running under HTTP and HTTPS default ports, running on the custom bridge network named ‘hakasenet’, and we will configure it to use the Letsencrypt certificates that will be defined on the ‘traefik.toml’ file.

Edit the compose script.

vim docker-compose.yml

Paste configurations under the db service configuration.

 traefik:
 image: traefik:latest
 command: --docker
 ports:
 - 80:80
 - 443:443
 labels:
 - "traefik.enable=true"
 - "traefik.backend=dashboard"
 - "traefik.frontend.rule=Host:traefik.hakase-labs.io"
 - "traefik.port=8080"
 networks:
 - hakasenet
 volumes:
 - /var/run/docker.sock:/var/run/docker.sock
 - ./traefik.toml:/traefik.toml
 - ./acme.json:/acme.json
 container_name: traefik
 restart: always

Save and exit.

Now edit the ‘traefik.toml’ configuration file.

vim traefik.toml

And paste the following configuration.

#Traefik Global Configuration
debug = false
checkNewVersion = true
logLevel = "ERROR"

#Define the EntryPoint for HTTP and HTTPS
defaultEntryPoints = ["https","http"]

#Define the HTTP port 80 and
#HTTPS port 443 EntryPoint
#Enable automatically redirect HTTP to HTTPS
[entryPoints]
[entryPoints.http]
address = ":80"
[entryPoints.http.redirect]
entryPoint = "https"
[entryPoints.https]
address = ":443"
[entryPoints.https.tls]

#Enable Traefik Dashboard on port 8080
#with basic authentication method
#hakase and password
[entryPoints.dash]
address=":8080"
[entryPoints.dash.auth]
[entryPoints.dash.auth.basic]
 users = [
 "hakase:$apr1$hEgpZUN2$OYG3KwpzI3T1FqIg9LIbi.",
 ]

[api]
entrypoint="dash"
dashboard = true

#Enable retry sending a request if the network error
[retry]

#Define Docker Backend Configuration
[docker]
endpoint = "unix:///var/run/docker.sock"
domain = "hakase-labs.io"
watch = true
exposedbydefault = false

#Letsencrypt Registration
#Define the Letsencrypt ACME HTTP challenge
[acme]
email = "[email protected]"
storage = "acme.json"
entryPoint = "https"
OnHostRule = true
 [acme.httpChallenge]
 entryPoint = "http"

Save and exit.

Note:

  • Change the acme letsencrypt email with your valid email address.

Setup Gitea Service

Edit the ‘docker-compose.yml’ configuration file.

vim docker-compose.yml

Paste the gitea service configuration at the bottom of the line.

 server:
 image: gitea/gitea:latest
 environment:
 - USER_UID=1000
 - USER_GID=1000
 restart: always
 networks:
 - internal
 volumes:
 - ./gitea:/data
 ports:
 - "3000"
 - "22"
 labels:
 - "traefik.enabled=true"
 - "traefik.backend=gitea"
 - "traefik.frontend.rule=Host:git.hakase-labs.io"
 - "traefik.docker.network=hakasenet"
 - "traefik.port=3000"
 networks:
 - internal
 - hakasenet
 depends_on:
 - db
 - traefik

Save and exit.

The Gitea service will be running on the TCP port ‘3000’, using those two docker networks ‘internal’ and ‘hakasenet’, and will run under the traefik reverse proxy on domain ‘git.hakase-labs.io’.

Docker composes configuration for Gitea deployment has been completed.

Deploy All Services

Now deploy the stack using ‘docker-compose’ command below.

docker-compose up -d

The command will download all docker images needed and run services defined on the docker-compose script.

Deploy All Services

And when it’s complete, check available services on docker using ps option as below.

docker-compose ps

Now you will get the result as below.

docker-compose ps

The PostgreSQL database, Gitea, and traefik reverse proxy containers are now up and running. The database service is running on the default port ‘5432’, the gitea server is running on port ‘3000’, and the traefik proxy is running on HTTP and HTTPS ports, accessible from the outside network/internet.

If you want to check all logs from docker check, run command below.

docker-compose logs

docker-compose logs

Step 4 – Gitea Post-Installation

Open your web browser and type your Gitea URL installation. Mine is:

https://git.hakase-labs.io/

Now you will get the default Gitea page.

Gitea run with Docker

Add the installation path to the URL.

https://git.hakase-labs.io/install

Now you will get the Gitea installation page.

Database configuration

Type the PostgreSQL database details, and use ‘db‘ as a host.

Gitea web installer

General Gitea Configuration

Change the ‘Site title’ with your own title, the ‘SSH Server Domain’ (without https) and ‘Gitea Base URL’ with your gitea server URL.

General Gitea Configuration

Admin Account Settings

On the admin settings, type the admin username, password, and email address.

Admin Account Settings

Now click ‘Install Gitea’ button.

And you will be redirected to the Gitea default user dashboard.

Gitea default user dashboard

Step 5 – Create First Repository in Gitea

On the user home page, click the ‘+’ button.

Now type details of the repository that you want to create.

Add Repository

and click the ‘Create Repository’ button.

The repository has been created.

The repository has been created

Step 6 – Testing First Commit

Back to your terminal shell, and set up the default git user and email.

Run git commands below.

git config --global user.name "hakase"
git config --global user.email "[email protected]"

Now clone the repository.

git clone https://git.hakase-labs.io/hakase/myrepo.git

Go to the repository directory ‘myyrepo’ and edit the README.md file.

cd myrepo/
vim README.md

make changes on the README file, then save and exit.

Now commit the repository.

git add .
git commit -m 'Edit the README.md file by hakase-labs'

And push it to the server.

git push origin master

Type your username and password.

And following should be the result.

Test Git Commit

Check to the repository page and make sure you get the changes as below.

Gitea installed

The Gitea installation with Docker on Ubuntu 18.04 has been completed successfully.

Reference

نوشته های مشابه