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

在Docker中使用私有gitlab模块构建Go应用程序,git,docker,go,ssh,gitlab,Git,Docker,Go,Ssh,Gitlab,我正在尝试在docker文件上构建我的go应用程序。在my go.mod中有一个需要身份验证/ssh的私有包。这个问题类似于,但在我的例子中,我必须从gitlab而不是github中提取包。这是我的dockerfile: # builder image FROM golang:1.14.11-alpine AS builder # specific directory for build process WORKDIR /usr/src/build # copying the source

我正在尝试在docker文件上构建我的go应用程序。在my go.mod中有一个需要身份验证/ssh的私有包。这个问题类似于,但在我的例子中,我必须从
gitlab
而不是
github
中提取包。这是我的dockerfile:

# builder image
FROM golang:1.14.11-alpine AS builder

# specific directory for build process
WORKDIR /usr/src/build

# copying the source code 
# to the current working directory
COPY . .
RUN apk add --no-cache openssh-client
RUN apk add --no-cache git

# create ssh directory
RUN mkdir ~/.ssh
RUN touch ~/.ssh/known_hosts
RUN ssh-keyscan -t rsa gitlab.com >> ~/.ssh/known_hosts

# allow private repo pull
RUN git config --global url."https://my-personal-access-token:token@gitlab.com/".insteadOf "https://gitlab.com/"

ADD . /go/src/gitlab.com/my-repo/backends/backend-structs
CMD cd /go/src/gitlab.com/my-repo/backends/backend-structs; go get /go/src/gitlab.com/my-repo/backends/backend-structs && go build -o /go/bin/backend-structs


# executing build process
RUN GOOS=linux go build -ldflags="-s -w" -o app

# runtime image
FROM golang:1.14.11-alpine AS runtime

# create and use non-root user
# to increase container security 
# ref https://pythonspeed.com/articles/root-capabilities-docker-security/
RUN adduser myuser --disabled-password

USER myuser

WORKDIR /home/myuser

# copy the executable binary file from builder directory
# to the current working directory
COPY --from=builder /usr/src/build/app .

# exposing port
EXPOSE 8080

# run the application
CMD ["./app"]
我尝试按照本教程进行操作,将
github.com
更改为
gitlab.com
,但仍然失败

以下是错误详细信息:

#17 5.830       remote: HTTP Basic: Access denied
#17 5.830       fatal: Authentication failed for 'https://gitlab.com/my-repo/backends.git/'
------
executor failed running [/bin/sh -c GOOS=linux go build -ldflags="-s -w" -o app]: exit code: 1

这里的任何人都知道如何使用golang private package创建dockerfile(repo托管在gitlab.com上)?

根据我的经验,不要使用git配置来解决这个问题。仅使用
~/.netrc
。以下是专门为此制定的指南a:

我也会把它的内容贴在下面

问题
go
命令行工具需要能够从您的私有GitLab获取依赖项,但需要身份验证

这假设您的私有GitLab托管在
privategitlab.company.com

环境变量 建议使用以下环境变量:

export GO111MODULE=on
export GOPRIVATE=privategitlab.company.com
以上几行可能最适合您的shell启动,比如
~/.bashrc

解释
GO111MODULE=on
告诉Golang命令行工具您正在使用模块。我没有在没有使用 私人GitLab上的Golang模块

GOPRIVATE=privategitlab.company.com
告诉Golang命令行工具不要将公共internet资源用于主机名 已列出(如公共模块代理)

从您的私人GitLab获取个人访问令牌 为了进一步验证这些说明,请遵循。 我知道Golang命令行工具需要
read_api
作用域才能工作,我可能怀疑
read_repository
嗯,但是还没有证实这一点

设置
~/.netrc
为了让Golang命令行工具向GitLab进行身份验证,最好使用
~/.netrc
文件

要创建不存在的文件,请运行以下命令:

touch~/.netrc
chmod 600~/.netrc
现在编辑文件的内容以匹配以下内容:

machine privategitlab.company.com login USERNAME_HERE password TOKEN_HERE
Host privategitlab.company.com
  Hostname privategitlab.company.com
  User USERNAME_HERE
  IdentityFile ~/.ssh/id_rsa
其中,
USERNAME\u此处
替换为您的GitLab用户名,
TOKEN\u此处
替换为在 上一节

常见错误 不要设置一个全局git配置,其内容如下:

git config——全局url。“git@privategitlab.company.com:“.代替”https://privategitlab.company.com"
我相信在撰写本文时,Golang命令行工具并不完全支持SSH git,这可能会导致 与
~/.netrc
冲突

额外好处:SSH配置文件 要经常使用
git
工具,而不是Golang命令行工具,设置
~/.ssh/config
文件非常方便。 为此,请运行以下命令:

mkdir~/.ssh
chmod 700~/.ssh
touch~/.ssh/config
chmod 600~/.ssh/config
请注意,上面的文件和目录上的权限是SSH在其默认配置下工作所必需的 大多数Linux系统

然后,编辑
~/.ssh/config
文件以匹配以下内容:

machine privategitlab.company.com login USERNAME_HERE password TOKEN_HERE
Host privategitlab.company.com
  Hostname privategitlab.company.com
  User USERNAME_HERE
  IdentityFile ~/.ssh/id_rsa
请注意上述文件中的间距问题,如果文件不正确,将使文件无效

其中,
USERNAME\u HERE
是您的GitLab用户名,
~/.ssh/id\u rsa
是文件系统中ssh私钥的路径。
您已经将其公钥上载到GitLab。以下是。

您是否尝试过启用gitlab Go代理?我已经阅读了文档,上面写着:“GitLab的Go代理正在开发中,由于大型存储库存在潜在的性能问题,还没有准备好投入生产使用```你的解决方案帮助了我,谢谢