gpt4 book ai didi

git - 在 Gitlab 的 Dockerfile 上使用 SSH key - 权限被拒绝

转载 作者:行者123 更新时间:2023-12-02 18:29:49 25 4
gpt4 key购买 nike

嗨!

我目前正在尝试构建一个 docker 镜像,我需要从远程 GitLab 服务器 pull 一个 git 项目。问题是 publickey 方法 根本不起作用。

错误:

Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password)
fatal: Could not read from remote repository.

这是我启动的命令:

docker build . --build-arg priv_key="$(cat ~/.ssh/id_rsa)" --build-arg pub_key="$(cat ~/.ssh/id_rsa.pub)"

这是我的 Dockerfile:

FROM centos/python-35-centos7:latest
USER root
ARG pub_key
ARG priv_key
RUN mkdir -p /root/.ssh/
RUN chmod 0700 /root/.ssh/
RUN echo ${pub_key} >> /root/.ssh/id_rsa.pub
RUN chmod 600 /root/.ssh/id_rsa.pub
RUN echo ${priv_key} >> /root/.ssh/id_rsa
RUN chmod 600 /root/.ssh/id_rsa
RUN git config --global user.name "A name"
RUN git config --global user.email "email@address.com"
RUN echo "Host 192.168.1.28\n\tPasswordAuthentication no\n" >> /root/.ssh/config
RUN chmod 600 /root/.ssh/config
RUN ssh-keyscan -t rsa 192.168.1.28 >> /root/.ssh/known_hosts
RUN ssh -Tv git@192.168.1.28

问题是,当我检查 ssh -Tv 的输出时,它尝试读取密码而不是验证:

debug1: Next authentication method: publickey
debug1: Offering RSA public key: /root/.ssh/id_rsa
debug1: Server accepts key: pkalg rsa-sha2-512 blen 279
debug1: read_passphrase: can't open /dev/tty: No such device or address
debug1: Trying private key: /root/.ssh/id_dsa
debug1: Trying private key: /root/.ssh/id_ecdsa
debug1: Trying private key: /root/.ssh/id_ed25519

看起来它需要一个密码(/dev/tty:没有这样的设备或地址),但在主机本身上,相同的命令完全有效:

debug1: Next authentication method: publickey
debug1: Offering RSA public key: /root/.ssh/id_rsa
debug1: Server accepts key: pkalg rsa-sha2-512 blen 279
debug1: Authentication succeeded (publickey).
Authenticated to 192.168.1.28 ([192.168.1.28]:22).

知道发生了什么吗?

编辑:

随着website @emory 在下面提供,我已经尝试将代码放入(并添加 chmod 命令以避免 Docker 错误),将其设置为我拥有的 GitLab IP 地址......并且它有效!

我更改了以下几行:

FROM ubuntu as intermediate > FROM centos:7
apt-get update > yum update
apt-get install -y git > yum install -y git

而且效果也很好。我也试过原来的 centos/python-35-centos7:latest,它成功了。对于那些需要这个的人,有功能代码:

FROM centos/python-35-centos7:latest
#FROM centos:7
USER root
RUN yum update -y
RUN yum install -y git
ARG SSH_PRIVATE_KEY
RUN mkdir /root/.ssh/
RUN echo "${SSH_PRIVATE_KEY}" > /root/.ssh/id_rsa
RUN chmod 600 /root/.ssh/id_rsa
RUN touch /root/.ssh/known_hosts
RUN ssh-keyscan 192.168.1.28 >> /root/.ssh/known_hosts
RUN ssh -Tv git@192.168.1.28

最佳答案

显然答案是使用不同的基础图像。 ubuntu 据报告可以工作。 https://vsupalov.com/build-docker-image-clone-private-repo-ssh-key/是一个很好的指导。

关于git - 在 Gitlab 的 Dockerfile 上使用 SSH key - 权限被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50895755/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com