duandingyou3331 2017-12-02 17:47
浏览 78
已采纳

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.

command:

docker build -t testimage .

Dockerfile

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

  • 写回答

3条回答 默认 最新

  • duanjiongzhen2523 2017-12-03 04:17
    关注

    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
    
    ENTRYPOINT []
    CMD /usr/local/bin/myapp
    

    build it using:

    docker build -t myimagename:tag .

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料