我正在尝试使用 Docker 为我构建镜像,导入托管在私有(private) github 存储库中的 npm 包:"mypackage": "git@github.com:myaccount/myrepo.git#v0.0.2 "
这在本地运行良好,因为我有 SSH 访问权限,但显然我的 Docker 容器没有。我已按照以下指南使用 18.09 中启用的一些 ssh 转发来实现此目的:
https://medium.com/@tonistiigi/build-secrets-and-ssh-forwarding-in-docker-18-09-ae8161d066
使用以下 docker 文件:
# syntax=docker/dockerfile:experimental
FROM alpine
# Install ssh client and git
RUN apk add --no-cache openssh-client git
# Download public key for github.com
RUN mkdir -p -m 0600 ~/.ssh && ssh-keyscan github.com >> ~/.ssh/known_hosts
# Clone private repository
RUN --mount=type=ssh npm install
然后,运行 docker build --ssh default .
失败并出现以下错误:
#13 1.309 npm ERR! Host key verification failed.
#13 1.309 npm ERR! fatal: Could not read from remote repository.
#13 1.309 npm ERR!
#13 1.309 npm ERR! Please make sure you have the correct access rights
#13 1.309 npm ERR! and the repository exists.
#13 1.310 npm ERR!
#13 1.310 npm ERR! exited with error code: 128
我严格按照该文档进行操作,但没有成功。我错过了什么吗?我在 OSX 上,但这在我的 Travis 环境中也失败了,并出现了同样的错误。帮助!
最佳答案
这对我有用。
Dockerfile 提取:
# syntax=docker/dockerfile:experimental
...
RUN mkdir -p -m 0600 /root/.ssh
RUN ssh-keyscan bitbucket.org >> /root/.ssh/known_hosts
COPY development/config /root/.ssh
RUN chmod 0600 /root/.ssh/config
RUN --mount=type=ssh git clone **MY_PVT_REPOSITORY**
这是开发/配置文件的内容,你可以在第三行看到被复制
Host bitbucket.org
StrictHostKeyChecking no
IdentityFile **MY LOCAL PATH**/.ssh/id_rsa
棘手的事情是你必须把主机文件路径放到 id_rsa,而不是 docker 上的路径(比如/home/fabio/.ssh/id_rsa 而不是/root/.ssh/id_rsa)
然后启动
ssh-agent
export DOCKER_BUILDKIT=1
docker build --ssh default -f development/Dockerfile .
关于Docker --ssh 标志 - 主机 key 验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56465731/