2017-11-26 14:44:32 -07:00
---
2020-05-04 02:50:29 -07:00
date: "2020-03-19T19:27:00+02:00"
2017-11-26 14:44:32 -07:00
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-08 15:48:42 -07:00
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-26 14:44:32 -07:00
2018-10-27 20:18:55 -07:00
This reference setup guides users through the setup based on `docker-compose` , but the installation
2019-03-09 14:15:45 -07:00
of `docker-compose` is out of scope of this documentation. To install `docker-compose` itself, follow
2018-01-08 15:48:42 -07:00
the official [install instructions ](https://docs.docker.com/compose/install/ ).
2017-11-26 14:44:32 -07:00
## Basics
2018-01-08 15:48:42 -07:00
The most simple setup just creates a volume and a network and starts the `gitea/gitea:latest`
2019-03-09 14:15:45 -07:00
image as a service. Since there is no database available, one can be initialized using SQLite3.
2018-01-08 15:48:42 -07:00
Create a directory like `gitea` and paste the following content into a file named `docker-compose.yml` .
2018-02-15 18:56:10 -07:00
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-20 18:43:31 -07:00
Also be aware that the tag `:latest` will install the current development version.
2019-08-22 18:55:06 -07:00
For a stable release you can use `:1` or specify a certain release like `:{{< version >}}` .
2017-11-26 14:44:32 -07:00
```yaml
2020-10-08 19:31:07 -07:00
version: "3"
2017-11-26 14:44:32 -07:00
networks:
gitea:
external: false
services:
server:
image: gitea/gitea:latest
2020-10-08 19:31:07 -07:00
container_name: gitea
2018-02-15 18:56:10 -07:00
environment:
- USER_UID=1000
- USER_GID=1000
2017-11-26 14:44:32 -07:00
restart: always
networks:
- gitea
volumes:
- ./gitea:/data
2019-11-13 05:46:46 -07:00
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
2017-11-26 14:44:32 -07:00
ports:
2017-12-04 23:03:40 -07:00
- "3000:3000"
- "222:22"
2017-11-26 14:44:32 -07:00
```
## Custom port
2018-01-08 15:48:42 -07:00
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-26 14:44:32 -07:00
```diff
2020-10-08 19:31:07 -07:00
version: "3"
2017-11-26 14:44:32 -07:00
networks:
gitea:
external: false
services:
server:
image: gitea/gitea:latest
2020-10-08 19:31:07 -07:00
container_name: gitea
2018-02-15 18:56:10 -07:00
environment:
- USER_UID=1000
- USER_GID=1000
2017-11-26 14:44:32 -07:00
restart: always
networks:
- gitea
volumes:
- ./gitea:/data
2019-11-13 05:46:46 -07:00
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
2017-11-26 14:44:32 -07:00
ports:
2017-12-04 23:03:40 -07:00
- - "3000:3000"
- - "222:22"
+ - "8080:3000"
+ - "2221:22"
2017-11-26 14:44:32 -07:00
```
## MySQL database
2018-01-08 15:48:42 -07:00
To start Gitea in combination with a MySQL database, apply these changes to the
`docker-compose.yml` file created above.
2017-11-26 14:44:32 -07:00
```diff
2020-10-08 19:31:07 -07:00
version: "3"
2017-11-26 14:44:32 -07:00
networks:
gitea:
external: false
services:
server:
image: gitea/gitea:latest
2020-10-08 19:31:07 -07:00
container_name: gitea
2018-02-15 18:56:10 -07:00
environment:
- USER_UID=1000
- USER_GID=1000
2018-10-03 07:16:48 -07:00
+ - DB_TYPE=mysql
+ - DB_HOST=db:3306
+ - DB_NAME=gitea
+ - DB_USER=gitea
+ - DB_PASSWD=gitea
2017-11-26 14:44:32 -07:00
restart: always
networks:
- gitea
volumes:
- ./gitea:/data
2019-11-13 05:46:46 -07:00
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
2017-11-26 14:44:32 -07:00
ports:
2017-12-04 23:03:40 -07:00
- "3000:3000"
- "222:22"
2017-11-26 14:44:32 -07:00
+ 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-08 15:48:42 -07:00
To start Gitea in combination with a PostgreSQL database, apply these changes to
the `docker-compose.yml` file created above.
2017-11-26 14:44:32 -07:00
```diff
2020-10-08 19:31:07 -07:00
version: "3"
2017-11-26 14:44:32 -07:00
networks:
gitea:
external: false
services:
server:
image: gitea/gitea:latest
2020-10-08 19:31:07 -07:00
container_name: gitea
2018-02-15 18:56:10 -07:00
environment:
- USER_UID=1000
- USER_GID=1000
2018-10-03 07:16:48 -07:00
+ - DB_TYPE=postgres
+ - DB_HOST=db:5432
+ - DB_NAME=gitea
+ - DB_USER=gitea
+ - DB_PASSWD=gitea
2017-11-26 14:44:32 -07:00
restart: always
networks:
- gitea
volumes:
- ./gitea:/data
2019-11-13 05:46:46 -07:00
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
2018-02-15 18:56:10 -07:00
ports:
- "3000:3000"
- "222:22"
2017-11-26 14:44:32 -07:00
+ 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-08 15:48:42 -07:00
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-15 18:56:10 -07:00
create the required volume. You don't need to worry about permissions with
2019-03-09 14:15:45 -07:00
named volumes; Docker will deal with that automatically.
2017-11-26 14:44:32 -07:00
```diff
2020-10-08 19:31:07 -07:00
version: "3"
2017-11-26 14:44:32 -07:00
networks:
gitea:
external: false
+volumes:
+ gitea:
+ driver: local
+
services:
server:
image: gitea/gitea:latest
2020-10-08 19:31:07 -07:00
container_name: gitea
2017-11-26 14:44:32 -07:00
restart: always
networks:
- gitea
volumes:
- - ./gitea:/data
+ - gitea:/data
2019-11-13 05:46:46 -07:00
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
2017-11-26 14:44:32 -07:00
ports:
2017-12-04 23:03:40 -07:00
- "3000:3000"
- "222:22"
2017-11-26 14:44:32 -07:00
```
2018-01-08 15:48:42 -07:00
MySQL or PostgreSQL containers will need to be created separately.
2017-11-26 14:44:32 -07:00
## Start
2018-01-08 15:48:42 -07:00
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-26 14:44:32 -07:00
2018-01-08 15:48:42 -07:00
To shut down the setup, execute `docker-compose down` . This will stop
and kill the containers. The volumes will still exist.
2017-11-26 14:44:32 -07:00
2018-01-08 15:48:42 -07:00
Notice: if using a non-3000 port on http, change app.ini to match
`LOCAL_ROOT_URL = http://localhost:3000/` .
2017-11-30 04:18:35 -07:00
2017-11-26 14:44:32 -07:00
## Install
2019-03-09 14:15:45 -07:00
After starting the Docker setup via `docker-compose` , Gitea should be available using a
2018-01-08 15:48:42 -07:00
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-09 14:15:45 -07:00
documented above, please note that `db` must be used as the database hostname.
2017-11-26 14:44:32 -07:00
2018-04-16 17:56:28 -07:00
## 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.
* `RUN_MODE` : **dev** : For performance and other purposes, change this to `prod` when deployed to a production environment.
2020-05-04 02:50:29 -07:00
* `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-16 17:56:28 -07:00
* `SSH_PORT` : **22** : SSH port displayed in clone URL.
2019-08-23 16:44:24 -07:00
* `SSH_LISTEN_PORT` : ** %(SSH\_PORT)s**: Port for the built-in SSH server.
2018-04-16 17:56:28 -07:00
* `DISABLE_SSH` : **false** : Disable SSH feature when it's not available.
* `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-23 22:33:57 -07:00
* `LFS_START_SERVER` : **false** : Enables git-lfs support.
2018-04-16 17:56:28 -07:00
* `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-09 14:15:45 -07:00
* `DB_PASSWD` : ** "\<empty>"**: Database user password. Use \`your password\` for quoting if you use special characters in the password.
2018-04-16 17:56:28 -07:00
* `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 08:31:12 -07:00
* `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-23 19:55:48 -07:00
* `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-16 17:56:28 -07:00
2017-11-26 14:44:32 -07:00
# Customization
2018-01-08 15:48:42 -07:00
Customization files described [here ](https://docs.gitea.io/en-us/customizing-gitea/ ) should
2019-03-09 14:15:45 -07:00
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-08 15:48:42 -07:00
`/var/lib/docker/volumes/gitea_gitea/_data` . The configuration file will be saved at
`/data/gitea/conf/app.ini` after the installation.
2018-11-25 18:51:02 -07:00
# Upgrading
2019-04-05 08:01:38 -07:00
:exclamation::exclamation: **Make sure you have volumed data to somewhere outside Docker container** :exclamation::exclamation:
2018-11-25 18:51:02 -07:00
To upgrade your installation to the latest release:
```
# 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 10:53:23 -07:00
# SSH Container Passthrough
Since SSH is running inside the container, you'll have to pass SSH from the host to the
container if you wish to use SSH support. If you wish to do this without running the container
2019-03-09 14:15:45 -07:00
SSH on a non-standard port (or move your host port to a non-standard port), you can forward
2019-01-05 10:53:23 -07:00
SSH connections destined for the container with a little extra setup.
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`. You should also create the directory
`/var/lib/gitea` on the host, owned by the `git` user and mounted in the container, e.g.
```
2020-10-08 19:31:07 -07:00
version: "3"
2019-01-05 10:53:23 -07:00
services:
server:
image: gitea/gitea:latest
2020-10-08 19:31:07 -07:00
container_name: gitea
2019-01-05 10:53:23 -07:00
environment:
- USER_UID=1000
- USER_GID=1000
restart: always
networks:
- gitea
volumes:
- /var/lib/gitea:/data
2019-11-13 05:46:46 -07:00
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
2019-01-05 10:53:23 -07:00
ports:
- "3000:3000"
- "127.0.0.1:2222:22"
```
You can see that we're also exposing the container SSH port to port 2222 on the host, and binding this
to 127.0.0.1 to prevent it being accessible external to the host machine itself.
On the **host** , you should create the file `/app/gitea/gitea` with the following contents and
make it executable (`chmod +x /app/gitea/gitea`):
```
#!/bin/sh
ssh -p 2222 -o StrictHostKeyChecking=no git@127.0.0.1 "SSH_ORIGINAL_COMMAND=\"$SSH_ORIGINAL_COMMAND\" $0 $@"
```
Your `git` user needs to have an SSH key generated:
```
sudo -u git ssh-keygen -t rsa -b 4096 -C "Gitea Host Key"
```
2020-10-04 16:52:40 -07:00
Now, proceed with one of the points given below:
- symlink the container `.ssh/authorized_keys` file to your git user `.ssh/authorized_keys` .
2019-01-05 10:53:23 -07:00
This can be done on the host as the `/var/lib/gitea` directory is mounted inside the container under `/data` :
```
ln -s /var/lib/gitea/git/.ssh/authorized_keys /home/git/.ssh/authorized_keys
```
Then echo the `git` user SSH key into the authorized_keys file so the host can talk to the container over SSH:
```
echo "no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty $(cat /home/git/.ssh/id_rsa.pub)" >> /var/lib/gitea/git/.ssh/authorized_keys
```
2020-10-04 16:52:40 -07:00
Lastly, Gitea makes `authorized_keys` backups by default. This could be a problem
as the symbolic link made to `authorized_keys` previously could end up pointing
to an old backup. To resolve this, please put the following into your Gitea
config:
```
[ssh]
SSH_BACKUP_AUTHORIZED_KEYS=false
```
- mount your `.ssh` directory directly into the container i.e. add the
following to the `volumes` section of your Docker container config:
```
- /home/git/.ssh/:/data/git/.ssh/
```
2019-01-05 10:53:23 -07:00
Now you should be able to use Git over SSH to your container without disrupting SSH access to the host.
Please note: SSH container passthrough will work only if using opensshd in container, and will not work if
`AuthorizedKeysCommand` is used in combination with setting `SSH_CREATE_AUTHORIZED_KEYS_FILE=false` to disable
authorized files key generation.