开发在Docker容器中运行的Golang应用的最佳实践/方法是什么?

huangapple go评论84阅读模式
英文:

Best practice/way to develop Golang app to be run in Docker container

问题

基本上,标题所说的就是...有没有一种最佳实践或高效的方法来开发一个将被Docker化的Golang应用程序?我知道你可以挂载卷来指向你的源代码,对于像PHP这样不需要编译代码的语言来说,这种方法非常好用。但是对于Go来说,似乎在Docker旁边开发会很麻烦,因为我猜你只有两个选择。

第一种方式是使用一个只有onbuild指令的Dockerfile,这样当容器运行时它会启动Go应用程序,因此每次更改(无论是小的还是大的)都需要构建一个新的镜像。或者,你可以将源代码目录挂载到容器目录,然后连接到容器本身,并像通常一样手动进行Go的构建/运行。

除非你不在Docker容器中开发Go应用程序,否则这两种方式似乎是唯一的选择。只需正常开发,然后在准备运行时使用scratch镜像方法,将Go预先构建为二进制文件,然后将其复制到容器中。我认为这可能是最好的方法,但我想向专业人士请教一下这个问题,并可能获得一些反馈。

英文:

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.

答案1

得分: 9

不确定这是否是最佳实践,但这是我的方式。

  1. Makefile是必需的。
  2. 对于小的迭代,我使用我的本地机器和Go工具。
  3. 使用基于golang:{1.X,latest}的专用构建容器,将代码目录挂载到构建发布版本,主要是为了确保我的代码能够在CI上正确构建。(提示:这是我用于发布构建的标准Go构建命令:CGO_ENABLED=0 GOGC=off go build -ldflags -s -w
  4. 测试代码。
  5. 然后使用FROM scratch构建发布容器(复制二进制文件+入口点)。
  6. 将镜像推送到注册表。

步骤3到6是CI的任务。

重要提示:由于新的多阶段构建功能(https://docs.docker.com/engine/userguide/eng-image/multistage-build/),这些步骤正在发生变化,不再有构建容器和发布容器的区分。

构建容器和发布容器将合并为一个多阶段构建,因此只需要一个Dockerfile(语法可能不正确,但你会明白的):

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"]
英文:

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 : https://docs.docker.com/engine/userguide/eng-image/multistage-build/, 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" ]

答案2

得分: 3

最近,我一直在使用https://github.com/thockin/go-build-template作为我所有项目的基础。该模板附带了一个Makefile,可以在Docker中构建/测试您的应用程序。

英文:

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

答案3

得分: 0

根据你的问题,我理解你想要一个正在运行的容器来开发一个Go语言应用程序。同样的事情也可以在你的主机上完成。但好消息是,如果你能构建这样的应用程序,那将被视为云平台即服务(PaaS)。

容器的基本要求包括:Ubuntu镜像和其他软件包,如编辑器、Go语言编译器等。

我建议你查看Docker开发环境。
https://docs.docker.com/opensource/project/set-up-dev-env/

Docker开发环境在一个容器内运行,文件从主机目录挂载。容器镜像是从Ubuntu scratch镜像构建的,并添加了编译Docker源代码所需的软件包。

希望你已经基本找到了你要的东西。

英文:

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.
https://docs.docker.com/opensource/project/set-up-dev-env/

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.

huangapple
  • 本文由 发表于 2017年5月19日 12:50:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/44061696.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定