Running Gitea using Docker

In this tutorial, we will be showing you how you can run Gitea on your machine using Docker.

Running Gitea using Docker Compose

Gitea is what is called a forge software package. It allows you to host your own Git version control with many features offered by platforms such as GitHub and Bitbucket. For example, it has bug tracking, code review, continuous integration, kanban boards, and more,

One key benefit of using Gitea is that it allows anyone to self-host their service. This allows you to remain completely in control of your code.

Docker is a great way to run Gitea because it makes the whole installation and update process a breeze. In a few short commands, you can have your own self-hosted Git version control up and running.

Thanks to both Docker and Gitea being relatively resource light, you can run this project on almost any device. You can even run Gitea on an ARM device like the Raspberry Pi.

Installing Gitea using Docker Compose

In the following sections, we will walk you through the very simple steps to get Gitea up and running using a Docker Compose file.

These steps should work for any Linux-based operating system where you can install and run Docker.

Preparing your Device to run Gitea using Docker

1. Before we can run Gitea on your device using it’s Docker container, you will obviously need to install Docker itself.

You can follow our guide on installing Docker on Linux to get the latest version of the runtime. Newer versions offer improved functionality and better performance.

https://pimylifeup.com/linux-docker-install/

2. Once you have installed Docker, we can move on to creating a folder to store the Docker Compose file for Gitea. It is also the same directory from which we will get the software to store all its data.

You can create this directory by using the mkdir command within the terminal.

sudo mkdir -p /opt/stacks/gitea/

3. After creating a directory to store Gitea, let us change in to it by using the cd command.

cd /opt/stacks/gitea

Writing a Docker Compose File for Gitea

4. Now that we are in the right place, we can move on and begin writing a Docker Compose file for Gitea. This Compose file will do a few things. The first is that it will set up Gitea itself, and the second is that it will also spin up a MySQL server container that Gitea will use to store its information.

We can begin writing this Compose file by using the nano text editor. You can use any editor, but nano is one of the easiest to use for beginners.

sudo nano compose.yaml

5. Within this Compose file, you will want to enter the following lines. There is nothing you need to adjust here unless you need to change the ports that Gitea runs off of.

With this configuration, Gitea will operate its web interface on port 3000 and its SSH server on port 222.

As we aren’t exposing any of the MySQL databases ports, only the Gitea server should be able to access it.

services:
  server:
    image: gitea/gitea:latest
    container_name: gitea
    environment:
      - USER_UID=1000
      - USER_GID=1000
      - 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:8
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=gitea
      - MYSQL_USER=gitea
      - MYSQL_PASSWORD=gitea
      - MYSQL_DATABASE=gitea
    networks:
      - gitea
    volumes:
      - ./mysql:/var/lib/mysql

networks:
  gitea:
    external: false

6. Once you have finished writing the Compose file, you can save and quit by pressing CTRL + X, Y, and then ENTER.

Starting up your Gitea Docker Container

7. All you need to do now to start up the Gitea docker container on your Linux system is to run the following command.

Docker will then interpret the Compose file and start up the database server and then Gitea. This process can take a couple of minutes if you have a slower internet connection as Docker will need to download the containers.

docker compose up -d 

8. If everything has worked properly, you can now access your new Gitea server by visiting its web interface.

If you don’t know your server’s IP, you can often get it by using the hostname command.

hostname -I

9. The Gitea web interface should be reachable through your machine’s IP address, followed by port 3000.

Replace “<IPADDRESS>” with the IP of your device.

http://<IPADDRESS>:3000

10. Once you have accessed this URL, you must follow some additional setup steps before your Gitea Docker container is fully up and running.

Most of these options you won’t need to change as they are automatically being handled by the container or have their settings automatically passed in such as the database settings.

11. After completing the initial set up steps, you must register an account with your Gitea installation.

The first user will become the software’s administrator. You should be able to find the register screen by clicking at the top right corner of the web interface.

How to Update Gitea using Docker

One key advantage of using Docker to run Gitea on your machine is that it makes the update process relatively easy. With Gitea pushing out new stable releases relatively often, being able to upgrade easily and quickly is crucial.

Over the next few short steps, we will show you how easy Docker makes the update process.

1. Your first step is to change to the directory where we wrote the Docker Compose file for Gitea earlier in the tutorial.

You can use the command below to change to the correct directory.

cd /opt/stacks/gitea

2. Our first step is getting Docker to pull the latest version of the Gitea and MySQL docker containers.

Docker makes this process easy; we only need to use the following command. With this command, Docker will read the Compose file and check if it has the latest version of each container. If a newer version is available, it will download it.

docker compose pull

3. While the previous command downloaded a new version of the Gitea container, it would not have automatically updated the currently running one.

Luckily, Docker Compose again makes this process really straightforward. By running the command below, Docker will automatically restart any container that has a new image available.

docker compose up -d

Conclusion

Hopefully, at this stage, you should have successfully set Gitea up and running using its official Docker container.

Gitea is a fantastic self-hosted solution for storing and managing your source code. It has many of the features of cloud services, such as GitHub, but you can remain completely in control of where your code is hosted and processed.

Please comment below if you have had any issues getting this Git server to work using Docker.

If you found this tutorial helpful, we recommend you explore our many other Docker guides.

Leave a Reply

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