简体   繁体   English

"Docker:从私有 GitHub 存储库中获取"

[英]Docker: go get from a private GitHub repo

I'm trying to run a container that will expose a golang service from a package that I have on a private GitHub repo.我正在尝试运行一个容器,该容器将从我在私有 GitHub 存储库中拥有的包中公开 golang 服务。

Since I am working with GCE, my starter image is google\/debian:wheezy.因为我在使用 GCE,所以我的起始图像是 google\/debian:wheezy。

After installing all the required dependancies and tools, I am running安装所有必需的依赖项和工具后,我正在运行

RUN go get github.com/<my_org>/<my_package>

go get is trying to use https, completely ignoring ssh. go get试图使用https,完全忽略ssh。

You will have to setup ~/.netrc : 你必须设置~/.netrc

ADD priv/.netrc /root/.netrc

Where netrc looks like: netrc看起来像:

machine github.com login github-username password github-password

ref: 参考:

I figured this out after a bit of hacking around. 经过一番黑客攻击后,我想出了这个。 Not an ideal solution as it involves installing SSH, plus building a private key into the container. 这不是一个理想的解决方案,因为它涉及安装SSH,以及在容器中构建私钥。 This example is based on the official Docker golang image (Debian Wheezy): 这个例子基于官方的Docker golang图像 (Debian Wheezy):

The main difference to your example is that you need a git config command to force ssh instead of the default https. 与您的示例的主要区别在于您需要git config命令来强制ssh而不是默认的https。

FROM golang

RUN apt-get update && apt-get install -y ca-certificates git-core ssh

ADD keys/my_key_rsa /root/.ssh/id_rsa
RUN chmod 700 /root/.ssh/id_rsa
RUN echo "Host github.com\n\tStrictHostKeyChecking no\n" >> /root/.ssh/config
RUN git config --global url.ssh://git@github.com/.insteadOf https://github.com/

ADD . /go/src/github.com/myaccount/myprivaterepo

RUN go get github.com/myaccount/myprivaterepo
RUN go install github.com/myaccount/myprivaterepo

Elaborating on OneOfOne's ~/.netrc answer, this is what I am doing with Jenkins on linux: 详细阐述OneOfOne的~/.netrc答案,这就是我在Linux上使用Jenkins做的事情:

FROM golang:1.6

ARG GITHUB_USER=$GITHUB_USER
ARG GITHUB_PASS=$GITHUB_PASS

# Copy local package files to the container's workspace.
ADD . /go/src/github.com/my-org/my-project
WORKDIR /go/src/github.com/my-org/my-project/

# Build application inside the container.
RUN echo "machine github.com\n\tlogin $GITHUB_USER\n\tpassword $GITHUB_PASS" >> ~/.netrc && \
    go get github.com/tools/godep && \
    go get github.com/onsi/ginkgo/ginkgo && \
    godep restore && \
    ginkgo -r --randomizeAllSpecs --randomizeSuites --failOnPending && \
    godep go install && \
    rm -f ~/.netrc

ENTRYPOINT /go/bin/my-project

EXPOSE 8080

The docker build command is: docker build命令是:

docker build \
    --build-arg GITHUB_USER=xxxxx \
    --build-arg GITHUB_PASS=yyyyy \
    -t my-project .

The two ARG directives map --build-arg s so docker can use them inside the Dockerfile. 这两个ARG指令映射--build-arg因此docker可以在Dockerfile中使用它们。

The first and last lines of RUN create and remove the ~/.netrc . RUN的第一行和最后一行创建并删除~/.netrc

In Jenkins, I use the same creds from git pull in the build command. 在Jenkins中,我在构建命令中使用git pull中的相同信用。

In this strategy, the password is not echoed during the docker build process and not saved on any layer of your docker image. 在此策略中,密码不会在泊坞窗构建过程中回显,也不会保存在泊坞窗图像的任何图层上。 Also note that the gingko test results are printed to console during the build. 另请注意,在构建期间,银杏测试结果将打印到控制台。

In the newest version of golang (v1.11) there are now modules . 在最新版本的golang(v1.11)中,现在有模块

To quote the source: 引用来源:

A module is a collection of related Go packages that are versioned together as a single unit. 模块是相关Go包的集合,它们作为单个单元一起版本化。 Most often, a single version-control repository corresponds exactly to a single module. 通常,单个版本控制存储库与单个模块完全对应。

Using the latest version of golang will allow you to have dependencies that are in private repositories. 使用最新版本的golang将允许您拥有私有存储库中的依赖项。 Essentially by running the $ go mod vendor command will create a vendor directory locally for all external dependencies. 实质上,通过运行$ go mod vendor命令将在本地为所有外部依赖项创建vendor目录。 Now making sure your docker image has Golang v1.11, you will update your Dockerfile with the following: 现在确保您的docker镜像具有Golang v1.11,您将使用以下内容更新Dockerfile:

WORKDIR /<your repostiory>

COPY . ./

i had this problem in Github and i fix it using personal access token :我在 Github 中遇到了这个问题,我使用personal access token修复了它:

first of all please use ARG for your Dockerfile vars(inputs):首先,请为您的 Dockerfile vars(inputs) 使用ARG

after that configure your git with github personal access token之后使用 github 个人访问令牌配置您的 git

GITHUB_PAT -> github personal access token GITHUB_PAT -> github 个人访问令牌

FROM golang:1.17 as builder

ARG GITHUB_PAT

WORKDIR /your-app
COPY go.mod .
COPY go.sum .
RUN git config --global url."https://${GITHUB_TOKEN}:x-oauth-basic@github.com/".insteadOf "https://github.com/"
RUN go mod download
COPY . .
RUN go build -ldflags '-w -s' -o ./out ./main.go


FROM golang:1.17

WORKDIR /app

COPY --from=builder /your-app/out ./
WORKDIR /app/

ENTRYPOINT [ "./out" ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM