code.gitea.io/gitea@v1.22.3/docs/content/installation/with-docker-rootless.en-us.md (about)

     1  ---
     2  date: "2020-02-09T20:00:00+02:00"
     3  title: "Installation with Docker (rootless)"
     4  slug: "install-with-docker-rootless"
     5  sidebar_position: 60
     6  toc: false
     7  draft: false
     8  aliases:
     9    - /en-us/install-with-docker-rootless
    10  menu:
    11    sidebar:
    12      parent: "installation"
    13      name: "With Docker Rootless"
    14      sidebar_position: 60
    15      identifier: "install-with-docker-rootless"
    16  ---
    17  
    18  # Installation with Docker
    19  
    20  Gitea provides automatically updated Docker images within its Docker Hub organization. It is
    21  possible to always use the latest stable tag or to use another service that handles updating
    22  Docker images.
    23  
    24  The rootless image uses Gitea internal SSH to provide Git protocol and doesn't support OpenSSH.
    25  
    26  This reference setup guides users through the setup based on `docker-compose`, but the installation
    27  of `docker-compose` is out of scope of this documentation. To install `docker-compose` itself, follow
    28  the official [install instructions](https://docs.docker.com/compose/install/).
    29  
    30  ## Basics
    31  
    32  The most simple setup just creates a volume and a network and starts the `gitea/gitea:latest-rootless`
    33  image as a service. Since there is no database available, one can be initialized using SQLite3.
    34  
    35  Create a directory for `data` and `config`:
    36  
    37  ```sh
    38  mkdir -p gitea/{data,config}
    39  cd gitea
    40  touch docker-compose.yml
    41  ```
    42  
    43  Then paste the following content into a file named `docker-compose.yml`:
    44  
    45  ```yaml
    46  version: "2"
    47  
    48  services:
    49    server:
    50      image: gitea/gitea:@version@-rootless
    51      restart: always
    52      volumes:
    53        - ./data:/var/lib/gitea
    54        - ./config:/etc/gitea
    55        - /etc/timezone:/etc/timezone:ro
    56        - /etc/localtime:/etc/localtime:ro
    57      ports:
    58        - "3000:3000"
    59        - "2222:2222"
    60  ```
    61  
    62  Note that the volume should be owned by the user/group with the UID/GID specified in the config file. By default Gitea in docker will use uid:1000 gid:1000. If needed you can set ownership on those folders with the command:
    63  
    64  ```sh
    65  sudo chown 1000:1000 config/ data/
    66  ```
    67  
    68  > If you don't give the volume correct permissions, the container may not start.
    69  
    70  For a stable release you could use `:latest-rootless`, `:1-rootless` or specify a certain release like `:@version@-rootless`, but if you'd like to use the latest development version then `:nightly-rootless` would be an appropriate tag. If you'd like to run the latest commit from a release branch you can use the `:1.x-nightly-rootless` tag, where x is the minor version of Gitea. (e.g. `:1.16-nightly-rootless`)
    71  
    72  ## Custom port
    73  
    74  To bind the integrated ssh and the webserver on a different port, adjust
    75  the port section. It's common to just change the host port and keep the ports within
    76  the container like they are.
    77  
    78  ```diff
    79  version: "2"
    80  
    81  services:
    82    server:
    83      image: gitea/gitea:@version@-rootless
    84      restart: always
    85      volumes:
    86        - ./data:/var/lib/gitea
    87        - ./config:/etc/gitea
    88        - /etc/timezone:/etc/timezone:ro
    89        - /etc/localtime:/etc/localtime:ro
    90      ports:
    91  -      - "3000:3000"
    92  -      - "2222:2222"
    93  +      - "80:3000"
    94  +      - "22:2222"
    95  ```
    96  
    97  ## MySQL database
    98  
    99  To start Gitea in combination with a MySQL database, apply these changes to the
   100  `docker-compose.yml` file created above.
   101  
   102  ```diff
   103  version: "2"
   104  
   105  services:
   106    server:
   107      image: gitea/gitea:@version@-rootless
   108  +    environment:
   109  +      - GITEA__database__DB_TYPE=mysql
   110  +      - GITEA__database__HOST=db:3306
   111  +      - GITEA__database__NAME=gitea
   112  +      - GITEA__database__USER=gitea
   113  +      - GITEA__database__PASSWD=gitea
   114      restart: always
   115      volumes:
   116        - ./data:/var/lib/gitea
   117        - ./config:/etc/gitea
   118        - /etc/timezone:/etc/timezone:ro
   119        - /etc/localtime:/etc/localtime:ro
   120      ports:
   121        - "3000:3000"
   122        - "2222:2222"
   123  +    depends_on:
   124  +      - db
   125  +
   126  +  db:
   127  +    image: mysql:8
   128  +    restart: always
   129  +    environment:
   130  +      - MYSQL_ROOT_PASSWORD=gitea
   131  +      - MYSQL_USER=gitea
   132  +      - MYSQL_PASSWORD=gitea
   133  +      - MYSQL_DATABASE=gitea
   134  +    volumes:
   135  +      - ./mysql:/var/lib/mysql
   136  ```
   137  
   138  ## PostgreSQL database
   139  
   140  To start Gitea in combination with a PostgreSQL database, apply these changes to
   141  the `docker-compose.yml` file created above.
   142  
   143  ```diff
   144  version: "2"
   145  
   146  services:
   147    server:
   148      image: gitea/gitea:@version@-rootless
   149      environment:
   150  +      - GITEA__database__DB_TYPE=postgres
   151  +      - GITEA__database__HOST=db:5432
   152  +      - GITEA__database__NAME=gitea
   153  +      - GITEA__database__USER=gitea
   154  +      - GITEA__database__PASSWD=gitea
   155      restart: always
   156      volumes:
   157        - ./data:/var/lib/gitea
   158        - ./config:/etc/gitea
   159        - /etc/timezone:/etc/timezone:ro
   160        - /etc/localtime:/etc/localtime:ro
   161      ports:
   162        - "3000:3000"
   163        - "2222:2222"
   164  +    depends_on:
   165  +      - db
   166  +
   167  +  db:
   168  +    image: postgres:14
   169  +    restart: always
   170  +    environment:
   171  +      - POSTGRES_USER=gitea
   172  +      - POSTGRES_PASSWORD=gitea
   173  +      - POSTGRES_DB=gitea
   174  +    volumes:
   175  +      - ./postgres:/var/lib/postgresql/data
   176  ```
   177  
   178  ## Named volumes
   179  
   180  To use named volumes instead of host volumes, define and use the named volume
   181  within the `docker-compose.yml` configuration. This change will automatically
   182  create the required volume. You don't need to worry about permissions with
   183  named volumes; Docker will deal with that automatically.
   184  
   185  ```diff
   186  version: "2"
   187  
   188  +volumes:
   189  +  gitea-data:
   190  +    driver: local
   191  +  gitea-config:
   192  +    driver: local
   193  +
   194  services:
   195    server:
   196      image: gitea/gitea:@version@-rootless
   197      restart: always
   198      volumes:
   199  -      - ./data:/var/lib/gitea
   200  +      - gitea-data:/var/lib/gitea
   201  -      - ./config:/etc/gitea
   202  +      - gitea-config:/etc/gitea
   203        - /etc/timezone:/etc/timezone:ro
   204        - /etc/localtime:/etc/localtime:ro
   205      ports:
   206        - "3000:3000"
   207        - "2222:2222"
   208  ```
   209  
   210  MySQL or PostgreSQL containers will need to be created separately.
   211  
   212  ## Custom user
   213  
   214  You can choose to use a custom user (following --user flag definition https://docs.docker.com/engine/reference/run/#user).
   215  As an example to clone the host user `git` definition use the command `id -u git` and add it to `docker-compose.yml` file:
   216  Please make sure that the mounted folders are writable by the user.
   217  
   218  ```diff
   219  version: "2"
   220  
   221  services:
   222    server:
   223      image: gitea/gitea:@version@-rootless
   224      restart: always
   225  +    user: 1001
   226      volumes:
   227        - ./data:/var/lib/gitea
   228        - ./config:/etc/gitea
   229        - /etc/timezone:/etc/timezone:ro
   230        - /etc/localtime:/etc/localtime:ro
   231      ports:
   232        - "3000:3000"
   233        - "2222:2222"
   234  ```
   235  
   236  ## Start
   237  
   238  To start this setup based on `docker-compose`, execute `docker-compose up -d`,
   239  to launch Gitea in the background. Using `docker-compose ps` will show if Gitea
   240  started properly. Logs can be viewed with `docker-compose logs`.
   241  
   242  To shut down the setup, execute `docker-compose down`. This will stop
   243  and kill the containers. The volumes will still exist.
   244  
   245  Notice: if using a non-3000 port on http, change app.ini to match
   246  `LOCAL_ROOT_URL = http://localhost:3000/`.
   247  
   248  ## Install
   249  
   250  After starting the Docker setup via `docker-compose`, Gitea should be available using a
   251  favorite browser to finalize the installation. Visit http://server-ip:3000 and follow the
   252  installation wizard. If the database was started with the `docker-compose` setup as
   253  documented above, please note that `db` must be used as the database hostname.
   254  
   255  # Customization
   256  
   257  Customization files described [here](administration/customizing-gitea.md) should
   258  be placed in `/var/lib/gitea/custom` directory. If using host volumes, it's quite easy to access these
   259  files; for named volumes, this is done through another container or by direct access at
   260  `/var/lib/docker/volumes/gitea_gitea/_/var_lib_gitea`. The configuration file will be saved at
   261  `/etc/gitea/app.ini` after the installation.
   262  
   263  # Upgrading
   264  
   265  :exclamation::exclamation: **Make sure you have volumed data to somewhere outside Docker container** :exclamation::exclamation:
   266  
   267  To upgrade your installation to the latest release:
   268  
   269  ```
   270  # Edit `docker-compose.yml` to update the version, if you have one specified
   271  # Pull new images
   272  docker-compose pull
   273  # Start a new container, automatically removes old one
   274  docker-compose up -d
   275  ```
   276  
   277  # Upgrading from standard image
   278  
   279  - Backup your setup
   280  - Change volume mountpoint from /data to /var/lib/gitea
   281  - If you used a custom app.ini move it to a new volume mounted to /etc/gitea
   282  - Rename folder (inside volume) gitea to custom
   283  - Edit app.ini if needed
   284    - Set START_SSH_SERVER = true
   285  - Use image gitea/gitea:@version@-rootless
   286  
   287  ## Managing Deployments With Environment Variables
   288  
   289  In addition to the environment variables above, any settings in `app.ini` can be set
   290  or overridden with an environment variable of the form: `GITEA__SECTION_NAME__KEY_NAME`.
   291  These settings are applied each time the docker container starts, and won't be passed into Gitea's sub-processes.
   292  Full information [here](https://github.com/go-gitea/gitea/tree/main/contrib/environment-to-ini).
   293  
   294  These environment variables can be passed to the docker container in `docker-compose.yml`.
   295  The following example will enable a smtp mail server if the required env variables
   296  `GITEA__mailer__FROM`, `GITEA__mailer__HOST`, `GITEA__mailer__PASSWD` are set on the host
   297  or in a `.env` file in the same directory as `docker-compose.yml`.
   298  
   299  The settings can be also set or overridden with the content of a file by defining an environment variable of the form:
   300  `GITEA__section_name__KEY_NAME__FILE` that points to a file.
   301  
   302  ```bash
   303  ...
   304  services:
   305    server:
   306      environment:
   307        - GITEA__mailer__ENABLED=true
   308        - GITEA__mailer__FROM=${GITEA__mailer__FROM:?GITEA__mailer__FROM not set}
   309        - GITEA__mailer__PROTOCOL=smtp
   310        - GITEA__mailer__HOST=${GITEA__mailer__HOST:?GITEA__mailer__HOST not set}
   311        - GITEA__mailer__IS_TLS_ENABLED=true
   312        - GITEA__mailer__USER=${GITEA__mailer__USER:-apikey}
   313        - GITEA__mailer__PASSWD="""${GITEA__mailer__PASSWD:?GITEA__mailer__PASSWD not set}"""
   314  ```
   315  
   316  To set required TOKEN and SECRET values, consider using Gitea's built-in [generate utility functions](administration/command-line.md#generate).
   317  
   318  # SSH Container Passthrough
   319  
   320  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 commands from the host to the container. This setup is explained in the following.
   321  
   322  This guide assumes that you have created a user on the host called `git` with permission to run `docker exec`, and that the Gitea container is called `gitea`. You will need to modify that user's shell to forward the commands to the `sh` executable inside the container, using `docker exec`.
   323  
   324  First, create the file `/usr/local/bin/gitea-shell` on the host, with the following contents:
   325  
   326  ```bash
   327  #!/bin/sh
   328  /usr/bin/docker exec -i --env SSH_ORIGINAL_COMMAND="$SSH_ORIGINAL_COMMAND" gitea sh "$@"
   329  ```
   330  
   331  Note that `gitea` in the docker command above is the name of the container. If you named yours differently, don't forget to change that.
   332  
   333  You should also make sure that you’ve set the permissions of the shell wrapper correctly:
   334  
   335  ```bash
   336  sudo chmod +x /usr/local/bin/gitea-shell
   337  ```
   338  
   339  Once the wrapper is in place, you can make it the shell for the `git` user:
   340  
   341  ```bash
   342  sudo usermod -s /usr/local/bin/gitea-shell git
   343  ```
   344  
   345  Now that all the SSH commands are forwarded to the container, you need to set up the SSH authentication on the host. This is done by leveraging the [SSH AuthorizedKeysCommand](administration/command-line.md#keys) to match the keys against those accepted by Gitea. Add the following block to `/etc/ssh/sshd_config`, on the host:
   346  
   347  ```bash
   348  Match User git
   349    AuthorizedKeysCommandUser git
   350    AuthorizedKeysCommand /usr/bin/docker exec -i gitea /usr/local/bin/gitea keys -c /etc/gitea/app.ini -e git -u %u -t %t -k %k
   351  ```
   352  
   353  (From 1.16.0 you will not need to set the `-c /etc/gitea/app.ini` option.)
   354  
   355  All that is left to do is restart the SSH server:
   356  
   357  ```bash
   358  sudo systemctl restart sshd
   359  ```
   360  
   361  **Notes**
   362  
   363  This isn't actually using the docker SSH - it is simply using the commands around it.
   364  You could theoretically not run the internal SSH server.