Skip to content

Commit b05ebcf

Browse files
committed
Add docs on passing SSH through to container
Wasn't clear how to pass SSH connections through the container, found a few blog posts which described roughly the same procedure I've documented here. Credit should really go to: * https://blog.sakuragawa.moe/gitea-in-docker-container-and-sharing-ssh-with-host/ * http://www.ateijelo.com/blog/2016/07/09/share-port-22-between-docker-gogs-ssh-and-local-system Signed-off-by: Dane Elwell <[email protected]>
1 parent d7ca839 commit b05ebcf

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

docs/content/doc/installation/with-docker.en-us.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,63 @@ be placed in `/data/gitea` directory. If using host volumes it's quite easy to a
267267
files; for named volumes this is done through another container or by direct access at
268268
`/var/lib/docker/volumes/gitea_gitea/_data`. The configuration file will be saved at
269269
`/data/gitea/conf/app.ini` after the installation.
270+
271+
# SSH Container Passthrough
272+
273+
Since SSH is running inside the container, you'll have to pass SSH from the host to the
274+
container if you wish to use SSH support. If you wish to do this without running the container
275+
SSH on a non-standard port (or move your host port to a non-standard port) you can forward
276+
SSH connections destined for the container with a little extra setup.
277+
278+
This guide assumes that you have created a user on the host called `git` which shares the same
279+
UID/GID as the container values `USER_UID`/`USER_GID`. You should also create the directory
280+
`/var/lib/gitea` on the host, owned by the `git` user and mounted in the container, e.g.
281+
282+
```
283+
services:
284+
server:
285+
image: gitea/gitea:latest
286+
environment:
287+
- USER_UID=1000
288+
- USER_GID=1000
289+
restart: always
290+
networks:
291+
- gitea
292+
volumes:
293+
- /var/lib/gitea:/data
294+
ports:
295+
- "3000:3000"
296+
- "127.0.0.1:2222:22"
297+
```
298+
299+
You can see that we're also exposing the container SSH port to port 2222 on the host, and binding this
300+
to 127.0.0.1 to prevent it being accessible external to the host machine itself.
301+
302+
On the **host**, you should create the file `/app/gitea/gitea` with the following contents and
303+
make it executable (`chmod +x /app/gitea/gitea`):
304+
305+
```
306+
#!/bin/sh
307+
ssh -p 2222 -o StrictHostKeyChecking=no [email protected] "SSH_ORIGINAL_COMMAND=\"$SSH_ORIGINAL_COMMAND\" $0 $@"
308+
```
309+
310+
Your `git` user needs to have an SSH key generated:
311+
312+
```
313+
sudo -u git ssh-keygen -t rsa -b 4096 -C "Gitea Host Key"
314+
```
315+
316+
Still on the host, symlink the container `.ssh/authorized_keys` file to your git user `.ssh/authorized_keys`.
317+
This can be done on the host as the `/var/lib/gitea` directory is mounted inside the container under `/data`:
318+
319+
```
320+
ln -s /var/lib/gitea/git/.ssh/authorized_keys /home/git/.ssh/authorized_keys
321+
```
322+
323+
Then echo the `git` user SSH key into the authorized_keys file so the host can talk to the container over SSH:
324+
325+
```
326+
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
327+
```
328+
329+
Now you should be able to use Git over SSH to your container without disrupting SSH access to the host.

0 commit comments

Comments
 (0)