Gitea
Setting Up Gitea in a Docker Container and Securing it with Nginx
Gitea is a lightweight, self-hosted Git service that can be easily deployed in a Docker container. In this post, we’ll go through the process of setting up Gitea in a Docker container and securing it with Nginx as a reverse proxy.
Prerequisites
First, you need to install Docker on your server. If you are not familiar with Docker, check out a previous post to get started and install the required software.
Second, if you want to access Gitea remotely over HTTPS, you need to set up a DNS record that points to the system where Gitea is running. You can name the hostname anything but this example will be gitea.example.com
.
Next, create a directory to store persistent data for the Gitea container.
mkdir -p gitea
Create a Docker Container for Gitea
Create a Docker container for Gitea. Note this will use a sqlite database which is fine for testing and a single user but for any environment with multiple users, check out using mySQL as an alternative.
docker run
You can create the container with the docker CLI command.
|
|
This command creates a new Docker container named gitea
, maps the container’s SSH port 22 to the host’s port 10022, and the HTTP port 3000 to the host’s port 10080.
You can stop here and access your Gitea instance using the hostname or IP of the system where the container was run and the port 10080. If using your local system, you can navigate to this URL:
Cleanup this container by entering the command:
|
|
The container image and volume can be cleaned by entering
|
|
Now if you want to use HTTPS, a reverse proxy can be used to encrypt traffic from the network to the Gitea container.
Install and Configure Nginx with Gitea
This example uses Nginx to proxy requests to Gitea and secure using SSL/TLS encryption via HTTPS. If you are not familiar with Nginx, check out a previous post to learn more.
First, create a docker-compose.yml
file with the following contents:
|
|
This is defining two services in the Docker Compose stack: nginx and gitea. The nginx service uses the official Nginx image with Alpine Linux, and exposes port 80 and 443 to the host machine. It also depends on the gitea service, which means that it will wait for Gitea to be started before starting itself.
Next, create an Nginx configuration file called nginx.conf
in the same directory as your docker-compose.yml
file:
|
|
In this configuration Nginx will listen on port 443 for requests to gitea.example.com
. Nginx will use the private key and certificate key specified under ssl_certificate
and ssl_certificate_key
. Providing a certificate is out of scope for my post but if you are interested in how to create a wildcard certificate for a domain that you own, check out a previous post where I set one up for free.
Create a directory to store certificates and mount them in the Nginx container:
mkdir -p certs
We then define a server block with the domain name and port for our reverse proxy. The location /
block specifies that all incoming requests should be proxied to Gitea, using the proxy_pass
directive. We also set headers in the proxy_set_header
directives to preserve the original hostname and IP address of the client.
Start your Docker Compose stack by running docker-compose up -d
in your terminal. Once the containers are started, you can test your reverse proxy configuration by making requests to https://gitea.example.com/ from a web browser or command line tool like curl
. The request should be routed to Gitea, and you should see the Gitea homepage displayed in your browser.
You now have a reverse proxy set up using Nginx that routes incoming requests to Gitea. You can repeat this process for other services in your Docker Compose stack to create a complete reverse proxy solution.
For more information about how to configure Gitea, check out the official documentation.