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.

346 lines
11 KiB

  1. ---
  2. date: "2016-12-01T16:00:00+02:00"
  3. title: "Installation with Docker"
  4. slug: "install-with-docker"
  5. weight: 10
  6. toc: true
  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. ## Basics
  23. The most simple setup just creates a volume and a network and starts the `gitea/gitea:latest`
  24. image as a service. Since there is no database available, one can be initialized using SQLite3.
  25. Create a directory like `gitea` and paste the following content into a file named `docker-compose.yml`.
  26. Note that the volume should be owned by the user/group with the UID/GID specified in the config file.
  27. If you don't give the volume correct permissions, the container may not start.
  28. Also be aware that the tag `:latest` will install the current development version.
  29. For a stable release you can use `:1` or specify a certain release like `:1.5.1`.
  30. ```yaml
  31. version: "2"
  32. networks:
  33. gitea:
  34. external: false
  35. services:
  36. server:
  37. image: gitea/gitea:latest
  38. environment:
  39. - USER_UID=1000
  40. - USER_GID=1000
  41. restart: always
  42. networks:
  43. - gitea
  44. volumes:
  45. - ./gitea:/data
  46. ports:
  47. - "3000:3000"
  48. - "222:22"
  49. ```
  50. ## Custom port
  51. To bind the integrated openSSH daemon and the webserver on a different port, adjust
  52. the port section. It's common to just change the host port and keep the ports within
  53. the container like they are.
  54. ```diff
  55. version: "2"
  56. networks:
  57. gitea:
  58. external: false
  59. services:
  60. server:
  61. image: gitea/gitea:latest
  62. environment:
  63. - USER_UID=1000
  64. - USER_GID=1000
  65. restart: always
  66. networks:
  67. - gitea
  68. volumes:
  69. - ./gitea:/data
  70. ports:
  71. - - "3000:3000"
  72. - - "222:22"
  73. + - "8080:3000"
  74. + - "2221:22"
  75. ```
  76. ## MySQL database
  77. To start Gitea in combination with a MySQL database, apply these changes to the
  78. `docker-compose.yml` file created above.
  79. ```diff
  80. version: "2"
  81. networks:
  82. gitea:
  83. external: false
  84. services:
  85. server:
  86. image: gitea/gitea:latest
  87. environment:
  88. - USER_UID=1000
  89. - USER_GID=1000
  90. + - DB_TYPE=mysql
  91. + - DB_HOST=db:3306
  92. + - DB_NAME=gitea
  93. + - DB_USER=gitea
  94. + - DB_PASSWD=gitea
  95. restart: always
  96. networks:
  97. - gitea
  98. volumes:
  99. - ./gitea:/data
  100. ports:
  101. - "3000:3000"
  102. - "222:22"
  103. + depends_on:
  104. + - db
  105. +
  106. + db:
  107. + image: mysql:5.7
  108. + restart: always
  109. + environment:
  110. + - MYSQL_ROOT_PASSWORD=gitea
  111. + - MYSQL_USER=gitea
  112. + - MYSQL_PASSWORD=gitea
  113. + - MYSQL_DATABASE=gitea
  114. + networks:
  115. + - gitea
  116. + volumes:
  117. + - ./mysql:/var/lib/mysql
  118. ```
  119. ## PostgreSQL database
  120. To start Gitea in combination with a PostgreSQL database, apply these changes to
  121. the `docker-compose.yml` file created above.
  122. ```diff
  123. version: "2"
  124. networks:
  125. gitea:
  126. external: false
  127. services:
  128. server:
  129. image: gitea/gitea:latest
  130. environment:
  131. - USER_UID=1000
  132. - USER_GID=1000
  133. + - DB_TYPE=postgres
  134. + - DB_HOST=db:5432
  135. + - DB_NAME=gitea
  136. + - DB_USER=gitea
  137. + - DB_PASSWD=gitea
  138. restart: always
  139. networks:
  140. - gitea
  141. volumes:
  142. - ./gitea:/data
  143. ports:
  144. - "3000:3000"
  145. - "222:22"
  146. + depends_on:
  147. + - db
  148. +
  149. + db:
  150. + image: postgres:9.6
  151. + restart: always
  152. + environment:
  153. + - POSTGRES_USER=gitea
  154. + - POSTGRES_PASSWORD=gitea
  155. + - POSTGRES_DB=gitea
  156. + networks:
  157. + - gitea
  158. + volumes:
  159. + - ./postgres:/var/lib/postgresql/data
  160. ```
  161. ## Named volumes
  162. To use named volumes instead of host volumes, define and use the named volume
  163. within the `docker-compose.yml` configuration. This change will automatically
  164. create the required volume. You don't need to worry about permissions with
  165. named volumes; Docker will deal with that automatically.
  166. ```diff
  167. version: "2"
  168. networks:
  169. gitea:
  170. external: false
  171. +volumes:
  172. + gitea:
  173. + driver: local
  174. +
  175. services:
  176. server:
  177. image: gitea/gitea:latest
  178. restart: always
  179. networks:
  180. - gitea
  181. volumes:
  182. - - ./gitea:/data
  183. + - gitea:/data
  184. ports:
  185. - "3000:3000"
  186. - "222:22"
  187. ```
  188. MySQL or PostgreSQL containers will need to be created separately.
  189. ## Start
  190. To start this setup based on `docker-compose`, execute `docker-compose up -d`,
  191. to launch Gitea in the background. Using `docker-compose ps` will show if Gitea
  192. started properly. Logs can be viewed with `docker-compose logs`.
  193. To shut down the setup, execute `docker-compose down`. This will stop
  194. and kill the containers. The volumes will still exist.
  195. Notice: if using a non-3000 port on http, change app.ini to match
  196. `LOCAL_ROOT_URL = http://localhost:3000/`.
  197. ## Install
  198. After starting the Docker setup via `docker-compose`, Gitea should be available using a
  199. favorite browser to finalize the installation. Visit http://server-ip:3000 and follow the
  200. installation wizard. If the database was started with the `docker-compose` setup as
  201. documented above, please note that `db` must be used as the database hostname.
  202. ## Environments variables
  203. You can configure some of Gitea's settings via environment variables:
  204. (Default values are provided in **bold**)
  205. * `APP_NAME`: **"Gitea: Git with a cup of tea"**: Application name, used in the page title.
  206. * `RUN_MODE`: **dev**: For performance and other purposes, change this to `prod` when deployed to a production environment.
  207. * `SSH_DOMAIN`: **localhost**: Domain name of this server, used for the displayed clone URL in Gitea's UI.
  208. * `SSH_PORT`: **22**: SSH port displayed in clone URL.
  209. * `DISABLE_SSH`: **false**: Disable SSH feature when it's not available.
  210. * `HTTP_PORT`: **3000**: HTTP listen port.
  211. * `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).
  212. * `DB_TYPE`: **sqlite3**: The database type in use \[mysql, postgres, mssql, sqlite3\].
  213. * `DB_HOST`: **localhost:3306**: Database host address and port.
  214. * `DB_NAME`: **gitea**: Database name.
  215. * `DB_USER`: **root**: Database username.
  216. * `DB_PASSWD`: **"\<empty>"**: Database user password. Use \`your password\` for quoting if you use special characters in the password.
  217. * `INSTALL_LOCK`: **false**: Disallow access to the install page.
  218. * `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`.
  219. * `DISABLE_REGISTRATION`: **false**: Disable registration, after which only admin can create accounts for users.
  220. * `REQUIRE_SIGNIN_VIEW`: **false**: Enable this to force users to log in to view any page.
  221. * `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).
  222. * `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).
  223. # Customization
  224. Customization files described [here](https://docs.gitea.io/en-us/customizing-gitea/) should
  225. be placed in `/data/gitea` directory. If using host volumes, it's quite easy to access these
  226. files; for named volumes, this is done through another container or by direct access at
  227. `/var/lib/docker/volumes/gitea_gitea/_data`. The configuration file will be saved at
  228. `/data/gitea/conf/app.ini` after the installation.
  229. # Upgrading
  230. :exclamation::exclamation: **Make sure you have volumed data to somewhere outside Docker container** :exclamation::exclamation:
  231. To upgrade your installation to the latest release:
  232. ```
  233. # Edit `docker-compose.yml` to update the version, if you have one specified
  234. # Pull new images
  235. docker-compose pull
  236. # Start a new container, automatically removes old one
  237. docker-compose up -d
  238. ```
  239. # SSH Container Passthrough
  240. Since SSH is running inside the container, you'll have to pass SSH from the host to the
  241. container if you wish to use SSH support. If you wish to do this without running the container
  242. SSH on a non-standard port (or move your host port to a non-standard port), you can forward
  243. SSH connections destined for the container with a little extra setup.
  244. This guide assumes that you have created a user on the host called `git` which shares the same
  245. UID/GID as the container values `USER_UID`/`USER_GID`. You should also create the directory
  246. `/var/lib/gitea` on the host, owned by the `git` user and mounted in the container, e.g.
  247. ```
  248. services:
  249. server:
  250. image: gitea/gitea:latest
  251. environment:
  252. - USER_UID=1000
  253. - USER_GID=1000
  254. restart: always
  255. networks:
  256. - gitea
  257. volumes:
  258. - /var/lib/gitea:/data
  259. ports:
  260. - "3000:3000"
  261. - "127.0.0.1:2222:22"
  262. ```
  263. You can see that we're also exposing the container SSH port to port 2222 on the host, and binding this
  264. to 127.0.0.1 to prevent it being accessible external to the host machine itself.
  265. On the **host**, you should create the file `/app/gitea/gitea` with the following contents and
  266. make it executable (`chmod +x /app/gitea/gitea`):
  267. ```
  268. #!/bin/sh
  269. ssh -p 2222 -o StrictHostKeyChecking=no git@127.0.0.1 "SSH_ORIGINAL_COMMAND=\"$SSH_ORIGINAL_COMMAND\" $0 $@"
  270. ```
  271. Your `git` user needs to have an SSH key generated:
  272. ```
  273. sudo -u git ssh-keygen -t rsa -b 4096 -C "Gitea Host Key"
  274. ```
  275. Still on the host, symlink the container `.ssh/authorized_keys` file to your git user `.ssh/authorized_keys`.
  276. This can be done on the host as the `/var/lib/gitea` directory is mounted inside the container under `/data`:
  277. ```
  278. ln -s /var/lib/gitea/git/.ssh/authorized_keys /home/git/.ssh/authorized_keys
  279. ```
  280. Then echo the `git` user SSH key into the authorized_keys file so the host can talk to the container over SSH:
  281. ```
  282. 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
  283. ```
  284. Now you should be able to use Git over SSH to your container without disrupting SSH access to the host.
  285. Please note: SSH container passthrough will work only if using opensshd in container, and will not work if
  286. `AuthorizedKeysCommand` is used in combination with setting `SSH_CREATE_AUTHORIZED_KEYS_FILE=false` to disable
  287. authorized files key generation.