在docker里基于git的post-receive实现部署时提示`Host key verification failed`

在看了很多使用git的post-receive实现部署的文章设置都很简单,但是我这里的情况相对复杂。

  1. 远端仓库使用gogs管理,而且gogs建立在docker的容器里
  2. 部署目录在主容器的/www下,挂载到gogs容器的/www下面
  3. 仓库使用ssh链接远端的
  4. gogs容器下的用户是git

找了很大一部分解决办法说是使用ssh链接远端可以修改GIT_SSH_COMMAND指定key文件,我试过在主容器(root用户)的/www目录下创建key文件,并修改其归属为git用户,结果未能解决。

# in git/hooks/post-receive

$ ls -l /www/.ssh
-rw-------    1 git      root          1675 Mar 20 04:17 uxfeel
-rw-r--r--    1 git      root           392 Mar 20 04:17 uxfeel.pub

$ GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /www/.ssh/uxfeel"
$ cd /www/***
$ git fetch

Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

第二个方案又再直接一些,由于gogs的容器把git用户的目录映射了出来,我就在主容器下找到映射出来的git/.ssh/目录下生成key文件,然后修改为git所属。结果还是提示Host key verification failed.

# in git/hooks/post-receive

$ ls -l ~/.ssh
-rw-------    1 git      git           6897 Mar 20 07:01 authorized_keys
-rw-------    1 git      git             23 Feb  9 07:26 environment
-rw-------    1 git      root          1679 Mar 20 07:12 id_rsa
-rw-r--r--    1 git      root           392 Mar 20 07:13 id_rsa.pub
-rw-r--r--    1 git      git            806 Mar 20 05:37 known_hosts

$ cd /www/***
$ git fetch

Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

到这只能放弃了。求大神搭救啊!!!

----------------------补充------------------------

Host key verification failed.的出现之前写drone的插件时遇到过,当时的情况是key文件没有给相应的权限,导致插件内部用户无法访问然后git请求实际没有带上密钥。但是此处已经多次核实容器内的用户是拥有文件可读权限的。

阅读 5.6k
1 个回答
新手上路,请多包涵

仅供参考:
进入 gogs docker 命令行,
然后 su git
然后 ssh-keygen 回车回车回车,
将生成的id_rsa.pub 内容 添加到gogs你的个人账号的ssh密钥,
可以试一下

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题