簡體   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