Home > Software engineering >  How to download private repo from Dockerfile with bitbucket and golang project
How to download private repo from Dockerfile with bitbucket and golang project

Time:05-15

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:[email protected]".insteadOf "https://bitbucket.org"
RUN go mod tidy

RUN go build -o user-management

CodePudding user response:

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

CodePudding user response:

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.

CodePudding user response:

Firstly, you can set up an ssh key on Bitbucket.

https://support.atlassian.com/bitbucket-cloud/docs/set-up-an-ssh-key/

Copy your private key to docker container with COPY.

Finally, run git clone via git protocol in Dockerfile.

Just like:

git clone [email protected]:accountname/reponame.git

  • Related