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

with-docker.en-us.md 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. ---
  2. date: "2020-03-19T19:27:00+02:00"
  3. title: "Installation with Docker"
  4. slug: "install-with-docker"
  5. weight: 10
  6. toc: false
  7. draft: false
  8. menu:
  9. sidebar:
  10. parent: "installation"
  11. name: "With Docker"
  12. weight: 10
  13. identifier: "install-with-docker"
  14. ---
  15. # Installation with Docker
  16. Gitea provides automatically updated Docker images within its Docker Hub organization. It is
  17. possible to always use the latest stable tag or to use another service that handles updating
  18. Docker images.
  19. This reference setup guides users through the setup based on `docker-compose`, but the installation
  20. of `docker-compose` is out of scope of this documentation. To install `docker-compose` itself, follow
  21. the official [install instructions](https://docs.docker.com/compose/install/).
  22. **Table of Contents**
  23. {{< toc >}}
  24. ## Basics
  25. The most simple setup just creates a volume and a network and starts the `gitea/gitea:latest`
  26. image as a service. Since there is no database available, one can be initialized using SQLite3.
  27. Create a directory like `gitea` and paste the following content into a file named `docker-compose.yml`.
  28. Note that the volume should be owned by the user/group with the UID/GID specified in the config file.
  29. If you don't give the volume correct permissions, the container may not start.
  30. Also be aware that the tag `:latest` will install the current development version.
  31. For a stable release you can use `:1` or specify a certain release like `:{{< version >}}`.
  32. ```yaml
  33. version: "3"
  34. networks:
  35. gitea:
  36. external: false
  37. services:
  38. server:
  39. image: gitea/gitea:{{< version >}}
  40. container_name: gitea
  41. environment:
  42. - USER_UID=1000
  43. - USER_GID=1000
  44. restart: always
  45. networks:
  46. - gitea
  47. volumes:
  48. - ./gitea:/data
  49. - /etc/timezone:/etc/timezone:ro
  50. - /etc/localtime:/etc/localtime:ro
  51. ports:
  52. - "3000:3000"
  53. - "222:22"
  54. ```
  55. ## Ports
  56. To bind the integrated openSSH daemon and the webserver on a different port, adjust
  57. the port section. It's common to just change the host port and keep the ports within
  58. the container like they are.
  59. ```diff
  60. version: "3"
  61. networks:
  62. gitea:
  63. external: false
  64. services:
  65. server:
  66. image: gitea/gitea:{{< version >}}
  67. container_name: gitea
  68. environment:
  69. - USER_UID=1000
  70. - USER_GID=1000
  71. restart: always
  72. networks:
  73. - gitea
  74. volumes:
  75. - ./gitea:/data
  76. - /etc/timezone:/etc/timezone:ro
  77. - /etc/localtime:/etc/localtime:ro
  78. ports:
  79. - - "3000:3000"
  80. - - "222:22"
  81. + - "8080:3000"
  82. + - "2221:22"
  83. ```
  84. ## Databases
  85. ### MySQL database
  86. To start Gitea in combination with a MySQL database, apply these changes to the
  87. `docker-compose.yml` file created above.
  88. ```diff
  89. version: "3"
  90. networks:
  91. gitea:
  92. external: false
  93. services:
  94. server:
  95. image: gitea/gitea:{{< version >}}
  96. container_name: gitea
  97. environment:
  98. - USER_UID=1000
  99. - USER_GID=1000
  100. + - DB_TYPE=mysql
  101. + - DB_HOST=db:3306
  102. + - DB_NAME=gitea
  103. + - DB_USER=gitea
  104. + - DB_PASSWD=gitea
  105. restart: always
  106. networks:
  107. - gitea
  108. volumes:
  109. - ./gitea:/data
  110. - /etc/timezone:/etc/timezone:ro
  111. - /etc/localtime:/etc/localtime:ro
  112. ports:
  113. - "3000:3000"
  114. - "222:22"
  115. + depends_on:
  116. + - db
  117. +
  118. + db:
  119. + image: mysql:5.7
  120. + restart: always
  121. + environment:
  122. + - MYSQL_ROOT_PASSWORD=gitea
  123. + - MYSQL_USER=gitea
  124. + - MYSQL_PASSWORD=gitea
  125. + - MYSQL_DATABASE=gitea
  126. + networks:
  127. + - gitea
  128. + volumes:
  129. + - ./mysql:/var/lib/mysql
  130. ```
  131. ### PostgreSQL database
  132. To start Gitea in combination with a PostgreSQL database, apply these changes to
  133. the `docker-compose.yml` file created above.
  134. ```diff
  135. version: "3"
  136. networks:
  137. gitea:
  138. external: false
  139. services:
  140. server:
  141. image: gitea/gitea:{{< version >}}
  142. container_name: gitea
  143. environment:
  144. - USER_UID=1000
  145. - USER_GID=1000
  146. + - DB_TYPE=postgres
  147. + - DB_HOST=db:5432
  148. + - DB_NAME=gitea
  149. + - DB_USER=gitea
  150. + - DB_PASSWD=gitea
  151. restart: always
  152. networks:
  153. - gitea
  154. volumes:
  155. - ./gitea:/data
  156. - /etc/timezone:/etc/timezone:ro
  157. - /etc/localtime:/etc/localtime:ro
  158. ports:
  159. - "3000:3000"
  160. - "222:22"
  161. + depends_on:
  162. + - db
  163. +
  164. + db:
  165. + image: postgres:9.6
  166. + restart: always
  167. + environment:
  168. + - POSTGRES_USER=gitea
  169. + - POSTGRES_PASSWORD=gitea
  170. + - POSTGRES_DB=gitea
  171. + networks:
  172. + - gitea
  173. + volumes:
  174. + - ./postgres:/var/lib/postgresql/data
  175. ```
  176. ## Named volumes
  177. To use named volumes instead of host volumes, define and use the named volume
  178. within the `docker-compose.yml` configuration. This change will automatically
  179. create the required volume. You don't need to worry about permissions with
  180. named volumes; Docker will deal with that automatically.
  181. ```diff
  182. version: "3"
  183. networks:
  184. gitea:
  185. external: false
  186. +volumes:
  187. + gitea:
  188. + driver: local
  189. +
  190. services:
  191. server:
  192. image: gitea/gitea:{{< version >}}
  193. container_name: gitea
  194. restart: always
  195. networks:
  196. - gitea
  197. volumes:
  198. - - ./gitea:/data
  199. + - gitea:/data
  200. - /etc/timezone:/etc/timezone:ro
  201. - /etc/localtime:/etc/localtime:ro
  202. ports:
  203. - "3000:3000"
  204. - "222:22"
  205. ```
  206. MySQL or PostgreSQL containers will need to be created separately.
  207. ## Startup
  208. To start this setup based on `docker-compose`, execute `docker-compose up -d`,
  209. to launch Gitea in the background. Using `docker-compose ps` will show if Gitea
  210. started properly. Logs can be viewed with `docker-compose logs`.
  211. To shut down the setup, execute `docker-compose down`. This will stop
  212. and kill the containers. The volumes will still exist.
  213. Notice: if using a non-3000 port on http, change app.ini to match
  214. `LOCAL_ROOT_URL = http://localhost:3000/`.
  215. ## Installation
  216. After starting the Docker setup via `docker-compose`, Gitea should be available using a
  217. favorite browser to finalize the installation. Visit http://server-ip:3000 and follow the
  218. installation wizard. If the database was started with the `docker-compose` setup as
  219. documented above, please note that `db` must be used as the database hostname.
  220. ## Environment variables
  221. You can configure some of Gitea's settings via environment variables:
  222. (Default values are provided in **bold**)
  223. - `APP_NAME`: **"Gitea: Git with a cup of tea"**: Application name, used in the page title.
  224. - `RUN_MODE`: **prod**: Application run mode, affects performance and debugging. Either "dev", "prod" or "test".
  225. - `DOMAIN`: **localhost**: Domain name of this server, used for the displayed http clone URL in Gitea's UI.
  226. - `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).
  227. - `SSH_PORT`: **22**: SSH port displayed in clone URL.
  228. - `SSH_LISTEN_PORT`: **%(SSH_PORT)s**: Port for the built-in SSH server.
  229. - `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.
  230. - `HTTP_PORT`: **3000**: HTTP listen port.
  231. - `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).
  232. - `LFS_START_SERVER`: **false**: Enables git-lfs support.
  233. - `DB_TYPE`: **sqlite3**: The database type in use \[mysql, postgres, mssql, sqlite3\].
  234. - `DB_HOST`: **localhost:3306**: Database host address and port.
  235. - `DB_NAME`: **gitea**: Database name.
  236. - `DB_USER`: **root**: Database username.
  237. - `DB_PASSWD`: **"\<empty>"**: Database user password. Use \`your password\` for quoting if you use special characters in the password.
  238. - `INSTALL_LOCK`: **false**: Disallow access to the install page.
  239. - `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`.
  240. - `DISABLE_REGISTRATION`: **false**: Disable registration, after which only admin can create accounts for users.
  241. - `REQUIRE_SIGNIN_VIEW`: **false**: Enable this to force users to log in to view any page.
  242. - `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).
  243. - `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).
  244. ## Customization
  245. Customization files described [here](https://docs.gitea.io/en-us/customizing-gitea/) should
  246. be placed in `/data/gitea` directory. If using host volumes, it's quite easy to access these
  247. files; for named volumes, this is done through another container or by direct access at
  248. `/var/lib/docker/volumes/gitea_gitea/_data`. The configuration file will be saved at
  249. `/data/gitea/conf/app.ini` after the installation.
  250. ## Upgrading
  251. :exclamation::exclamation: **Make sure you have volumed data to somewhere outside Docker container** :exclamation::exclamation:
  252. To upgrade your installation to the latest release:
  253. ```bash
  254. # Edit `docker-compose.yml` to update the version, if you have one specified
  255. # Pull new images
  256. docker-compose pull
  257. # Start a new container, automatically removes old one
  258. docker-compose up -d
  259. ```
  260. ## SSH Container Passthrough
  261. 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.
  262. 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`:
  263. ```bash
  264. environment:
  265. - USER_UID=1000
  266. - USER_GID=1000
  267. ```
  268. Next mount `/home/git/.ssh` of the host into the container. Otherwise the SSH authentication cannot work inside the container.
  269. ```bash
  270. volumes:
  271. - /home/git/.ssh/:/data/git/.ssh
  272. ```
  273. 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.
  274. ```bash
  275. sudo -u git ssh-keygen -t rsa -b 4096 -C "Gitea Host Key"
  276. ```
  277. 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`:
  278. ```bash
  279. ssh -p 2222 -o StrictHostKeyChecking=no git@127.0.0.1 "SSH_ORIGINAL_COMMAND=\"$SSH_ORIGINAL_COMMAND\" $0 $@"
  280. ```
  281. 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:
  282. ```bash
  283. ports:
  284. # [...]
  285. - "127.0.0.1:2222:22"
  286. ```
  287. 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
  288. ```bash
  289. 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>
  290. ```
  291. and replace `<YOUR_SSH_PUBKEY>` with a valid SSH public key of yours.
  292. 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`.
  293. Here is a detailed explanation what is happening when a SSH request is made:
  294. 1. A SSH request is made against the host using the `git` user, e.g. `git clone git@domain:user/repo.git`.
  295. 2. In `/home/git/.ssh/authorized_keys` , the command executes the `/app/gitea/gitea` script.
  296. 3. `/app/gitea/gitea` forwards the SSH request to port 2222 which is mapped to the SSH port (22) of the container.
  297. 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.
  298. 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.
  299. **Notes**
  300. SSH container passthrough will work only if
  301. - `opensshd` is used in the container
  302. - if `AuthorizedKeysCommand` is _not used_ in combination with `SSH_CREATE_AUTHORIZED_KEYS_FILE=false` to disable authorized files key generation
  303. - `LOCAL_ROOT_URL` is not changed