# Gitea with docker-compose

Ensure you have installed docker-compose (see instructions for [Raspian](../raspbian/README.md)) and have added the user you wish to start gitea services to the `docker` group. To add the current user: `sudo usermod -aG docker $USER`, or specify the user you desire. On Raspian you can use the default user `pi`. Re-login, or simply restart to make the changes effective.

This setup uses systemd to run a docker-compose file that starts gitea with a PostgreSQL database. You will also need to define directories to store the contents of the postgres data, and gitea data such as config and git repositories. You will likely want to store these directories on a [USB drive](../hardware/usb-flash-drive.md), or somewhere other than the Micro SD Card hosting the operating system. 

## Create data directories

Create the following directories in a convenient location (the paths are up to you). For the rest of the insructions, you will substitute the path any time it appears in curly braces.

| Path           | Example                                 |
|----------------|-----------------------------------------|
| gitea-data     | /media/my-usb-drive/gitea/data          |
| gitea-app      | /media/my-usb-drive/gitea/app/data      | 
| gitea-postgres | /media/my-usb-drive/gitea/postgres/data |
| lollipop-home  | /home/me/lollipop 

Ensure the user that is going to run docker-compose to launch the gitea services has both read and write permissions on these directories. To ensure correct ownership is assigned to the application config at `{gitea-data}/gitea/conf/app.ini` you may need to create an empty file before starting the docker services (`touch {gitea-data}/gitea/conf/app.ini`).

## Define services with docker-compose

Next create the following file in your `{lollipop-home}` directory of the user that will launch docker-compose. Replace all the values in curly-braces with a path from the table above and create a secretkey for your installation using an string of characters you prefer.

/home/pi/lollipop/docker-compose.lollipop-gitea.yml:

```
version: "2"

networks:
  gitea:

services:
  server:
    image: registry.lollipopcloud.solutions/arm32v7/gitea:latest
    environment:
      - USER_UID=1000
      - USER_GID=1000
      - DB_TYPE=postgres
      - DB_HOST=db:5432
      - DB_NAME=gitea
      - DB_USER=gitea
      - DB_PASSWD=gitea
      - SECRET_KEY="{secretkey}"
    restart: always
    networks:
      - gitea
    volumes:
      - {gitea-data}:/data
      - {gitea-app}:/app/gitea/data
    ports:
      - "3000:3000"
      - "222:22"
    depends_on:
      - db

  db:
    image: postgres:9.6
    restart: always
    environment:
      - POSTGRES_USER=gitea
      - POSTGRES_PASSWORD=gitea
      - POSTGRES_DB=gitea
    networks:
      - gitea
    volumes:
      - {gitea-postgres}:/var/lib/postgresql/data
```

The file above instructs docker-compose to launch two services: a gitea server and a postgres server upon which it depends. The postgres image used is a standard image supplied on the main docker registry. We use the gitea image provided by the Lollipop Cloud project. The docker-compose file creates a virtual network for these services to communicate. For further information on the environment variables please see [gitea installation with docker](https://docs.gitea.io/en-us/install-with-docker/) and [postgres docker](https://docs.docker.com/samples/library/postgres/) documentation.

## First time configuration

Now would be a good time to test your installation: `docker-compose -f {lollipop-home}/docker-compose.lollipop-gitea.yml up` should start your services and gitea should be running on port 3000.

Register an initial account. You will be prompted to confirm initial config. Ensure the database is set to _PostgreSQL_, the host and port are 'db:5432', and enter the password from the docker-compose config above. Set a host name for SSH and HTTPS.  

If everything is good, shutdown your services with: `docker-compose -f {lollipop-home}/docker-compose.lollipop-gitea.yml down`.
  
##  Start at boot

Next we will setup systemd to automatically launch gitea on boot, and generally provide a simple interface to manage starting, stopping and checking the status of the services. Create the following file:

/etc/systemd/system/lollipop-gitea.service:

```
[Unit]
Description=Run Lollipop services for gitea 
After=network.target

[Service]
Type=simple
ExecStart=/usr/bin/docker-compose -f docker-compose.lollipop-gitea.yml up
ExecStop=/usr/bin/docker-compose -f docker-compose.lollipop-gitea.yml down
WorkingDirectory={lollipop-home}
User=pi
Group=pi

[Install]
WantedBy=multi-user.target
```

You will need to inform systemd of a new service by running: `sudo systemctl daemon-reload`. Then you must enable the service by running `sudo systemctl enable lollipop-gitea.service'.

Start the service for the first time by running `sudo systemctl start lollipop-gitea.service`, and check the status by running `sudo systemctl status lollipop-gitea.service'. Reboot your computer with `sudo reboot` and your service should start during the boot sequence.

## Further configuration

[https://git.lollipopcloud.solutions/lollipop-docker/gitea](https://git.lollipopcloud.solutions/lollipop-docker/gitea) has some good examples on how to integrate Matamo, Fathom, create custom labels and more.

[https://docs.gitea.io/en-us/](https://docs.gitea.io/en-us/) also has great information on how to further configure Gitea.
