2017-05-19 04:50
浏览 106


Basically what the title says... Is there a best practice or an efficient way to develop a Golang app that will be Dockerized? I know you can mount volumes to point to your source code, and it works great for languages like PHP where you don't need to compile your code. But for Go, it seems like it would be a pain to develop alongside Docker since you pretty much only have two options I guess.

First would be to have a Dockerfile that is just onbuild so it starts the go app when a container is run, thus having to build a new image on every change (whether it be small or not). Or, you do mount your source code dir to the container dir, then attach to the container itself and do the manual go build/run yourself as if you would normally.

Those two ways are really the only way that I see it happening unless you just don't develop your Go app in a docker container. Just develop it as normal, then use the scratch image method where you pre build the Go into a binary then copy that into your container when you are ready to run it. I assume that is probably the way to go, but I wanted to ask more professional people on the subject and maybe get some feedback on the topic.

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

3条回答 默认 最新

  • douwo1517
    douwo1517 2017-05-19 07:45

    Not sure it's the best pratice but here is my way.

    1. Makefile is MANDATORY
    2. Use my local machine and my go tools for small iterations
    3. Use a dedicated build container based on golang:{1.X,latest}, mount code directory to build a release, mainly to ensure that my code will build correctly on the CI. (Tips, here is my standard go build command for release build : CGO_ENABLED=0 GOGC=off go build -ldflags -s -w)
    4. Test code
    5. Then use a FROM scratch to build a release container (copy the bin + entrypoint)
    6. Push you image to your registry

    Steps 3 to 6 are tasks for the CI.

    Important note : this is changing due to the new multistage builds feature :, no more build vs release containers.

    Build container and release container will be merged in one multistage build so one Dockerfile with (not sure about the correct syntax but, you will get the idea) :

    FROM golang:latest as build
    WORKDIR /go/src/myrepos/myproject
    RUN go build -o mybin
    FROM scratch
    COPY --from=build /go/src/myrepos/myproject/mybin /usr/local/bin/mybin
    ENTRYPOINT [ "/usr/local/bin/mybin" ]
    点赞 评论
  • douwaz34842
    douwaz34842 2017-05-19 08:04

    As far as I understood from you question, you want to have a running container to develop a golang application. The same thing can be done in your host machine also. But good thing is that if you could build such application, then that will be consider as cloud Platform-as-a-Service(PaaS).

    The basic requirement of the container will be: Ubuntu image and other packages such as editor, golang compiler and so on.

    I would suggest to look on the docker development environment.

    The docker development environment is running inside a container and the files are mounted from one of the host directory. The container image is build from Ubuntu scratch image and added required packages which are needed to compile docker source code.

    I hope you almost got what you are looking for.

    点赞 评论
  • dtq26360
    dtq26360 2017-05-19 16:28

    Lately, I've been using As a base for all of my projects. The template comes with a Makefile that will build/test your application in a Docker.

    点赞 评论