在 Docker 中使用私有模块构建 Go 应用程序 [英] Building Go apps with private modules in Docker

查看:19
本文介绍了在 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

我也尝试使用

RUN echo -e "machine gitlab.com
login gitlab-ci-token
password ${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"]

这篇关于在 Docker 中使用私有模块构建 Go 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆