簡體   English   中英

遠程 Docker 容器內的 Github 訪問(帶 2FA)

[英]Github access (with 2FA) inside remote Docker container

無法找到另一個類似這樣的問題,但請告訴我是否有一個涵蓋所有相同元素的問題(遠程訪問主機、在那里設置 Docker 容器、Github 2FA 以訪問私有存儲庫)。

我最近加入了一家公司,在那里我從家里的筆記本電腦遠程 ssh-ing 到主機。 在這些機器上,我的同事每個人都設置了一個 Docker 容器,我也做了同樣的事情(我的第一次)。

Having said that, I use the following workflows to (a) push to my company's private Github repos (let's say, https://github.com/my_company_name/my_company_repo ), (b) clone and install from my company's private Github repos (比方說, https://github.com/my_company_name/colleague_repo )。

對於(a),我首先導航到 Docker 容器內的終端,然后導航到 repo 目錄, git fetch從我的 repo 中獲取,然后(在添加/提交等之后) git push在我的用戶名點處填充它,和密碼。 密碼在這里不起作用; 我必須填寫我的個人訪問令牌(我使用讀寫權限創建的)。

對於(b)我首先從同事的git clone ,並且必須輸入我的用戶名和密碼。 (同樣,需要個人訪問令牌而不是密碼。)然后我pip install -e按名稱安裝 repo。

我想通過在我的 Dockerfile 中使用某種適當的配置來避免不斷提供我的憑據。 (所以對於(a)我想打開我的終端並git push <origin> <master>就是這樣。)我還想克隆並安裝我同事的存儲庫 Dockerfile 本身(即完成所有克隆並在 Docker 版本中安裝業務),因為每次都需要安裝一組非常具體的公司存儲庫——但正如您可以想象的那樣,身份驗證不起作用。

我試圖添加像

RUN git config --global user.name <my_username>
RUN git config --global user.password <personal_access_token>
RUN pip install -e git+https://github.com/my_company_name/colleague_repo

到我的 Dockerfile (並且還在第二行嘗試了我的實際密碼)。 沒用 - 收到身份驗證失敗的相同消息。

有誰能幫忙嗎?

您可以使用 SSH 密鑰,采用多階段方法,如Vladislav Sulova的“ 從 Dockerfile 訪問私有存儲庫而不留下 SSH 密鑰”中所示

# this is our first build stage, it will not persist in the final image
FROM ubuntu as intermediate

# install git
RUN apt-get update
RUN apt-get install -y git

# add credentials on build
ARG SSH_PRIVATE_KEY
RUN mkdir /root/.ssh/
RUN echo "${SSH_PRIVATE_KEY}" > /root/.ssh/id_rsa

# make sure your domain is accepted
RUN touch /root/.ssh/known_hosts
RUN ssh-keyscan bitbucket.org >> /root/.ssh/known_hosts

RUN git clone git@bitbucket.org:your-user/your-repo.git

FROM ubuntu
# copy the repository form the previous image
COPY --from=intermediate /your-repo /srv/your-repo
# ... actually use the repo :)

更現代的方法是使用 BuildKit

使用新的 SSH 安裝類型,您可以允許 Docker 構建使用主機的 SSH 密鑰。

這是它的樣子:

 RUN --mount=type=ssh...

您將新的掛載類型添加到您的 RUN 命令中,整個過程都會為您處理好。

請參閱BuildKit / Dockerfile 前端語法/ RUN --mount=type=ssh

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM