首頁 > 後端開發 > Golang > 主體

如何使用 Docker 容器中的私有儲存庫進行驗證

WBOY
發布: 2024-02-09 09:40:19
轉載
598 人瀏覽過

如何使用 Docker 容器中的私有存储库进行身份验证

php小編草莓為您介紹如何使用 Docker 容器中的私人儲存庫進行身份驗證。 Docker 是一種流行的容器化平台,能夠幫助開發人員在不同的環境中快速部署和運行應用程式。然而,對於一些敏感的應用程式或私有的程式碼庫,我們可能需要對容器中的儲存庫進行身份驗證,以確保只有授權的人員可以存取。本文將向您展示如何設定和使用私有儲存庫的身份驗證,以保護您的敏感資料和程式碼。

問題內容

我有一個git 儲存庫,它是一個私人儲存庫,我需要能夠對其進行身份驗證,並能夠在運行時在container build 視角中查看它。有關一些背景信息,我有一個 github 工作流程,用於構建容器映像並將其發佈到 ghcr.io 註冊表。但是,因為我的套件依賴的儲存庫是私有的,所以它不起作用。現在它可以在本地運行,我考慮過更改存儲 github 身份驗證的方式以允許我訪問它,但我想知道是否有人知道更好的方法讓我訪問私有存儲庫。 p>

以下是發佈到 ghcr.io 註冊表的 github 操作:

name: docker dataeng_github_metrics

# run workflow on tags starting with v (eg. v2, v1.2.0)
on:
  push:
    branches: [ "master" ]
    paths:
      - ./data_pipelines/dataeng_github_metrics/*
  pull_request:
    branches: [ "master" ]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: checkout code
        uses: actions/checkout@v1
        
      - name: login to github container registry
        uses: docker/login-action@v1
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.ghcr_registry_token }}

      - name: set up docker buildx
        uses: docker/setup-buildx-action@v2

      - name: build and push docker image
        uses: docker/build-push-action@v3
        with:
          context: ./data_pipelines/dataeng_github_metrics/
          file: ./data_pipelines/dataeng_github_metrics/dockerfile
          push: true # will only build if this is not here
          tags: |
            ghcr.io/mirantis/dataeng_github_metrics:latest
          # todo: i cannot use dataeng as public and need to change the way gitconfig is used in the dockerfile for authentication
          secrets: |
            token=${{ secrets.automation_pat}}
登入後複製

這是 dockerfile

###############
# cache image #
###############
arg go_image=golang:1.17.3-alpine3.14
arg base_image=alpine:3.14.2

from ${go_image} as cache
# add the keys
arg github_id
env github_id=$github_id
arg github_token
env github_token=$github_token

# install git
run apk add git

# todo: encrypt the github_id and github_token
# make git configuration
run git config \
    --global \
    url."https://${github_id}:${github_token}@github.com/".insteadof \
    "https://github.com/"

workdir /src
copy go.mod go.sum /src/
run go mod download

##############
# base image #
##############
from cache as dataeng_github_metrics
copy . /bin
workdir /bin

# setup git terminal prompt & go build
run go build .

###############
# final image #
###############
from ${base_image}
copy --from=dataeng_github_metrics /bin/dataeng_github_metrics bin/
entrypoint [ "bin/dataeng_github_metrics" ]
登入後複製

我認為讓我困惑的重要部分是這個,但想知道是否有更好的方法來實現它:

# make git configuration
run git config \
    --global \
    url."https://${github_id}:${github_token}@github.com/".insteadof \
    "https://github.com/"
登入後複製

如何存取私有儲存庫並避免工作流程中出現以下錯誤:

#14 9.438   remote: Repository not found.
#14 9.438   fatal: Authentication failed for 'https://github.com/Mirantis/dataeng/'
------
Dockerfile:26
--------------------
  24 |     WORKDIR /src
  25 |     COPY go.mod go.sum /src/
  26 | >>> RUN go mod download
  27 |     
  28 |     ##############
--------------------
ERROR: failed to solve: process "/bin/sh -c go mod download" did not complete successfully: exit code: 1
Error: buildx failed with: ERROR: failed to solve: process "/bin/sh -c go mod download" did not complete successfully: exit code: 1
登入後複製

解決方法

dockerfile 中,為了使用操作傳遞的金鑰(稱為token),您應該以下列方式執行:

RUN --mount=type=secret,id=TOKEN \
    echo "machine github.com login x password $(head -n 1 /run/secrets/TOKEN)" > ~/.netrc && \
git config \
    --global \
    url."https://${GITHUB_ID}:${TOKEN}@github.com/".insteadOf \
    "https://github.com/"
登入後複製

記得將 github_id 也傳遞給 dockerfile

以上是如何使用 Docker 容器中的私有儲存庫進行驗證的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:stackoverflow.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!