Loading... 如果主机的 22 端口已被使用,使用 `Docker` 安装 `Gitea` 时只能把容器的 22 端口映射到主机的其它端口(如:10022),这是没有任何问题的。但是以 `SSH` 方式 `clone` 项目时,`URL` 长这样 `ssh://git@git.example.com:10022:username/project.git` 如果我们想要类似以下这样的 `URL` 时就需要把 `Gitea` 容器的和主机共享 22 端口 `git@git.example.com:username/project.git` 下面总结一下使用 `Docker` 安装 `Gitea` 共享主机 22 端口的主要步骤,`Gogs` 应该是同理。 ### 创建 git 用户 ```bash # Create git user adduser git # Make sure user has UID and GID 1000 usermod -u 1000 -g 1000 git # Create docker group groupadd docker # Add git user to docker group usermod -aG docker git # Create the gitea data directory mkdir -p /home/git/gitea/data ``` <!--more--> ### 安装 Gitea ```bash docker run -d --name=gitea -p 10022:22 -p 10080:3000 -v /home/git/gitea/data:/data --restart=always gitea/gitea:latest # Create a symlink between the container authorized_keys and the host git user authorized_keys ln -s /home/git/gitea/data/git/.ssh /home/git/ ``` ### 生成 SSH key ```bash sudo -u git ssh-keygen -t rsa -b 4096 -C "Gitea Host Key" echo "no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty $(cat /home/git/.ssh/id_rsa.pub)" >> /home/git/.ssh/authorized_keys chmod 600 /home/git/.ssh/authorized_keys ``` ### 配置 SSH passthrough 配置 `passthrough` 连接到 `Gitea` 容器的 `SSH` 映射端口 10022 ```bash mkdir -p /app/gitea/ cat >/app/gitea/gitea <<'END' #!/bin/sh ssh -p 10022 -o StrictHostKeyChecking=no git@127.0.0.1 \ "SSH_ORIGINAL_COMMAND=\"$SSH_ORIGINAL_COMMAND\" $0 $@" END chmod +x /app/gitea/gitea ``` ### Caddy 反向代理配置 这里使用 `Caddy` 反向代理配置域名,`Caddyfile` 配置信息如下: ``` git.example.com { encode zstd gzip reverse_proxy localhost:10080 header / Strict-Transport-Security "max-age=31536000;" } ``` 配置完域名之后,输入域名进行安装,现在就可以修改 【SSH 服务域名】 为 `git.example.com`,【Gitea 基本 URL】 为 `https://git.example.com/`,也可以后通过 `/home/git/gogs/data/gogs/conf/app.ini` 配置文件修改相关配置。 ### 注意事项 * 由于 `docker` 启动容器的默认 `uid` 和 `gid` 是 1000,所以 `git` 用户的 `uid`、`gid` 必须为 1000,如果 `git` 用户的 `uid` 和 `gid` 不是 1000(比如:1002),尝试通过 `docker run --user 1002:1002`、 `docker run -e "PUID=1002" -e "PGID=1002"` 等方式启动 `docker` 容器都不管用。 * 保证 `git` 用户下的所有文件都属于 `git` 用户和 `git` 组 ```bash [git]$ ls -la /home/git/gitea/data total 20 drwxrwxr-x 5 git git 4096 Jan 5 14:14 . drwxrwxr-x 3 git git 4096 Jan 5 13:56 .. drwxr-xr-x 5 git git 4096 Jan 5 14:20 git drwxr-xr-x 10 git git 4096 Jan 5 14:40 gitea drwx------ 2 git git 4096 Jan 5 14:14 ssh ``` ### 参考文章 * [Gogs 与主机共享 22 端口](https://raincal.com/post/gogs-share-22-port) * [docker-gitea](https://gitea.com/jwobith/docker-gitea#additional-steps) — The End — Last modification:January 14th, 2021 at 11:09 pm © 允许规范转载 Support If you think my article is useful to you, please feel free to appreciate ×Close Appreciate the author Sweeping payments Pay by AliPay Pay by WeChat
One comment