如何使用Bitbucket和Golang项目从Dockerfile下载私有仓库?

huangapple go评论85阅读模式
英文:

How to download private repo from Dockerfile with bitbucket and golang project

问题

我想从Bitbucket下载一个私有仓库,但是遇到了一些错误。

fatal: could not read Username for 'https://bitbucket.org': terminal prompts disabled

这是我的Dockerfile:

FROM golang:1.17 as build

RUN apt update && apt upgrade -y && \
  apt install -y git \
  make openssh-client 

WORKDIR /src 

COPY . . 

RUN git config --global url."https://username:app_password@bitbucket.org".insteadOf "https://bitbucket.org"
RUN go mod tidy

RUN go build -o user-management

请注意,这只是翻译的结果,我无法执行任何代码或提供实际的帮助。

英文:

I want download a private repository from bitbucket, but get some error

fatal: could not read Username for 'https://bitbucket.org': terminal prompts disabled

here my dockerfile

FROM golang:1.17 as build

RUN apt update && apt upgrade -y && \
  apt install -y git \
  make openssh-client 

WORKDIR /src 

COPY . . 


RUN git config --global url."https://username:app_password@bitbucket.org".insteadOf "https://bitbucket.org"
RUN go mod tidy

RUN go build -o user-management

答案1

得分: 4

我认为最干净的方法是使用SSH密钥。实际上,你可以告诉Docker使用本地SSH代理的配置。为此,你需要启用BuildKit。请参阅文档

eval "$(ssh-agent -s)"
ssh-add /path/to/ssh-key
docker build --ssh default --tag example .

在你的Dockerfile中,你需要在特定的运行指令上挂载它:

RUN --mount=type=ssh go mod download

这要求你将相同的SSH密钥添加到你的私有Bitbucket仓库中,以便你可以使用它来下载依赖项。


还有其他方法可以在运行指令的生命周期内挂载密钥。请参阅文档

英文:

I think it's most clean to use an ssh key for that. You can actually tell docker to use the config from your local ssh agent. You need to enable buildkit for this. See the docs.

eval "$(ssh-agent -s)"
ssh-add /path/to/ssh-key
docker build --ssh default --tag example .

In your Dockerfile, you need to mount this on a specific run instruction:

RUN --mount=type=ssh go mod download

That requires you adding the same ssh key to your private bitbucket repo, so that you can use it to download dependencies.


There are more ways to mount secrets for the lifetime of a run instruction. See the docs.

答案2

得分: 1

对于我来说,帮助添加"machine github.com login USERNAME password APIKEY"到$HOME/.netrc

我的Dockerfile:

FROM golang:latest as builder
ARG GIT_CONFIG
WORKDIR /builder/
ADD . /builder/
RUN "${GIT_CONFIG}" > $HOME/.netrc
&& CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main ./cmd/main.go
&& rm -f $HOME/.netrc

FROM scratch
COPY --from=builder /builder/main/ .

也许这会对你有所帮助。

英文:

For me helped add "machine github.com login USERNAME password APIKEY
" to $HOME/.netrc

My Dockerfile:

FROM golang:latest as builder
ARG GIT_CONFIG
WORKDIR /builder/
ADD . /builder/
RUN "${GIT_CONFIG}" > $HOME/.netrc
&& CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main ./cmd/main.go
&& rm -f $HOME/.netrc

FROM scratch
COPY --from=builder /builder/main/ .

Maybe it will help you.

答案3

得分: -1

永远不要这样做,你需要在docker build命令之外下载存储库,并使用COPY将这些文件传输到镜像中进行构建。

英文:

never do that, you need download the repo outside the docker build command, and use COPY to transfer these files into images, when build

huangapple
  • 本文由 发表于 2022年5月14日 18:30:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/72239378.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定