使用 Docker 容器内的私有存储库进行身份验证的步骤
来源:stackoverflow
时间:2024-02-07 12:18:22 153浏览 收藏
学习知识要善于思考,思考,再思考!今天golang学习网小编就给大家带来《使用 Docker 容器内的私有存储库进行身份验证的步骤》,以下内容主要包含等知识点,如果你正在学习或准备学习Golang,就都不要错过本文啦~让我们一起来看看吧,能帮助到你就更好了!
问题内容
我有一个 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
以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。
声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习