【发布时间】:2019-01-14 20:49:40
【问题描述】:
我正在尝试从 Dockerfile 构建 docker 映像,需要采取的步骤之一是安装仅可通过私有 Gitlab 存储库获得的依赖项。这意味着容器需要访问 SSH 密钥才能进行克隆。我知道这不是最安全的方法,但是这只是一个中间容器,一旦运行应用程序所需的所有组件都到位,它将被删除。
问题是,无论我尝试什么,都无法在 docker 中获取 ssh 代理来建立连接。我明白了:
npm ERR! Host key verification failed.
npm ERR! fatal: Could not read from remote repository.
npm ERR!
npm ERR! Please make sure you have the correct access rights
npm ERR! and the repository exists.
如果我尝试简单地克隆存储库而不运行npm install,也会发生同样的事情。这是我使用的 Dockerfile:
FROM risingstack/alpine:3.4-v6.9.4-4.2.0
RUN apk update
RUN apk add openssh
ARG SSH_KEY
# Authorize SSH Host
RUN mkdir -p /root/.ssh && \
chmod 700 /root/.ssh && \
ssh-keyscan github.com > /root/.ssh/known_hosts
# Add the keys and set permissions
RUN echo "$SSH_KEY" > /root/.ssh/id_rsa && \
chmod 700 /root/.ssh/id_rsa && \
RUN eval "$(ssh-agent -s)" && ssh-add /root/.ssh/id_rsa && ssh -o StrictHostKeyChecking=no git@github.com || true && npm install
和命令(我将私钥作为构建参数传递):
docker build -t test --build-arg SSH_KEY="$(cat ~/.ssh/id_rsa)" .
【问题讨论】:
标签: git docker ssh dockerfile docker-container