2017-01-18 14:49
浏览 157


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?

图片转代码服务由CSDN问答提供 功能建议

我正在寻找解决简单配置问题的解决方案; 现在已经困扰了我好一段时间了。 :)


现在,我要确保< 您可以在本地使用此新建二进制文件构建git clone和docker容器后运行code> godep go install ... 命令。

作为一种选择,用户 应该能够将其推送到docker hub或适用的私有存储库中。

我正在考虑使用Makefile,但这似乎太复杂了(设置gopath,然后godep构建,修改 Dockerfile动态地指向二进制文件所在的位置,然后是docker构建文件。)


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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:

    .PHONY: clean image binary
    all: image
        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
    ADD ${APP_BINARY} /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

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

    goi install

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

    点赞 打赏 评论
  • dongri1989 2017-01-19 08:01

    Provide a base image (with GOPATH, godep ... pre configured).

    Create Dockerfile based on the base image, then user only need to alter the COPY path.

    点赞 打赏 评论

相关推荐 更多相似问题