2017-12-02 17:47

docker构建go app私有github


I'm trying to build a docker image from my go app. The github repo is private. I'm a bit new to go (and docker), but I thought that the /vendor file managed through glide should circumvent the problem? Also why does docker have to get everything from github? Everything is available locally.


docker build -t testimage .


FROM golang:1.8-onbuild

It fails at the step

exec go get -v -d
fatal: could not read Username for 'https://github.com': No such device or address

How can I get around this? Thx

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • duanjiongzhen2523 duanjiongzhen2523 4年前

    Only use golang:1.8-onbuild if you need to compile your go code image INSIDE a container. That's typically useful for CI builds. Otherwise avoid since it's a massive image.

    A much much faster solution is to build your Go application locally (dev env for instance) and copy the final Go application to a very lightweight container.

    I'll give you our standard process.

    1. If your local machine is a Mac or Windows, you need to cross-compile your Go code for linux using: GOOS=liux GOARCH=amd64 go build -o myapp_linux-amd64. The linux-amd64 is just a convention to remind yourself that the file is compiled for linux, not mac or windows.

    2. We also deploy our Go apps to the very lightweight Alpine linux container. Alpine is now the standard Docker image to create app. It's very small and secure but it has one major quirk; it is using the musc instead of the more common glibc as the underlying OS/IO library, so we need a few more compilation flags: -a -ldflags '-w -extldflags "-static"'

    3. As an extra, we also remove the developer's own path in the filename listed in a stacktrace using: -gcflags=-trimpath=$(pwd) -asmflags=-trimpath=$(pwd)

    The resulting compile command that we use is: CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOROOT_FINAL=$(pwd) go build -a -ldflags '-w -extldflags "-static"' -gcflags=-trimpath=$(pwd) -asmflags=-trimpath=$(pwd) -o myapp_linux-amd64

    You can now build your app locally on your dev env and create the image using the following Dockerfile:

    FROM alpine:3.6
    COPY ./myapp_linux-amd64 /usr/local/bin/myapp
    CMD /usr/local/bin/myapp

    build it using:

    docker build -t myimagename:tag .

    点赞 评论 复制链接分享
  • donglie1898 donglie1898 4年前

    If you have /vendor inside the root directory of your project/package, it should not pull anything from the internet. Here's how I did mine: https://gist.github.com/orvyl/7000f973abe88d40c8c91d4e28e139a7

    点赞 评论 复制链接分享
  • duanliang8464 duanliang8464 4年前

    Is there anything else in the Dockerfile other than FROM? What docker command are you using to turn on the container? If you're just trying to build a docker image and that's failing, there shouldn't be any interaction or authorization with github unless defined in the Dockerfile.

    Can you try running this? It will pull down the official Go Alpine image and boot you into an interactive terminal.

    docker run -it golang:1.9.2-alpine3.6 sh

    Alternatively you could also use your image.

    docker run -it golang:1.8-onbuild sh

    If that works, the issue is probably with the Dockerfile and not with Docker or the Go image.

    If you want to know more about Docker and Go checkout the official repository. I also recently wrote about Docker and Go and how to get up and running with it quickly.

    点赞 评论 复制链接分享