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.en-us.md 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. For a stable release you can use `:latest`, `:1` or specify a certain release like `:{{< version >}}`, but if you'd like to use the latest development version of Gitea then you could use the `:dev` tag. If you'd like to run the latest commit from a release branch you can use the `:1.x-dev` tag, where x is the minor version of Gitea. (e.g. `:1.16-dev`)
  31. ```yaml
  32. version: "3"
  33. networks:
  34. gitea:
  35. external: false
  36. services:
  37. server:
  38. image: gitea/gitea:{{< version >}}
  39. container_name: gitea
  40. environment:
  41. - USER_UID=1000
  42. - USER_GID=1000
  43. restart: always
  44. networks:
  45. - gitea
  46. volumes:
  47. - ./gitea:/data
  48. - /etc/timezone:/etc/timezone:ro
  49. - /etc/localtime:/etc/localtime:ro
  50. ports:
  51. - "3000:3000"
  52. - "222:22"
  53. ```
  54. ## Ports
  55. To bind the integrated OpenSSH daemon and the webserver on a different port, adjust
  56. the port section. It's common to just change the host port and keep the ports within
  57. the container like they are.
  58. ```diff
  59. version: "3"
  60. networks:
  61. gitea:
  62. external: false
  63. services:
  64. server:
  65. image: gitea/gitea:{{< version >}}
  66. container_name: gitea
  67. environment:
  68. - USER_UID=1000
  69. - USER_GID=1000
  70. restart: always
  71. networks:
  72. - gitea
  73. volumes:
  74. - ./gitea:/data
  75. - /etc/timezone:/etc/timezone:ro
  76. - /etc/localtime:/etc/localtime:ro
  77. ports:
  78. - - "3000:3000"
  79. - - "222:22"
  80. + - "8080:3000"
  81. + - "2221:22"
  82. ```
  83. ## Databases
  84. ### MySQL database
  85. To start Gitea in combination with a MySQL database, apply these changes to the
  86. `docker-compose.yml` file created above.
  87. ```diff
  88. version: "3"
  89. networks:
  90. gitea:
  91. external: false
  92. services:
  93. server:
  94. image: gitea/gitea:{{< version >}}
  95. container_name: gitea
  96. environment:
  97. - USER_UID=1000
  98. - USER_GID=1000
  99. + - GITEA__database__DB_TYPE=mysql
  100. + - GITEA__database__HOST=db:3306
  101. + - GITEA__database__NAME=gitea
  102. + - GITEA__database__USER=gitea
  103. + - GITEA__database__PASSWD=gitea
  104. restart: always
  105. networks:
  106. - gitea
  107. volumes:
  108. - ./gitea:/data
  109. - /etc/timezone:/etc/timezone:ro
  110. - /etc/localtime:/etc/localtime:ro
  111. ports:
  112. - "3000:3000"
  113. - "222:22"
  114. + depends_on:
  115. + - db
  116. +
  117. + db:
  118. + image: mysql:8
  119. + restart: always
  120. + environment:
  121. + - MYSQL_ROOT_PASSWORD=gitea
  122. + - MYSQL_USER=gitea
  123. + - MYSQL_PASSWORD=gitea
  124. + - MYSQL_DATABASE=gitea
  125. + networks:
  126. + - gitea
  127. + volumes:
  128. + - ./mysql:/var/lib/mysql
  129. ```
  130. ### PostgreSQL database
  131. To start Gitea in combination with a PostgreSQL database, apply these changes to
  132. the `docker-compose.yml` file created above.
  133. ```diff
  134. version: "3"
  135. networks:
  136. gitea:
  137. external: false
  138. services:
  139. server:
  140. image: gitea/gitea:{{< version >}}
  141. container_name: gitea
  142. environment:
  143. - USER_UID=1000
  144. - USER_GID=1000
  145. + - GITEA__database__DB_TYPE=postgres
  146. + - GITEA__database__HOST=db:5432
  147. + - GITEA__database__NAME=gitea
  148. + - GITEA__database__USER=gitea
  149. + - GITEA__database__PASSWD=gitea
  150. restart: always
  151. networks:
  152. - gitea
  153. volumes:
  154. - ./gitea:/data
  155. - /etc/timezone:/etc/timezone:ro
  156. - /etc/localtime:/etc/localtime:ro
  157. ports:
  158. - "3000:3000"
  159. - "222:22"
  160. + depends_on:
  161. + - db
  162. +
  163. + db:
  164. + image: postgres:14
  165. + restart: always
  166. + environment:
  167. + - POSTGRES_USER=gitea
  168. + - POSTGRES_PASSWORD=gitea
  169. + - POSTGRES_DB=gitea
  170. + networks:
  171. + - gitea
  172. + volumes:
  173. + - ./postgres:/var/lib/postgresql/data
  174. ```
  175. ## Named volumes
  176. To use named volumes instead of host volumes, define and use the named volume
  177. within the `docker-compose.yml` configuration. This change will automatically
  178. create the required volume. You don't need to worry about permissions with
  179. named volumes; Docker will deal with that automatically.
  180. ```diff
  181. version: "3"
  182. networks:
  183. gitea:
  184. external: false
  185. +volumes:
  186. + gitea:
  187. + driver: local
  188. +
  189. services:
  190. server:
  191. image: gitea/gitea:{{< version >}}
  192. container_name: gitea
  193. restart: always
  194. networks:
  195. - gitea
  196. volumes:
  197. - - ./gitea:/data
  198. + - gitea:/data
  199. - /etc/timezone:/etc/timezone:ro
  200. - /etc/localtime:/etc/localtime:ro
  201. ports:
  202. - "3000:3000"
  203. - "222:22"
  204. ```
  205. MySQL or PostgreSQL containers will need to be created separately.
  206. ## Startup
  207. To start this setup based on `docker-compose`, execute `docker-compose up -d`,
  208. to launch Gitea in the background. Using `docker-compose ps` will show if Gitea
  209. started properly. Logs can be viewed with `docker-compose logs`.
  210. To shut down the setup, execute `docker-compose down`. This will stop
  211. and kill the containers. The volumes will still exist.
  212. Notice: if using a non-3000 port on http, change app.ini to match
  213. `LOCAL_ROOT_URL = http://localhost:3000/`.
  214. ## Installation
  215. After starting the Docker setup via `docker-compose`, Gitea should be available using a
  216. favorite browser to finalize the installation. Visit http://server-ip:3000 and follow the
  217. installation wizard. If the database was started with the `docker-compose` setup as
  218. documented above, please note that `db` must be used as the database hostname.
  219. ## Configure the user inside Gitea using environment variables
  220. - `USER`: **git**: The username of the user that runs Gitea within the container.
  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. ```bash
  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. ## Managing Deployments With Environment Variables
  240. In addition to the environment variables above, any settings in `app.ini` can be set or overridden with an environment variable of the form: `GITEA__SECTION_NAME__KEY_NAME`. These settings are applied each time the docker container starts. Full information [here](https://github.com/go-gitea/gitea/tree/master/contrib/environment-to-ini).
  241. These environment variables can be passed to the docker container in `docker-compose.yml`. The following example will enable an smtp mail server if the required env variables `GITEA__mailer__FROM`, `GITEA__mailer__HOST`, `GITEA__mailer__PASSWD` are set on the host or in a `.env` file in the same directory as `docker-compose.yml`:
  242. ```bash
  243. ...
  244. services:
  245. server:
  246. environment:
  247. - GITEA__mailer__ENABLED=true
  248. - GITEA__mailer__FROM=${GITEA__mailer__FROM:?GITEA__mailer__FROM not set}
  249. - GITEA__mailer__MAILER_TYPE=smtp
  250. - GITEA__mailer__HOST=${GITEA__mailer__HOST:?GITEA__mailer__HOST not set}
  251. - GITEA__mailer__IS_TLS_ENABLED=true
  252. - GITEA__mailer__USER=${GITEA__mailer__USER:-apikey}
  253. - GITEA__mailer__PASSWD="""${GITEA__mailer__PASSWD:?GITEA__mailer__PASSWD not set}"""
  254. ```
  255. To set required TOKEN and SECRET values, consider using Gitea's built-in [generate utility functions](https://docs.gitea.io/en-us/command-line/#generate).
  256. ## SSH Container Passthrough
  257. 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 for Gitea users to ssh to a Gitea user on the host which will then relay those connections to the docker.
  258. To understand what needs to happen, you first need to understand what happens without passthrough. So we will try to explain this:
  259. 1. The client adds their SSH public key to Gitea using the webpage.
  260. 2. Gitea will add an entry for this key to the `.ssh/authorized_keys` file of its running user, `git`.
  261. 3. This entry has the public key, but also has a `command=` option. It is this command that Gitea uses to match this key to the client user and manages authentication.
  262. 4. The client then makes an SSH request to the SSH server using the `git` user, e.g. `git clone git@domain:user/repo.git`.
  263. 5. The client will attempt to authenticate with the server, passing one or more public keys one at a time to the server.
  264. 6. For each key the client provides, the SSH server will first check its configuration for an `AuthorizedKeysCommand` to see if the public key matches, and then the `git` user's `authorized_keys` file.
  265. 7. The first entry that matches will be selected, and assuming this is a Gitea entry, the `command=` will now be executed.
  266. 8. The SSH server creates a user session for the `git` user, and using the shell for the `git` user runs the `command=`
  267. 9. This runs `gitea serv` which takes over control of the rest of the SSH session and manages gitea authentication & authorization of the git commands.
  268. Now, for the SSH passthrough to work, we need the host SSH to match the public keys and then run the `gitea serv` on the docker. There are multiple ways of doing this. However, all of these require some information about the docker being passed to the host.
  269. ### SSHing Shim (with authorized_keys)
  270. In this option, the idea is that the host simply uses the `authorized_keys` that gitea creates but at step 9 the `gitea` command that the host runs is a shim that actually runs ssh to go into the docker and then run the real docker `gitea` itself.
  271. - 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:
  272. ```yaml
  273. ports:
  274. # [...]
  275. - "127.0.0.1:2222:22"
  276. ```
  277. - Next on the host create the `git` user 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`:
  278. ```yaml
  279. environment:
  280. - USER_UID=1000
  281. - USER_GID=1000
  282. ```
  283. - Mount `/home/git/.ssh` of the host into the container. This ensures that the `authorized_keys` file is shared between the host `git` user and the container `git` user otherwise the SSH authentication cannot work inside the container.
  284. ```yaml
  285. volumes:
  286. - /home/git/.ssh/:/data/git/.ssh
  287. ```
  288. - 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. As an administrative user on the host run: (by administrative user we mean a user that can sudo to root)
  289. ```bash
  290. sudo -u git ssh-keygen -t rsa -b 4096 -C "Gitea Host Key"
  291. ```
  292. - Please note depending on the local version of ssh you may want to consider using `-t ecdsa` here.
  293. - `/home/git/.ssh/authorized_keys` on the host now needs to be modified. It needs to act in the same way as `authorized_keys` within the Gitea container. Therefore add the public key of the key you created above ("Gitea Host Key") to `~/git/.ssh/authorized_keys`. As an administrative user on the host run:
  294. ```bash
  295. sudo -u git cat /home/git/.ssh/id_rsa.pub | sudo -u git tee -a /home/git/.ssh/authorized_keys
  296. sudo -u git chmod 600 /home/git/.ssh/authorized_keys
  297. ```
  298. Important: The pubkey from the `git` user needs to be added "as is" while all other pubkeys added via the Gitea web interface will be prefixed with `command="/usr [...]`.
  299. `/home/git/.ssh/authorized_keys` should then look somewhat like
  300. ```bash
  301. # SSH pubkey from git user
  302. ssh-rsa <Gitea Host Key>
  303. # other keys from users
  304. command="/usr/local/bin/gitea --config=/data/gitea/conf/app.ini serv key-1",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty <user pubkey>
  305. ```
  306. - The next step is to create the fake host `gitea` command that will forward commands from the host to the container. The name of this file depends on your version of Gitea:
  307. - For Gitea v1.16.0+. As an administrative user on the host run:
  308. ```bash
  309. cat <<"EOF" | sudo tee /usr/local/bin/gitea
  310. #!/bin/sh
  311. ssh -p 2222 -o StrictHostKeyChecking=no git@127.0.0.1 "SSH_ORIGINAL_COMMAND=\"$SSH_ORIGINAL_COMMAND\" $0 $@"
  312. EOF
  313. sudo chmod +x /usr/local/bin/gitea
  314. ```
  315. Here is a detailed explanation what is happening when a SSH request is made:
  316. 1. The client adds their SSH public key to Gitea using the webpage.
  317. 2. Gitea in the container will add an entry for this key to the `.ssh/authorized_keys` file of its running user, `git`.
  318. - However, because `/home/git/.ssh/` on the host is mounted as `/data/git/.ssh` this means that the key has been added to the host `git` user's `authorized_keys` file too.
  319. 3. This entry has the public key, but also has a `command=` option.
  320. - This command matches the location of the Gitea binary on the container, but also the location of the shim on the host.
  321. 4. The client then makes an SSH request to the host SSH server using the `git` user, e.g. `git clone git@domain:user/repo.git`.
  322. 5. The client will attempt to authenticate with the server, passing one or more public keys in turn to the host.
  323. 6. For each key the client provides, the host SSH server will first check its configuration for an `AuthorizedKeysCommand` to see if the public key matches, and then the host `git` user's `authorized_keys` file.
  324. - Because `/home/git/.ssh/` on the host is mounted as `/data/git/.ssh` this means that the key they added to the Gitea web will be found
  325. 7. The first entry that matches will be selected, and assuming this is a Gitea entry, the `command=` will now be executed.
  326. 8. The host SSH server creates a user session for the `git` user, and using the shell for the host `git` user runs the `command=`
  327. 9. This means that the host runs the host `/usr/local/bin/gitea` shim that opens an SSH from the host to container passing the rest of the command arguments directly to `/usr/local/bin/gitea` on the container.
  328. 10. Meaning that the container `gitea serv` is run, taking over control of the rest of the SSH session and managing gitea authentication & authorization of the git commands.
  329. **Notes**
  330. SSH container passthrough using `authorized_keys` will work only if
  331. - `opensshd` is used in the container
  332. - if `AuthorizedKeysCommand` is _not used_ in combination with `SSH_CREATE_AUTHORIZED_KEYS_FILE=false` to disable authorized files key generation
  333. - `LOCAL_ROOT_URL` is not changed (depending on the changes)
  334. If you try to run `gitea` on the host, you will attempt to ssh to the container and thence run the `gitea` command there.
  335. Never add the `Gitea Host Key` as a SSH key to a user on the Gitea interface.
  336. ### SSHing Shell (with authorized_keys)
  337. In this option, the idea is that the host simply uses the `authorized_keys` that gitea creates but at step 8 above we change the shell that the host runs to ssh directly into the docker and then run the shell there. This means that the `gitea` that is then run is the real docker `gitea`.
  338. - In this case we setup as per SSHing Shim except instead of creating `/usr/local/bin/gitea`
  339. we create a new shell for the git user. As an administrative user on the host run:
  340. ```bash
  341. cat <<"EOF" | sudo tee /home/git/ssh-shell
  342. #!/bin/sh
  343. shift
  344. ssh -p 2222 -o StrictHostKeyChecking=no git@127.0.0.1 "SSH_ORIGINAL_COMMAND=\"$SSH_ORIGINAL_COMMAND\" $@"
  345. EOF
  346. sudo chmod +x /home/git/ssh-shell
  347. sudo usermod -s /home/git/ssh-shell git
  348. ```
  349. Be careful here - if you try to login as the git user in future you will ssh directly to the docker.
  350. Here is a detailed explanation what is happening when a SSH request is made:
  351. 1. The client adds their SSH public key to Gitea using the webpage.
  352. 2. Gitea in the container will add an entry for this key to the `.ssh/authorized_keys` file of its running user, `git`.
  353. - However, because `/home/git/.ssh/` on the host is mounted as `/data/git/.ssh` this means that the key has been added to the host `git` user's `authorized_keys` file too.
  354. 3. This entry has the public key, but also has a `command=` option.
  355. - This command matches the location of the Gitea binary on the container.
  356. 4. The client then makes an SSH request to the host SSH server using the `git` user, e.g. `git clone git@domain:user/repo.git`.
  357. 5. The client will attempt to authenticate with the server, passing one or more public keys in turn to the host.
  358. 6. For each key the client provides, the host SSH server will first check its configuration for an `AuthorizedKeysCommand` to see if the public key matches, and then the host `git` user's `authorized_keys` file.
  359. - Because `/home/git/.ssh/` on the host is mounted as `/data/git/.ssh` this means that the key they added to the Gitea web will be found
  360. 7. The first entry that matches will be selected, and assuming this is a Gitea entry, the `command=` will now be executed.
  361. 8. The host SSH server creates a user session for the `git` user, and using the shell for the host `git` user runs the `command=`
  362. 9. The shell of the host `git` user is now our `ssh-shell` which opens an SSH connection from the host to container, (which opens a shell on the container for the container `git`).
  363. 10. The container shell now runs the `command=` option meaning that the container `gitea serv` is run, taking over control of the rest of the SSH session and managing gitea authentication & authorization of the git commands.
  364. **Notes**
  365. SSH container passthrough using `authorized_keys` will work only if
  366. - `opensshd` is used in the container
  367. - if `AuthorizedKeysCommand` is _not used_ in combination with `SSH_CREATE_AUTHORIZED_KEYS_FILE=false` to disable authorized files key generation
  368. - `LOCAL_ROOT_URL` is not changed (depending on the changes)
  369. If you try to login as the `git` user on the host in future you will ssh directly to the docker.
  370. Never add the `Gitea Host Key` as a SSH key to a user on the Gitea interface.
  371. ### Docker Shell (with authorized_keys)
  372. Similar to the above ssh shell technique we can use a shell which simply uses `docker exec`. As an administrative user on the host run:
  373. ```bash
  374. cat <<"EOF" | sudo tee /home/git/docker-shell
  375. #!/bin/sh
  376. /usr/bin/docker exec -i -u git --env SSH_ORIGINAL_COMMAND="$SSH_ORIGINAL_COMMAND" gitea sh "$@"
  377. EOF
  378. sudo chmod +x /home/git/docker-shell
  379. sudo usermod -s /home/git/docker-shell git
  380. ```
  381. Here is a detailed explanation what is happening when a SSH request is made:
  382. 1. The client adds their SSH public key to Gitea using the webpage.
  383. 2. Gitea in the container will add an entry for this key to the `.ssh/authorized_keys` file of its running user, `git`.
  384. - However, because `/home/git/.ssh/` on the host is mounted as `/data/git/.ssh` this means that the key has been added to the host `git` user's `authorized_keys` file too.
  385. 3. This entry has the public key, but also has a `command=` option.
  386. - This command matches the location of the Gitea binary on the container.
  387. 4. The client then makes an SSH request to the host SSH server using the `git` user, e.g. `git clone git@domain:user/repo.git`.
  388. 5. The client will attempt to authenticate with the server, passing one or more public keys in turn to the host.
  389. 6. For each key the client provides, the host SSH server will first check its configuration for an `AuthorizedKeysCommand` to see if the public key matches, and then the host `git` user's `authorized_keys` file.
  390. - Because `/home/git/.ssh/` on the host is mounted as `/data/git/.ssh` this means that the key they added to the Gitea web will be found
  391. 7. The first entry that matches will be selected, and assuming this is a Gitea entry, the `command=` will now be executed.
  392. 8. The host SSH server creates a user session for the `git` user, and using the shell for the host `git` user runs the `command=`
  393. 9. The shell of the host `git` user is now our `docker-shell` which uses `docker exec` to open a shell for the `git` user on the container.
  394. 10. The container shell now runs the `command=` option meaning that the container `gitea serv` is run, taking over control of the rest of the SSH session and managing gitea authentication & authorization of the git commands.
  395. 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. The host `git` user also has to have
  396. permission to run `docker exec`.
  397. **Notes**
  398. Docker shell passthrough using `authorized_keys` will work only if
  399. - `opensshd` is used in the container
  400. - if `AuthorizedKeysCommand` is _not used_ in combination with `SSH_CREATE_AUTHORIZED_KEYS_FILE=false` to disable authorized files key generation
  401. - `LOCAL_ROOT_URL` is not changed (depending on the changes)
  402. If you try to login as the `git` user on the host in future you will `docker exec` directly to the docker.
  403. A Docker execing shim could be created similarly to above.
  404. ### Docker Shell with AuthorizedKeysCommand
  405. The AuthorizedKeysCommand route provides another option that does not require many changes to the compose file or the `authorized_keys` - but does require changes to the host `/etc/sshd_config`.
  406. In this option, the idea is that the host SSH uses an `AuthorizedKeysCommand` instead of relying on sharing the `authorized_keys` file that gitea creates. We continue to use a special shell at step 8 above to exec into the docker and then run the shell there. This means that the `gitea` that is then run is the real docker `gitea`.
  407. - On the host create a `git` user with permission to run `docker exec`.
  408. - We will again assume that the Gitea container is called `gitea`.
  409. - Modify the `git` user's shell to forward commands to the `sh` executable inside the container using `docker exec`. As an administrative user on the host run:
  410. ```bash
  411. cat <<"EOF" | sudo tee /home/git/docker-shell
  412. #!/bin/sh
  413. /usr/bin/docker exec -i --env SSH_ORIGINAL_COMMAND="$SSH_ORIGINAL_COMMAND" gitea sh "$@"
  414. EOF
  415. sudo chmod +x /home/git/docker-shell
  416. sudo usermod -s /home/git/docker-shell git
  417. ```
  418. Now all attempts to login as the `git` user on the host will be forwarded to the docker - including the `SSH_ORIGINAL_COMMAND`. We now need to set-up SSH authentication on the host.
  419. We will do this by leveraging the [SSH AuthorizedKeysCommand](https://docs.gitea.io/en-us/command-line/#keys) to match the keys against those accepted by Gitea.
  420. Add the following block to `/etc/ssh/sshd_config`, on the host:
  421. ```bash
  422. Match User git
  423. AuthorizedKeysCommandUser git
  424. AuthorizedKeysCommand /usr/bin/docker exec -i gitea /usr/local/bin/gitea keys -c /data/gitea/conf/app.ini -e git -u %u -t %t -k %k
  425. ```
  426. (From 1.16.0 you will not need to set the `-c /data/gitea/conf/app.ini` option.)
  427. Finally restart the SSH server. As an administrative user on the host run:
  428. ```bash
  429. sudo systemctl restart sshd
  430. ```
  431. Here is a detailed explanation what is happening when a SSH request is made:
  432. 1. The client adds their SSH public key to Gitea using the webpage.
  433. 2. Gitea in the container will add an entry for this key to its database.
  434. 3. The client then makes an SSH request to the host SSH server using the `git` user, e.g. `git clone git@domain:user/repo.git`.
  435. 4. The client will attempt to authenticate with the server, passing one or more public keys in turn to the host.
  436. 5. For each key the client provides, the host SSH server will checks its configuration for an `AuthorizedKeysCommand`.
  437. 6. The host runs the above `AuthorizedKeysCommand`, which execs in to the docker and then runs the `gitea keys` command.
  438. 7. Gitea on the docker will look in it's database to see if the public key matches and will return an entry like that of an `authorized_keys` command.
  439. 8. This entry has the public key, but also has a `command=` option which matches the location of the Gitea binary on the container.
  440. 9. The host SSH server creates a user session for the `git` user, and using the shell for the host `git` user runs the `command=`.
  441. 10. The shell of the host `git` user is now our `docker-shell` which uses `docker exec` to open a shell for the `git` user on the container.
  442. 11. The container shell now runs the `command=` option meaning that the container `gitea serv` is run, taking over control of the rest of the SSH session and managing gitea authentication & authorization of the git commands.
  443. **Notes**
  444. Docker shell passthrough using `AuthorizedKeysCommand` will work only if
  445. - The host `git` user is allowed to run the `docker exec` command.
  446. If you try to login as the `git` user on the host in future you will `docker exec` directly to the docker.
  447. A Docker execing shim could be created similarly to above.
  448. ### SSH Shell with AuthorizedKeysCommand
  449. Create a key for the host `git` user as above, add it to the docker `/data/git/.ssh/authorized_keys` then finally create and set the `ssh-shell` as above.
  450. Add the following block to `/etc/ssh/sshd_config`, on the host:
  451. ```bash
  452. Match User git
  453. AuthorizedKeysCommandUser git
  454. AuthorizedKeysCommand /usr/bin/ssh -p 2222 -o StrictHostKeyChecking=no git@127.0.0.1 /usr/local/bin/gitea keys -c /data/gitea/conf/app.ini -e git -u %u -t %t -k %k
  455. ```
  456. (From 1.16.0 you will not need to set the `-c /data/gitea/conf/app.ini` option.)
  457. Finally restart the SSH server. As an administrative user on the host run:
  458. ```bash
  459. sudo systemctl restart sshd
  460. ```
  461. Here is a detailed explanation what is happening when a SSH request is made:
  462. 1. The client adds their SSH public key to Gitea using the webpage.
  463. 2. Gitea in the container will add an entry for this key to its database.
  464. 3. The client then makes an SSH request to the host SSH server using the `git` user, e.g. `git clone git@domain:user/repo.git`.
  465. 4. The client will attempt to authenticate with the server, passing one or more public keys in turn to the host.
  466. 5. For each key the client provides, the host SSH server will checks its configuration for an `AuthorizedKeysCommand`.
  467. 6. The host runs the above `AuthorizedKeysCommand`, which will SSH in to the docker and then run the `gitea keys` command.
  468. 7. Gitea on the docker will look in it's database to see if the public key matches and will return an entry like that of an `authorized_keys` command.
  469. 8. This entry has the public key, but also has a `command=` option which matches the location of the Gitea binary on the container.
  470. 9. The host SSH server creates a user session for the `git` user, and using the shell for the host `git` user runs the `command=`.
  471. 10. The shell of the host `git` user is now our `git-shell` which uses SSH to open a shell for the `git` user on the container.
  472. 11. The container shell now runs the `command=` option meaning that the container `gitea serv` is run, taking over control of the rest of the SSH session and managing gitea authentication & authorization of the git commands.
  473. **Notes**
  474. SSH container passthrough using `AuthorizedKeysCommand` will work only if
  475. - `opensshd` is running on the container
  476. If you try to login as the `git` user on the host in future you will `ssh` directly to the docker.
  477. Never add the `Gitea Host Key` as a SSH key to a user on the Gitea interface.
  478. SSHing shims could be created similarly to above.