问题
我们在使用docker搭建私有git仓库时, 会将容器的22端口映射到宿主的其他端口上, 此时如果我们使用ssh地址进行仓库clone, 则会报错 提示ssh: connect to host localhost port 22: Connection refused:

假设映射到宿主的端口号为10022, 那么解决方案如下:
解决方案
第一种
clone时指定端口号:1
git clone ssh://git@localhost:10022/songjian/ggg.git
注意: 需要带上
ssh://这个协议头第二种 在
.ssh目录下的config文件中增加port字段指定端口访问:1
2
3
4
5Host localhost
HostName localhost
Port 10022
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa由于
git默认port是22, 此时我们修改后则可以按照正常的形式进行仓库的克隆:1
git clone git@localhost/songjian/ggg.git
第三种 使用
nginx反向代理在
nginx.conf文件中配置如下:1
2
3
4
5
6
7
8
9stream {
upstream ssh-proxy {
server 服务器ip:10022;
}
server {
listen 22;
proxy_pass ssh-proxy;
}
}配置好后, 直接正常形式
clone:1
git clone git@localhost/songjian/ggg.git
本文为作者原创转载时请注明出处 谢谢
