【问题标题】:Docker build for Go project with GitLab private repositories使用 GitLab 私有存储库为 Go 项目构建 Docker
【发布时间】:2022-01-16 22:41:56
【问题描述】:

我在让我的构建与私有 GitLab 存储库中的依赖项一起工作时遇到了一些问题。总而言之,它是一个多阶段构建,但下面列出了我尝试构建 Go 项目的阶段。这对我来说在本地工作,所以在 Docker 中让它工作存在问题:

FROM golang:1.16.8-alpine3.14 as BuildStage

RUN apk update && apk add --no-cache git ca-certificates tzdata gcc libc-dev openssh-client bash

RUN mkdir /root/.ssh
RUN ssh-keyscan -H gitlab.com >> ~/.ssh/known_hosts

COPY localRsa /root/.ssh/id_rsa
RUN chmod 0400 /root/.ssh/id_rsa

RUN eval $(ssh-agent -s) && ssh-add /root/.ssh/id_rsa

WORKDIR $GOPATH/src/myproject
COPY . .

ENV GOPRIVATE="gitlab.com/MyGitLabUser"
RUN git config --global url."git@gitlab.com".insteadOf "https://gitlab.com"

RUN go mod download
RUN go mod verify

RUN GOOS=linux GOARCH=amd64 \
    go build -ldflags='-w -s -extldflags "-static"' -tags musl -a -o /go/bin/mybinary

我得到的错误信息:

转到:gitlab.com/MyProject/Sub1/Sub2/some-library@v0.0.6:阅读 gitlab.com/MyProject/Sub1/Sub2/some-library.git/go.mod 版本 v0.0.6:未知修订 v0.0.6

该版本肯定存在并且正在本地运行。我在某处遗漏了一步。


更新:

如果我添加,则从该阶段开始克隆项目:

git clone git@gitlab.com:MyProject/Sub1/Sub2/some-library.git

让我觉得我在 Go 配置或将 Go 与 Git 链接中遗漏了一些东西。

【问题讨论】:

  • 试试-x它可以帮助获得更多关于进程的细节go.dev/ref/mod#go-mod-download/pkg.go.dev/cmd/go#hdr-Compile_packages_and_dependencies
  • @mh-cbon 帮助我去一个新的地方。我看到一个错误:503 Service Unavailable (0.322s) 这有助于我看到一些新的东西 @RakeshGupta 我接下来会看看!
  • IIRC 这是 GitLab 的一个已知问题。我们最终使用了一个 Go 代理(通过 JFrog Artifactory)来解决这个问题......要在没有代理的情况下解决,请尝试将 .git 附加到您的私有模块。例如而不是 gitlab.example.com/path/to/project v1.2.3(按照惯例)使用 gitlab.example.com/path/to/project.git v1.2.3 来定义你的 go 模块。我不能 100% 确定这是否是完全相同的问题,但请参阅 here 以了解可能的问题描述和解决方法。
  • @sytech 我在我的go.mod 文件中有类似:replace gitlab.com/.../x => gitlab.com/.../x.git 这就是它在我的本地机器上工作的原因

标签: docker go ssh gitlab


【解决方案1】:

问题

将每个人在此处提供的所有内容汇总在一起(顺便说一句,谢谢)。我能够重新设计我的 Dockerfile 来完成我需要的工作并让事情正常工作!所以首先,让我来列举一下我最初提交的所有问题:

  1. 完全没有必要传递 SSH 密钥(请参阅 here - 提示@RakeshGupta)
  2. 使用go mod download -x 有助于在那里搜索更多具体信息(帽子提示@mh-cbon)
  3. 我能够简化更多(请参阅here - 帽子提示@sytech)
  4. 其中一件大事是我修正了一个错字,所以现在是:git config --global url."git@gitlab.com:".insteadOf "https://gitlab.com/"

更新的 Dockerfile

FROM golang:1.16.8-alpine3.14 as BuildStage

# setup Git & SSL (for getting dependencies)
RUN apk update && \
    apk add --no-cache git ca-certificates tzdata gcc libc-dev openssh-client && \
    update-ca-certificates

ENV GOPRIVATE="gitlab.com/MyProject"

RUN mkdir -p -m 0600 ~/.ssh && ssh-keyscan gitlab.com >> ~/.ssh/known_hosts
RUN git config --global url."git@gitlab.com:".insteadOf "https://gitlab.com/"

# setup an application user
ENV USER=appuser
ENV UID=10001

RUN adduser --disabled-password \
            --gecos "" \
            --home "/nonexistent" \
            --shell "/sbin/nologin" \
            --no-create-home \
            --uid "${UID}" "${USER}"

# build the project
WORKDIR $GOPATH/src/myproject
COPY . .

# make sure Go knows the packages are private
RUN go env -w GOPRIVATE="gitlab.com/MyProject/*"

# build the binary
RUN --mount=type=ssh go mod download -x && go mod verify
RUN --mount=type=ssh GOOS=linux GOARCH=amd64 go build -ldflags='-w -s -extldflags "-static"' -tags musl -a -o /go/bin/mybinary

更新的构建命令

这很可爱,但我需要BuildKit with SSH

eval "$(minikube docker-env)"
DOCKER_BUILDKIT=1 docker build --ssh default -t myservice:latest .

【讨论】:

    猜你喜欢
    • 2015-06-24
    • 2021-04-25
    • 1970-01-01
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    • 2021-06-19
    • 2019-02-20
    • 1970-01-01
    相关资源
    最近更新 更多