2017-11-27 03:14:32 +05:30
---
2020-05-04 15:20:29 +05:30
date: "2020-03-19T19:27:00+02:00"
2017-11-27 03:14:32 +05:30
title: "Installation with Docker"
slug: "install-with-docker"
weight: 10
toc: true
draft: false
menu:
sidebar:
parent: "installation"
name: "With Docker"
weight: 10
identifier: "install-with-docker"
---
# Installation with Docker
2018-01-09 04:18:42 +05:30
Gitea provides automatically updated Docker images within its Docker Hub organization. It is
possible to always use the latest stable tag or to use another service that handles updating
Docker images.
2017-11-27 03:14:32 +05:30
2018-10-28 08:48:55 +05:30
This reference setup guides users through the setup based on `docker-compose` , but the installation
2019-03-10 02:45:45 +05:30
of `docker-compose` is out of scope of this documentation. To install `docker-compose` itself, follow
2018-01-09 04:18:42 +05:30
the official [install instructions ](https://docs.docker.com/compose/install/ ).
2017-11-27 03:14:32 +05:30
## Basics
2018-01-09 04:18:42 +05:30
The most simple setup just creates a volume and a network and starts the `gitea/gitea:latest`
2019-03-10 02:45:45 +05:30
image as a service. Since there is no database available, one can be initialized using SQLite3.
2018-01-09 04:18:42 +05:30
Create a directory like `gitea` and paste the following content into a file named `docker-compose.yml` .
2018-02-16 07:26:10 +05:30
Note that the volume should be owned by the user/group with the UID/GID specified in the config file.
If you don't give the volume correct permissions, the container may not start.
2018-09-21 07:13:31 +05:30
Also be aware that the tag `:latest` will install the current development version.
2019-08-23 07:25:06 +05:30
For a stable release you can use `:1` or specify a certain release like `:{{< version >}}` .
2017-11-27 03:14:32 +05:30
```yaml
2020-10-09 08:01:07 +05:30
version: "3"
2017-11-27 03:14:32 +05:30
networks:
gitea:
external: false
services:
server:
2020-11-19 22:06:48 +05:30
image: gitea/gitea:{{< version > }}
2020-10-09 08:01:07 +05:30
container_name: gitea
2018-02-16 07:26:10 +05:30
environment:
- USER_UID=1000
- USER_GID=1000
2017-11-27 03:14:32 +05:30
restart: always
networks:
- gitea
volumes:
- ./gitea:/data
2019-11-13 18:16:46 +05:30
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
2017-11-27 03:14:32 +05:30
ports:
2017-12-05 11:33:40 +05:30
- "3000:3000"
- "222:22"
2017-11-27 03:14:32 +05:30
```
## Custom port
2018-01-09 04:18:42 +05:30
To bind the integrated openSSH daemon and the webserver on a different port, adjust
the port section. It's common to just change the host port and keep the ports within
the container like they are.
2017-11-27 03:14:32 +05:30
```diff
2020-10-09 08:01:07 +05:30
version: "3"
2017-11-27 03:14:32 +05:30
networks:
gitea:
external: false
services:
server:
2020-11-19 22:06:48 +05:30
image: gitea/gitea:{{< version > }}
2020-10-09 08:01:07 +05:30
container_name: gitea
2018-02-16 07:26:10 +05:30
environment:
- USER_UID=1000
- USER_GID=1000
2017-11-27 03:14:32 +05:30
restart: always
networks:
- gitea
volumes:
- ./gitea:/data
2019-11-13 18:16:46 +05:30
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
2017-11-27 03:14:32 +05:30
ports:
2017-12-05 11:33:40 +05:30
- - "3000:3000"
- - "222:22"
+ - "8080:3000"
+ - "2221:22"
2017-11-27 03:14:32 +05:30
```
## MySQL database
2018-01-09 04:18:42 +05:30
To start Gitea in combination with a MySQL database, apply these changes to the
`docker-compose.yml` file created above.
2017-11-27 03:14:32 +05:30
```diff
2020-10-09 08:01:07 +05:30
version: "3"
2017-11-27 03:14:32 +05:30
networks:
gitea:
external: false
services:
server:
2020-11-19 22:06:48 +05:30
image: gitea/gitea:{{< version > }}
2020-10-09 08:01:07 +05:30
container_name: gitea
2018-02-16 07:26:10 +05:30
environment:
- USER_UID=1000
- USER_GID=1000
2018-10-03 19:46:48 +05:30
+ - DB_TYPE=mysql
+ - DB_HOST=db:3306
+ - DB_NAME=gitea
+ - DB_USER=gitea
+ - DB_PASSWD=gitea
2017-11-27 03:14:32 +05:30
restart: always
networks:
- gitea
volumes:
- ./gitea:/data
2019-11-13 18:16:46 +05:30
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
2017-11-27 03:14:32 +05:30
ports:
2017-12-05 11:33:40 +05:30
- "3000:3000"
- "222:22"
2017-11-27 03:14:32 +05:30
+ depends_on:
+ - db
+
+ db:
+ image: mysql:5.7
+ restart: always
+ environment:
+ - MYSQL_ROOT_PASSWORD=gitea
+ - MYSQL_USER=gitea
+ - MYSQL_PASSWORD=gitea
+ - MYSQL_DATABASE=gitea
+ networks:
+ - gitea
+ volumes:
+ - ./mysql:/var/lib/mysql
```
## PostgreSQL database
2018-01-09 04:18:42 +05:30
To start Gitea in combination with a PostgreSQL database, apply these changes to
the `docker-compose.yml` file created above.
2017-11-27 03:14:32 +05:30
```diff
2020-10-09 08:01:07 +05:30
version: "3"
2017-11-27 03:14:32 +05:30
networks:
gitea:
external: false
services:
server:
2020-11-19 22:06:48 +05:30
image: gitea/gitea:{{< version > }}
2020-10-09 08:01:07 +05:30
container_name: gitea
2018-02-16 07:26:10 +05:30
environment:
- USER_UID=1000
- USER_GID=1000
2018-10-03 19:46:48 +05:30
+ - DB_TYPE=postgres
+ - DB_HOST=db:5432
+ - DB_NAME=gitea
+ - DB_USER=gitea
+ - DB_PASSWD=gitea
2017-11-27 03:14:32 +05:30
restart: always
networks:
- gitea
volumes:
- ./gitea:/data
2019-11-13 18:16:46 +05:30
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
2018-02-16 07:26:10 +05:30
ports:
- "3000:3000"
- "222:22"
2017-11-27 03:14:32 +05:30
+ depends_on:
+ - db
+
+ db:
+ image: postgres:9.6
+ restart: always
+ environment:
+ - POSTGRES_USER=gitea
+ - POSTGRES_PASSWORD=gitea
+ - POSTGRES_DB=gitea
+ networks:
+ - gitea
+ volumes:
+ - ./postgres:/var/lib/postgresql/data
```
## Named volumes
2018-01-09 04:18:42 +05:30
To use named volumes instead of host volumes, define and use the named volume
within the `docker-compose.yml` configuration. This change will automatically
2018-02-16 07:26:10 +05:30
create the required volume. You don't need to worry about permissions with
2019-03-10 02:45:45 +05:30
named volumes; Docker will deal with that automatically.
2017-11-27 03:14:32 +05:30
```diff
2020-10-09 08:01:07 +05:30
version: "3"
2017-11-27 03:14:32 +05:30
networks:
gitea:
external: false
+volumes:
+ gitea:
+ driver: local
+
services:
server:
2020-11-19 22:06:48 +05:30
image: gitea/gitea:{{< version > }}
2020-10-09 08:01:07 +05:30
container_name: gitea
2017-11-27 03:14:32 +05:30
restart: always
networks:
- gitea
volumes:
- - ./gitea:/data
+ - gitea:/data
2019-11-13 18:16:46 +05:30
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
2017-11-27 03:14:32 +05:30
ports:
2017-12-05 11:33:40 +05:30
- "3000:3000"
- "222:22"
2017-11-27 03:14:32 +05:30
```
2018-01-09 04:18:42 +05:30
MySQL or PostgreSQL containers will need to be created separately.
2017-11-27 03:14:32 +05:30
## Start
2018-01-09 04:18:42 +05:30
To start this setup based on `docker-compose` , execute `docker-compose up -d` ,
to launch Gitea in the background. Using `docker-compose ps` will show if Gitea
started properly. Logs can be viewed with `docker-compose logs` .
2017-11-27 03:14:32 +05:30
2018-01-09 04:18:42 +05:30
To shut down the setup, execute `docker-compose down` . This will stop
and kill the containers. The volumes will still exist.
2017-11-27 03:14:32 +05:30
2018-01-09 04:18:42 +05:30
Notice: if using a non-3000 port on http, change app.ini to match
`LOCAL_ROOT_URL = http://localhost:3000/` .
2017-11-30 16:48:35 +05:30
2017-11-27 03:14:32 +05:30
## Install
2019-03-10 02:45:45 +05:30
After starting the Docker setup via `docker-compose` , Gitea should be available using a
2018-01-09 04:18:42 +05:30
favorite browser to finalize the installation. Visit http://server-ip:3000 and follow the
installation wizard. If the database was started with the `docker-compose` setup as
2019-03-10 02:45:45 +05:30
documented above, please note that `db` must be used as the database hostname.
2017-11-27 03:14:32 +05:30
2018-04-17 06:26:28 +05:30
## Environments variables
You can configure some of Gitea's settings via environment variables:
(Default values are provided in **bold** )
* `APP_NAME` : ** "Gitea: Git with a cup of tea"**: Application name, used in the page title.
2020-12-01 01:22:04 +05:30
* `RUN_MODE` : **prod** : Application run mode, affects performance and debugging. Either "dev", "prod" or "test".
2020-05-04 15:20:29 +05:30
* `DOMAIN` : **localhost** : Domain name of this server, used for the displayed http clone URL in Gitea's UI.
* `SSH_DOMAIN` : **localhost** : Domain name of this server, used for the displayed ssh clone URL in Gitea's UI. If the install page is enabled, SSH Domain Server takes DOMAIN value in the form (which overwrite this setting on save).
2018-04-17 06:26:28 +05:30
* `SSH_PORT` : **22** : SSH port displayed in clone URL.
2019-08-24 05:14:24 +05:30
* `SSH_LISTEN_PORT` : ** %(SSH\_PORT)s**: Port for the built-in SSH server.
2020-10-22 09:54:20 +05:30
* `DISABLE_SSH` : **false** : Disable SSH feature when it's not available. If you want to disable SSH feature, you should set SSH port to `0` when installing Gitea.
2018-04-17 06:26:28 +05:30
* `HTTP_PORT` : **3000** : HTTP listen port.
* `ROOT_URL` : ** ""**: Overwrite the automatically generated public URL. This is useful if the internal and the external URL don't match (e.g. in Docker).
2019-06-24 11:03:57 +05:30
* `LFS_START_SERVER` : **false** : Enables git-lfs support.
2018-04-17 06:26:28 +05:30
* `DB_TYPE` : **sqlite3** : The database type in use \[mysql, postgres, mssql, sqlite3\].
* `DB_HOST` : **localhost:3306** : Database host address and port.
* `DB_NAME` : **gitea** : Database name.
* `DB_USER` : **root** : Database username.
2019-03-10 02:45:45 +05:30
* `DB_PASSWD` : ** "\<empty>"**: Database user password. Use \`your password\` for quoting if you use special characters in the password.
2018-04-17 06:26:28 +05:30
* `INSTALL_LOCK` : **false** : Disallow access to the install page.
* `SECRET_KEY` : ** ""**: Global secret key. This should be changed. If this has a value and `INSTALL_LOCK` is empty, `INSTALL_LOCK` will automatically set to `true` .
2018-05-23 21:01:12 +05:30
* `DISABLE_REGISTRATION` : **false** : Disable registration, after which only admin can create accounts for users.
* `REQUIRE_SIGNIN_VIEW` : **false** : Enable this to force users to log in to view any page.
2018-06-24 08:25:48 +05:30
* `USER_UID` : **1000** : The UID (Unix user ID) of the user that runs Gitea within the container. Match this to the UID of the owner of the `/data` volume if using host volumes (this is not necessary with named volumes).
* `USER_GID` : **1000** : The GID (Unix group ID) of the user that runs Gitea within the container. Match this to the GID of the owner of the `/data` volume if using host volumes (this is not necessary with named volumes).
2018-04-17 06:26:28 +05:30
2017-11-27 03:14:32 +05:30
# Customization
2018-01-09 04:18:42 +05:30
Customization files described [here ](https://docs.gitea.io/en-us/customizing-gitea/ ) should
2019-03-10 02:45:45 +05:30
be placed in `/data/gitea` directory. If using host volumes, it's quite easy to access these
files; for named volumes, this is done through another container or by direct access at
2018-01-09 04:18:42 +05:30
`/var/lib/docker/volumes/gitea_gitea/_data` . The configuration file will be saved at
`/data/gitea/conf/app.ini` after the installation.
2018-11-26 07:21:02 +05:30
# Upgrading
2019-04-05 20:31:38 +05:30
:exclamation::exclamation: **Make sure you have volumed data to somewhere outside Docker container** :exclamation::exclamation:
2018-11-26 07:21:02 +05:30
To upgrade your installation to the latest release:
2020-12-03 04:53:54 +05:30
```bash
2018-11-26 07:21:02 +05:30
# Edit `docker-compose.yml` to update the version, if you have one specified
# Pull new images
docker-compose pull
# Start a new container, automatically removes old one
docker-compose up -d
```
2019-01-05 23:23:23 +05:30
# SSH Container Passthrough
2020-12-03 04:53:54 +05:30
Since SSH is running inside the container, SSH needs to be passed through from the host to the container if SSH support is desired. One option would be to run the container SSH on a non-standard port (or moving the host port to a non-standard port). Another option which might be more straightforward is to forward SSH connections from the host to the container. This setup is explained in the following.
2019-01-05 23:23:23 +05:30
2020-12-03 04:53:54 +05:30
This guide assumes that you have created a user on the host called `git` which shares the same `UID` / `GID` as the container values `USER_UID` / `USER_GID` . These values can be set as environment variables in the `docker-compose.yml` :
2019-01-05 23:23:23 +05:30
2020-12-03 04:53:54 +05:30
```bash
environment:
- USER_UID=1000
- USER_GID=1000
2019-01-05 23:23:23 +05:30
```
2020-10-09 08:01:07 +05:30
2020-12-03 04:53:54 +05:30
Next mount `/home/git/.ssh` of the host into the container. Otherwise the SSH authentication cannot work inside the container.
2019-01-05 23:23:23 +05:30
2020-12-03 04:53:54 +05:30
```bash
volumes:
- /home/git/.ssh/:/data/git/.ssh
```
2019-01-05 23:23:23 +05:30
2020-12-03 04:53:54 +05:30
Now a SSH key pair needs to be created on the host. This key pair will be used to authenticate the `git` user on the host to the container.
2019-01-05 23:23:23 +05:30
2020-12-03 04:53:54 +05:30
```bash
sudo -u git ssh-keygen -t rsa -b 4096 -C "Gitea Host Key"
2019-01-05 23:23:23 +05:30
```
2020-12-03 04:53:54 +05:30
In the next step a file named `/app/gitea/gitea` (with executable permissions) needs to be created on the host. This file will issue the SSH forwarding from the host to the container. Add the following contents to `/app/gitea/gitea` :
```bash
2019-01-05 23:23:23 +05:30
ssh -p 2222 -o StrictHostKeyChecking=no git@127.0.0.1 "SSH_ORIGINAL_COMMAND=\"$SSH_ORIGINAL_COMMAND\" $0 $@"
```
2020-12-03 04:53:54 +05:30
To make the forwarding work, the SSH port of the container (22) needs to be mapped to the host port 2222 in `docker-compose.yml` . Since this port does not need to be exposed to the outside world, it can be mapped to the `localhost` of the host machine:
2019-01-05 23:23:23 +05:30
2020-12-03 04:53:54 +05:30
```bash
ports:
# [...]
- "127.0.0.1:2222:22"
2019-01-05 23:23:23 +05:30
```
2020-10-05 05:22:40 +05:30
2020-12-03 04:53:54 +05:30
In addition, `/home/git/.ssh/authorized_keys` on the host needs to be modified. It needs to act in the same way as `authorized_keys` within the Gitea container. Therefore add
2019-01-05 23:23:23 +05:30
2020-12-03 04:53:54 +05:30
```bash
command="/app/gitea/gitea --config=/data/gitea/conf/app.ini serv key-1",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa < YOUR_SSH_PUBKEY >
2019-01-05 23:23:23 +05:30
```
2020-12-03 04:53:54 +05:30
and replace `<YOUR_SSH_PUBKEY>` with a valid SSH public key of yours.
2019-01-05 23:23:23 +05:30
2020-12-03 04:53:54 +05:30
In addition the public key of the `git` user on the host needs to be added to `/home/git/.ssh/authorized_keys` so authentication against the container can succeed: `echo "$(cat /home/git/.ssh/id_rsa.pub)" >> /home/git/.ssh/authorized_keys` .
2019-01-05 23:23:23 +05:30
2020-12-03 04:53:54 +05:30
Here is a detailed explanation what is happening when a SSH request is made:
2020-10-05 05:22:40 +05:30
2020-12-03 04:53:54 +05:30
1. A SSH request is made against the host using the `git` user, e.g. `git clone git@domain:user/repo.git` .
2. In `/home/git/.ssh/authorized_keys` , the command executes the `/app/gitea/gitea` script.
3. `/app/gitea/gitea` forwards the SSH request to port 2222 which is mapped to the SSH port (22) of the container.
4. Due to the existence of the public key of the `git` user in `/home/git/.ssh/authorized_keys` the authentication host → container succeeds and the SSH request get forwarded to Gitea running in the docker container.
2020-10-05 05:22:40 +05:30
2020-12-03 04:53:54 +05:30
If a new SSH key is added in the Gitea web interface, it will be appended to `.ssh/authorized_keys` in the same way as the already existing key.
2020-10-05 05:22:40 +05:30
2020-12-03 04:53:54 +05:30
**Notes**
2020-10-05 05:22:40 +05:30
2020-12-03 04:53:54 +05:30
SSH container passthrough will work only if
2019-01-05 23:23:23 +05:30
2020-12-03 04:53:54 +05:30
- `opensshd` is used in the container
- if `AuthorizedKeysCommand` is _not used_ in combination with `SSH_CREATE_AUTHORIZED_KEYS_FILE=false` to disable authorized files key generation
- `LOCAL_ROOT_URL` is not changed