构建和运行 Go 可执行文件的 Docker 镜像

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

Building and running a docker image for a Go executable

问题

我正在尝试将我的Go应用程序容器化。我正在使用Docker来完成这个任务。我已经在我的系统上运行了一个完全可执行的Docker应用程序。为了在容器中运行它,我创建了一个Dockerfile。

FROM golang:1.7
EXPOSE "portno"

我保持我的Dockerfile非常简单,因为我已经在我的系统上运行了一个可执行文件。请帮助我添加哪些内容以使Go应用程序运行起来。我无法运行Go应用程序,因为许多内容没有被复制到容器中。

英文:

I am trying to containerize my Go applicaton. I am using Docker to do this. I have a fully executable Docker application running in my system. To run this in a container i am have created a Dockerfile.

FROM golang:1.7
EXPOSE "portno"

I have kept my dockerfile very simple because i already have an executable file running in my system. Please help me what all contents should i add to get the go app running. I am not able to run the go app as many of the contents are not getting copied in container.

答案1

得分: 4

你需要使用ADD命令将可执行文件添加到容器中:

ADD ./app /go/bin/app 

然后,你需要告诉Docker将其作为主要容器进程执行:

CMD ["/go/bin/app"]

请注意,最好在容器内部从源代码构建应用程序。可以在构建Docker镜像时完成此操作。
作为示例,可以参考这篇文章获取更多信息:http://thenewstack.io/dockerize-go-applications/

英文:

You need to add your executable file to your container using ADD command:

ADD ./app /go/bin/app 

And then you need to tell docker that it should be executed as the main container process:

CMD ["/go/bin/app"]

Note that it may be better to build your application from the source code inside your container. It can be done when you build your docker image.
As an example, see this article for more information: http://thenewstack.io/dockerize-go-applications/

huangapple
  • 本文由 发表于 2016年12月8日 12:09:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/41031622.html
匿名

发表评论

匿名网友

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

确定