繁体   English   中英

如何使用 windows docker 容器内部的 ssh 密钥克隆私有存储库?

[英]How to clone a private repository using ssh keys from inside a windows docker container?

我正在尝试设置一个 windows docker 容器,我从中 git 拉出一个托管在 ZD0498FAC9D72551402B 上的私有容器我在主机中设置了 ssh 密钥,允许我使用 git 提取所述存储库。 我看过很多关于这个主题的帖子,但它们都针对托管 linux 或 alpine 图像的容器,没有一个适用于 windows 容器。 这是我的 Dockerfile 的内容。

# syntax=docker/dockerfile:experimental
FROM mcr.microsoft.com/dotnet/framework/sdk:4.8-windowsservercore-ltsc2019

RUN Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

RUN choco install git -params '"/GitAndUnixToolsOnPath"' -y
RUN choco install openssh --pre -y

RUN mkdir C:/Users/ContainerAdministrator/.ssh/
RUN ssh-keyscan -t rsa -H gitlab.oit.<my_institution>.edu >> C:/Users/ContainerAdministrator/.ssh/known_hosts

ARG ssh_pub_key
ARG ssh_prv_key

RUN echo $env:ssh_prv_key > C:/Users/ContainerAdministrator/.ssh/id_rsa
RUN echo $env:ssh_pub_key > C:/Users/ContainerAdministrator/.ssh/id_rsa.pub
RUN git clone git+ssh://gitlab.oit.<my_institution>.edu/<path_to_our_repo>.git

然后我正在按照说明进行构建

docker build --build-arg ssh_prv_key="$(type ~/.ssh/id_rsa)" --build-arg ssh_pub_key="$(type ~/.ssh/id_rsa.pub)" .

我还通过 PowerShell window 进入容器并尝试手动逐行执行此操作,但在最后一行,我得到了真实性提示。 我认为 ssh-keyscan 线可以解决这个问题。 该命令确实使用三个不同的键填充了我的 known_hosts 文件,但是在尝试克隆时仍然会收到提示。

The authenticity of host 'gitlab.oit.<my_institution>.edu (<institution’s IP Address>)' can't be established.
RSA key fingerprint is SHA256:eCKEsLv7El0SfMuHCLJ8xZohKbHetIOo18FbIDUX+78.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? fatal: 

我认为 ssh-keyscan 线可以解决这个问题。 该命令确实使用密钥填充了我的 known_hosts 文件,但在尝试克隆时仍然会收到提示。 在单独的说明中,即使我手动键入是,我不想这样做(我希望这是一个可移植和自动化的过程),git 克隆命令也不适用于 ssh 键,因为我得到:

 Load key "/c/Users/ContainerAdministrator/.ssh/id_rsa": invalid format. 

这对我有用。 这有点棘手,因为 echo 生成 UTF-16 编码,乍一看很难注意到,所以我们需要解决这个问题:

ARG ssh_pub_key
ARG ssh_prv_key

RUN echo $env:ssh_prv_key | Out-File -encoding ASCII $env:UserProfile/.ssh/id_rsa
RUN echo $env:ssh_pub_key | Out-File -encoding ASCII $env:UserProfile/.ssh/id_rsa.pub

在我的情况下,我还必须将“\n”文本转换为实际的行尾符号(注意引号已转义以保护它们免受 Docker 处理):

ARG ssh_pub_key
ARG ssh_prv_key

RUN echo $env:ssh_prv_key.replace(\"\n\", \"`n\") | Out-File -encoding ASCII $env:UserProfile/.ssh/id_rsa
RUN echo $env:ssh_pub_key.replace(\"\n\", \"`n\") | Out-File -encoding ASCII $env:UserProfile/.ssh/id_rsa.pub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM