dongpu1315 2017-01-18 14:49
浏览 182
已采纳

为golang静态二进制文件构建,部署,推送docker映像

I am looking for a solution to a simple configuration problem to solve; it has been nagging me for quite some time now. :)

I have a golang project on github which gives me a static binary, and uses godeps.

Now I want to ensure that the godep go install ... command can be run after a git clone and a docker container be built from this newly built binary locally.

As an option, the user should be able to push it to docker hub or a private repo as applicable.

I am thinking of using Makefiles, but that seems too complicated (set the gopath, then godep build, modify the Dockerfile dynamically to point to the place where the binary is located, then a docker build).

Is there an easier way to do it?

  • 写回答

2条回答 默认 最新

  • dongnue2071 2017-01-19 19:44
    关注

    So far, when I've been on your situation, I've always come up with a Makefile for doing all the work, which, like you said, has never been simple. Although it's never been simple, I've done it using at least 2 different approaches dependending on the level of dependency you want between the build process and the development environment.

    The simplest way is, as you say, just throw in a Makefile the steps you would do by yourself. You can use Dockerfiles ARGuments to parameterize the binary name so you don't have to modify the Dockerfile during the build.

    Here you have a quick and dirty (working) Makefile I've just made up for getting you started:

    APP_IMAGE=group/example
    APP_TAG=1.0
    APP_BINARY=example
    
    .PHONY: clean image binary
    
    all: image
    
    clean:
        if [ -r $(APP_BINARY) ]; then rm $(APP_BINARY); fi
        if [ -n "$$(docker images -q $(APP_IMAGE):$(APP_TAG))" ]; then docker rmi $(APP_IMAGE):$(APP_TAG); fi
    
    image: binary
        docker build --build-arg APP_BINARY=$(APP_BINARY) -t $(APP_IMAGE):$(APP_TAG) $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
    
    binary: $(APP_BINARY)
    
    $(APP_BINARY): main.go
        go build -o $@ $^
    

    That Makefile expects to be in the same directory as the Dockerfile.

    Here is a minimal one (which works):

    FROM alpine:3.5
    ARG APP_BINARY
    ADD ${APP_BINARY} /app
    ENTRYPOINT /app
    

    I've tested that having a main.go in the same top-level project directory as both Makefile and Dockerfile, in case you have a main.go nested inside some inside directory ("ROOT/cmd/bla" is commonplace) then you should change the "go build" line to account for that.

    Although I've been doing things like that for a while, now that I see (and think about) your question I've come to see that a dedicated tool which knows specifically how to do this could be great to have. Specifically a tool that mimics "go build/get/install" but can build docker images... so where you can run the following command to get a binary:

    go install github.com/my/program
    

    You could also run the following command to get a simple docker image:

    goi install github.com/my/program
    

    How does that sound? Is there something like that in existence? Should I get started?

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

报告相同问题?

悬赏问题

  • ¥33 集成BiLSTM模型和GCN模型时,前模型的输出不匹配后模型的输入
  • ¥15 关于液体厚度提取,和铺展轨迹数据化处理
  • ¥20 对方微信是wxid如何添加成好友?或者转换成微信号?
  • ¥15 yolov5实验数据好,但是自己拍摄的照片检测结果差
  • ¥15 做过dali2.0认证的技术,
  • ¥15 一道python难题
  • ¥15 matlab 免疫算法
  • ¥50 可视化界面构建,实现信息模块功能
  • ¥15 基于bert-base-chinese的中文文本处理
  • ¥15 高价求抖音抓包技术,抖音抓包无网络问题