How to Install Gitea using Docker on Ubuntu 20.04

Gitea is a free and open-source software package for self-hosting a Git server. It also offers collaborative features like bug tracking, wikis, and code review. Gitea is a community-driven and lightweight code solution written in Go.

Developers need to regularly merge their code changes into a central repository when working. It can happen that you need to have a private central repository for your team that you will host and manage by yourself. You can use Gitea for this purpose. It is similar to GitHub, Bitbucket, and so on.

In this tutorial, we learn how to install Gitea using docker on Ubuntu 20.04.

Prerequisites

  • A node installed with Ubuntu 20.04
  • A user with sudo priviledge

Step 1: Install Docker on Ubuntu

Gitea provides the latest stable version of its Docker images from the Docker hub. It can be installed from source, binary, and as a package as well. Here we are deploying as a Docker image.

To install docker, you will need to install some pre-requisites:

$ sudo apt install apt-transport-https ca-certificates curl software-properties-common

Now let's add the GP key of the docker repository:

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Now add Docker to the APT sources. It will automatically update the cache.

$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"

Now install docker using the following command:

$ sudo apt install docker-ce

Add the user to the docker group so that can use the docker command without sudo privileges:

$ sudo usermod -aG docker username

Step 2: Run Nginx docker

To enable HTTPS you can use TLS termination proxies such as Nginx, Apache2, or Caddy. In our case, we will run an independent container as our Nginx reverse proxy.

$ docker run -d --name nginx -p 80:80 -p 443:443 -v /etc/nginx/htpasswd:/etc/nginx/htpasswd -v /etc/nginx/vhost.d:/etc/nginx/vhost.d:ro -v /etc/nginx/certs:/etc/nginx/certs -v /var/run/docker.sock:/tmp/docker.sock:ro etopian/nginx-proxy

It will help us to redirect all the default HTTP traffic to HTTPS when you integrate your certificates. You should notice the mapped volume to integrate your certificates which is /etc/nginx/certs

Now open the port 80 and 443 on the firewall:

$ sudo ufw allow 80,443/tcp

Also, make sure to open ssh before enabling UFW if not yet

$ sudo ufw allow 'OpenSSH'

Now enable UFW if not yet

$ sudo ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup

Step 3: Run MySQL docker

Gitea needs a database to save its data. Gitea supports MySQL/MariaDB, PostgreSQL, SQLite, and MSSQL. Here we will run a MySQL container to keep all the data.

$ docker run -d --name mysql-gitea -e MYSQL_ROOT_PASSWORD=o$su876HG@zvsRt3BT -v /opt/docker-volume/mysql-gitea:/var/lib/mysql mysql:5.7

We can check if our container is running

$ docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED       STATUS          PORTS                                                                      NAMES
6d41fec8b0e4   mysql:5.7             "docker-entrypoint.s…"   2 hours ago   Up 2 hours      3306/tcp, 33060/tcp                                                        mysql-gitea
ec9d83a3944e   etopian/nginx-proxy   "forego start -r"        3 days ago    Up 24 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp   nginx

The Gitea by default listens for connections on port 3000 and reverser proxy on HTTP and HTTPS ports. If you have a firewall make sure ports are open to access the Gitea web interface.

Let's have a look at some details of our MySQL container such as the IP address because we will need it later

$ docker inspect mysql-gitea
[
    {
        "Id": "6d41fec8b0e4b6ca465444d5cfea11913decddfd7586f4796702463cf08897fe",
        "Created": "2021-10-21T23:48:20.709365573Z",
...
...
        "Networks": {
                        "bridge": {
                            "IPAMConfig": null,
                            "Links": null,
                            "Aliases": null,
                            "NetworkID": "3f0b32613606db9e0761af15b7fa5d7f7c3b86f4d3b4668d574c579b9468915f",
                            "EndpointID": "f029fc1797c70d090da19d30cb31ca34c1b6baf4537a807397e931f87262f867",
                            "Gateway": "172.17.0.1",
                            "IPAddress": "172.17.0.3",
                            "IPPrefixLen": 16,
                            "IPv6Gateway": "",
                            "GlobalIPv6Address": "",
                            "GlobalIPv6PrefixLen": 0,
                            "MacAddress": "02:42:ac:11:00:03",
                            "DriverOpts": null
                        }
...
...

Now let's connect to the MySQL container

$ docker container exec -it mysql-gitea bash

Then access to MySQL command line

# mysql -u root -po$su876HG@zvsRt3BT

Now create the database and user for Gitea

mysql> CREATE USER 'gitea-user'@'%' IDENTIFIED BY '34@zv$TKji@s097BB';
mysql> CREATE DATABASE giteadb;
mysql> GRANT ALL PRIVILEGES ON giteadb.* TO 'gitea-user'@'%';
mysql> FLUSH PRIVILEGES;
mysql> exit;

Now exit also the container

# exit;

Step 4: Configure the SSL certificate

In order to access your Gitea through a secure domain name, you will need to copy the SSL certificate to the Nginx folder. You can use the free Let's Encrypt certificate or certificate from a certificate authority.

Copy the certificate

$ cp gitea.websitefortesting.com.crt /etc/nginx/certs/

Copy the key

$ cp gitea.websitefortesting.com.key /etc/nginx/certs/

Step 5: Run Gitea docker

To run Gitea, we need to indicate some information about the MySQL container that has been launched before such as:

  • The type of database which is mysql
  • the address ip of the mysql container
  • the name of the database
  • the database user name
  • the database user password
  • the port used by gitea
$ docker run -d --name gitea-selfhosted -v /opt/docker-volume/gitea-selfhosted:/data -p 3000:3000 -e VIRTUAL_HOST=gitea.websitefortesting.com -e VIRTUAL_PORT=3000 -e USER_UID=1001 -e USER_GID=1001 -e DB_TYPE=mysql -e DB_HOST=172.17.0.3:3306 -e DB_NAME=giteadb -e DB_USER=gitea-user -e DB_PASSWD=34@zv$TKji@s097BB gitea/gitea:1.8

We can check if it's running

$ docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED          STATUS          PORTS                                                                      NAMES
321b870a0735   gitea/gitea:1.8       "/usr/bin/entrypoint…"   11 seconds ago   Up 10 seconds   22/tcp, 0.0.0.0:3000->3000/tcp, :::3000->3000/tcp                          gitea-selfhosted
6d41fec8b0e4   mysql:5.7             "docker-entrypoint.s…"   2 hours ago      Up 3 minutes    3306/tcp, 33060/tcp                                                        mysql-gitea
ec9d83a3944e   etopian/nginx-proxy   "forego start -r"        3 days ago       Up 29 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp   nginx

Now you can try accessing Gitea by using the URL https://giteadomain.com

Gitea web interface home page

Now go to "sign in" in order to continue the installation process. You can personalize some information like the site title.

Gitea web interface  database settings

Make sure to indicate the domain name where it's necessary. Indicate also the information for the administrator account

Gitea web interface  domain and admin account settings

After the installation, you will be log in

Gitea dashboard

You can create a new repository

create new repository

You have the first repository

Gitea first repo

Conclusion

Now you know all the steps to install Gitea using Docker on Ubuntu 20.04 - your private central repository for your code. You will be able to manage it depending on your needs.

Leave a Comment