Gitea – A Self-Hosted Private Git Server on Docker

Gitea – A Self-Hosted Private Git Server on Docker

In this tutorial, you’ll be learning how to install Gitea on Docker

What is Gitea ?

  • It is a self-hosted Git Service similar to Github, Bitbucket, etc..
  • It serves as your very own private repository management system
  • It is lightweight, open source and free to use
  • It can be deployed and managed easily with Docker and Docker-Compose

Read more here: https://gitea.io/en-us/

For more blogs, visit: Blogs
For Docker related Blogs, visit: Docker

Pre-requisites

Steps to deploy Gitea on Docker

1. Create a folder to maintain the project and create a docker-compose.yml file

# Create a folder
mkdir gitea

# Navigate into it 
cd gitea

# Create a docker compose file
touch docker-compose.yml

2. Add the contents into the docker-compose.yml file as shown

# Get the UID and GID of the current user by using the below commands
# Note the output of this command, we will be using this in our docker-compose configuration
echo $(id -u) # This is the UID
echo $(id -g) # This is the GID

# Edit the file using the editor of your choice
nano docker-compose.yml

# Paste the below content into it
version: "3"

networks:
    gitea:
        external: false

services:
  server:
    image: gitea/gitea
    container_name: gitea
    environment:
        - USER_UID=1000 # Enter the UID found from previous command output
        - USER_GID=100 # Enter the GID found from previous command output
        - GITEA__database__DB_TYPE=mysql
        - GITEA__database__HOST=db:3306
        - GITEA__database__NAME=gitea
        - GITEA__database__USER=gitea
        - GITEA__database__PASSWD=gitea
    restart: always
    networks:
        - gitea
    volumes:
        - ./gitea:/data
        - /etc/timezone:/etc/timezone:ro
        - /etc/localtime:/etc/localtime:ro
    ports:
        - "3000:3000"
        - "222:22"
    depends_on:
        - db

  db:
    image: mysql
    restart: always
    environment:
        - MYSQL_ROOT_PASSWORD=gitea
        - MYSQL_USER=gitea
        - MYSQL_PASSWORD=gitea
        - MYSQL_DATABASE=gitea
    networks:
        - gitea
    volumes:
        - ./mysql:/var/lib/mysql
# Using this docker-compose configuration we will be deploying two containers which are gitea and mysql
# In order to make the mysql container accessible from the gitea container we need to specify environment variables with the required details
# Notice the Host is specified as "GITEA__database__HOST=db:3306" where "db" is the name of the mysql container i.e, if the name of the container is changed the host environment variable must also be updated accordingly
# All other environment variables are self explanatory where the User name and password needs to be specified for the databse
# The environment variable "USER_UID=1000" and "USER_GID=100" will have the UID and GID of the current user which can be obtained as shown in the previous step
# The port parameter "3000:3000" and "222:22" specifies the port mapping where the left port denotes the host port and the right port denotes the container port
# For the Port 22 notice that the host port is not 22. This is because the SSH port of the host is also 22. To avoid the clash between the host ssh port and container ssh port we need to specify a different port for this
# The volume mounts will create two folders "gitea" and "mysql" inside the project folder created previously
# For these containers a new network called "gitea" will be created and both will be a part of this network
# Save the file

3. Deploy the container with the docker-compose command

# -d runs the container in detached mode
docker-compose up -d

4. Wait for the container to come up and then browse to http://<ip>:<port> where <ip> is the IP of your machine and <port> is the port on which gitea is deployed.

You should now be able to see the “Initial Configuration” screen as shown below. There are only two modifications that are required in this configuration. (Refer to the images below)

  • SSH server domain: Replace localhost with the actual IP of your host
  • Gitea Base URL: Replace localhost with the actual IP of your host
Gitea
Gitea

5. You should now be redirected to the login screen in some time

Gitea

6. You will now need to create a new account to use gitea. Click on the “Register” button on the top right corner of your screen and create an account as shown below

Gitea

7. Now, you will be able to logged in to Gitea with the registered account and you should be able to see your dashboard as shown below

Gitea

Another important configuration needs to be changed which will disable the registration of new users onto your gitea installation. This is explained in the next section

Steps to disable registration

1. Login to the gitea docker container

# Get the ContainerID of the gitea docker container
docker ps

# Login to the container using the below command
docker exec -it <Container_ID> /bin/bash
# Refer to the below images
Gitea
Gitea

2. Once you have logged in to the container, modify the app.ini configuration as shown below

# Edit the app.ini configuration file
# You can use "vi" to edit the file as vi is preinstalled on the container
vi data/gitea/conf/app.ini

# Find the following lines and change the value as shown below
# The default value is false, change it to true
DISABLE_REGISTRATION              = true 
# The default value is true for the below variables, change it to false
ENABLE_OPENID_SIGNIN = false
ENABLE_OPENID_SIGNUP = false

# After making the changes, save the file and restart the docker container
docker restart <Container_ID>

# Refer below images
Gitea
Gitea

For SSH key setup and creation of repository refer here: Gitea – Setup SSH and Repository

Congratulations!! You’ve successfully installed Gitea on Docker

5141cookie-checkGitea – A Self-Hosted Private Git Server on Docker

2 Comments

  1. Pingback:Gitea - Setup SSH and Repository » EasyCode

  2. exactly what I needed to disable registration, awesome content

Leave a Comment

Your email address will not be published. Required fields are marked *