簡體   English   中英

如何創建一個依賴於托管在 gitlab 子組中的私有 go 包的 docker 鏡像?

[英]How to create a docker image which depends on private go packages that are hosted within a subgroup in gitlab?

我正在開發一個導入私有包的 go 項目。 私有包 repo 位於 gitlab 的一個子組中。 我使用~/.netrc文件設置了我的開發環境並設置了GOPRIVATE="gitlab.mycompany.io"並且一切正常。

但是,在 docker build 期間運行go mod download總是失敗。

真正奇怪的是,如果我構建一個包含RUN go mod download之前所有步驟的容器,我可以交互地運行容器並在容器內執行go mod download而不會出現問題。

這是我的Dockerfile樣子:

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.)

我使用以下命令運行它,其中NETRCSSH_PRIVATE_KEY包含這些文件的 cat 輸出。

docker build --no-cache --build-arg NETRC=$NETRC --build-arg SSH_PRIVATE_KEY=$SSH_PRIVATE_KEY -t test-downloader .

這個Dockerfile失敗了:

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.

誰能幫我理解:

  1. 當 docker 將命令作為構建步驟運行時和我以交互方式運行容器時,環境之間是否存在差異?
  2. 如何使這項工作? :)

附加信息:

  1. .netrc、.gitconfig 和 ssh 密鑰在容器內看起來應該如此。
  2. 我曾嘗試使用.git-credentials和 git url 進行身份驗證(在 git config 中使用insteadOf )。 這失敗並出現相同的錯誤,並且當我在具有所有前述步驟的容器中運行go mod download時也會失敗。
  3. 我已經嘗試在go.mod文件中使用各種替換指令來將 mod 指向正確的包,這樣的嘗試是:
    ...
    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


對我有用的替代解決方案

也許不是您正在尋找的答案,但對我有用的方法是不要嘗試在您的 Dockerfile 中進行任何類型的身份驗證或憑據內容,而是繼續供應應用程序。 我個人不喜歡必須有一個供應商文件夾,但這比必須在 Docker 中處理身份驗證要好。

您將在 Docker 命令之外運行go mod tidygo mod vendor ,提交該文件,然后您的 Dockerfile 將是這樣的(路徑和工作目錄可能不是正確的,相關部分是如何構建):

FROM golang:1.13-alpine AS build-stage

WORKDIR /app

COPY . /app
RUN go build -mod=vendor

# Final Stage
FROM alpine

RUN mkdir /app
WORKDIR /app

# Copy only the binary so that your Docker image does not have the uncompiled code.
COPY --from=build-stage  /app/APP_NAME .

EXPOSE 3000

ENTRYPOINT ./APP_NAME

我沒有嘗試過的更清潔的解決方案

另一個解決方案是弄清楚如何使用 GOPROXY。 我還沒有嘗試過該解決方案,但它會是一種更清潔的方法。 你可以使用類似雅典的東西: https : //docs.gomods.io/configuration/authentication/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM