You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

with-docker-rootless.en-us.md 12KB

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