在Docker中使用私有模块构建Go应用

问题描述:

我正在尝试在依赖于私有子模块的docker容器中构建go项目.

I'm trying to build a go project in a docker container that relies on private submodules.

我希望-mount = type = ssh 可以将我的ssh凭据传递给容器,并且可以正常工作.目前,我可以在本地进行构建,只需设置 GOPRIVATE 变量和更新 git config .

I was hoping that --mount=type=ssh would pass my ssh credentials to the container and it'd work. Currently I can build locally with just make the GOPRIVATE variable set and the git config update.

这是我目前相关的 Dockerfile

# syntax = docker/dockerfile:experimental

FROM golang:1.14.3-alpine AS build
RUN apk add --no-cache git \
                openssh-client \
                ca-certificates

WORKDIR /src
ENV GIT_TERMINAL_PROMPT=1
ENV GOPRIVATE="gitlab.com/company_foo"
RUN git config --global url."ssh://git@gitlab.com".insteadOf "https://gitlab.com"



# Authorize SSH Host
# Skip Host verification for git
RUN mkdir -p /root/.ssh && \
    chmod 0700 /root/.ssh && \
    ssh-keyscan gitlab.com > /root/.ssh/known_hosts &&\
    chmod 644 /root/.ssh/known_hosts && touch /root/.ssh/config \
    && echo "StrictHostKeyChecking no" > /root/.ssh/config

COPY go.mod go.sum .
RUN --mount=type=ssh mkdir -p /var/ssh && \
    GIT_SSH_COMMAND="ssh -o \"ControlMaster auto\" -o \"ControlPersist 300\" -o \"ControlPath /var/ssh/%r@%h:%p\"" \
    go mod download
COPY . .
RUN --mount=type=cache,target=/root/.cache/go-build go build -o api-server ./cmd/api-server
RUN --mount=type=cache,target=/root/.cache/go-build go build -o migrations ./cmd/migrations

我也尝试过使用

CI_JOB_TOKEN

RUN echo -e "machine gitlab.com\nlogin gitlab-ci-token\npassword ${CI_JOB_TOKEN}" > ~/.netrc

但是这也不起作用.也许我做错了.

but this also didn't work. Perhaps I did it wrong.

所有这些都会导致失败:

All of this results in the failure:

 revision v0.0.3: unknown revision v0.0.3

与我们的一个私人仓库有关.

relating to one of our private repos.

任何建议将不胜感激.

我绝对迷失了.

这对我有用.

FROM golang:1.14
ARG USERNAME=user1
ARG PASSWORD=secret

WORKDIR /app
ADD . .
ENV GOPRIVATE=private.git.local/*
RUN echo "machine private.git.local login $USERNAME password $PASSWORD" > ~/.netrc

RUN go build -o testGo main.go
CMD ["/app/testGo"]