I'm working on a go project which imports a private package. The private package repo is inside a subgroup in gitlab. I have my development environment set up using a ~/.netrc file and setting GOPRIVATE="gitlab.mycompany.io" and everything works fine.
However, running go mod download during the docker build always fails.
What is REALLY WIERD is that if I build a container which includes all steps before RUN go mod download I can run the container interactively and execute go mod download inside the container without issue.
Here is what my Dockerfile looks like:
FROM golang:latest AS builder
# Adds .netrc
ARG NETRC
RUN echo $NETRC > ~/.netrc
ENV GOPRIVATE="gitlab.mycompany.io/*"
ARG SSH_PRIVATE_KEY
RUN mkdir ~/.ssh && echo "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
RUN echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa && chmod 0600 ~/.ssh/id_rsa
WORKDIR /app
COPY . .
RUN go mod download && go mod verify
RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="-w -s"
# FROM scratch (...etc.)
I am running this with the following command, where NETRC and SSH_PRIVATE_KEY contain the cat output of those files.
docker build --no-cache --build-arg NETRC=$NETRC --build-arg SSH_PRIVATE_KEY=$SSH_PRIVATE_KEY -t test-downloader .
This Dockerfile fails with:
remote:
remote: ========================================================================
remote:
remote: The project you were looking for could not be found.
remote:
remote: ========================================================================
remote:
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Can anyone help me understand:
- Is there a difference between environments when docker runs a command as a build step and when I run a container interactively?
- How to make this work? :)
Additional info:
- The .netrc, .gitconfig, and ssh key look as they should inside the container.
- I have tried authenticating with
.git-credentialsand git url (usinginsteadOfin git config). This fails with the same error and also fails when I rungo mod downloadinside a container that has all preceding steps. - I have tried using all manner of replace directives in the
go.modfile in order to point mod to the correct package, one such attempt was:
...
gopkg.in/yaml.v2 v2.2.7
)
replace gitlab.mycompany.io/maingroup/subgroup/myapp v0.0.0 => gitlab.mycompany.io/maingroup/subgroup/myapp.git v0.0.0