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

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


Solution 1:[1]

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

Solution 2:[2]

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.

Solution 3:[3]

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.

like:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bill
Solution 2
Solution 3