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

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

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

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