Docker: go get from a private GitHub repo

后端 未结 4 1001
天涯浪人
天涯浪人 2021-02-19 06:17

I\'m trying to run a container that will expose a golang service from a package that I have on a private GitHub repo.

Since I am working with GCE, my starter image is g

相关标签:
4条回答
  • 2021-02-19 07:11

    go get is trying to use https, completely ignoring ssh.

    You will have to setup ~/.netrc:

    ADD priv/.netrc /root/.netrc
    

    Where netrc looks like:

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

    ref:

    • https://stackoverflow.com/a/13724351/145587
    0 讨论(0)
  • 2021-02-19 07:13

    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. This example is based on the official Docker golang image (Debian Wheezy):

    The main difference to your example is that you need a git config command to force ssh instead of the default 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
    
    0 讨论(0)
  • 2021-02-19 07:19

    In the newest version of golang (v1.11) there are now modules.

    To quote the source:

    A module is a collection of related Go packages that are versioned together as a single unit. 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. Essentially by running the $ go mod vendor command will create a vendor directory locally for all external dependencies. Now making sure your docker image has Golang v1.11, you will update your Dockerfile with the following:

    WORKDIR /<your repostiory>
    
    COPY . ./
    
    0 讨论(0)
  • 2021-02-19 07:19

    Elaborating on OneOfOne's ~/.netrc answer, this is what I am doing with Jenkins on linux:

    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 \
        --build-arg GITHUB_USER=xxxxx \
        --build-arg GITHUB_PASS=yyyyy \
        -t my-project .
    

    The two ARG directives map --build-args so docker can use them inside the Dockerfile.

    The first and last lines of RUN create and remove the ~/.netrc.

    In Jenkins, I use the same creds from git pull in the build command.

    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.

    0 讨论(0)
提交回复
热议问题