构建一个小型容器来运行编译后的Go代码。

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

Building small container for running compiled go code

问题

从https://docs.docker.com/articles/baseimages/中,我正在尝试构建一个基础镜像来运行编译的Go代码,源自https://github.com/tianon/dockerfiles/tree/master/true。

  1. 我尝试将true.go复制到Docker中,然后执行时出现错误:"exec: "/true": permission denied"。
  2. 我还尝试通过bash进入其中,然后执行时出现错误:"bash: executable file not found in $PATH"。
  3. 我还尝试使用debootstrap raring raring > /dev/null,然后执行时出现错误:"bash: executable file not found in $PATH"。

你如何解决这个问题?

谢谢。

英文:

From
https://docs.docker.com/articles/baseimages/

I am trying to build a base image to run compiled go code, from:
https://github.com/tianon/dockerfiles/tree/master/true

  1. I have tried to copy into docker the true.go<br>Then: exec: "/true": permission denied
  2. Also tried to bash into it, then: "bash"<br>Then: executable file not found in $PATH
  3. Also tried to use the debootstrap raring raring > /dev/null <br> Then: "bash": executable file not found in $PATH

How do you do this?

Thanks

答案1

得分: 2

我不确定我完全理解。

链接项目中的Dockerfile构建了一个只包含可执行文件的镜像 - 没有shell或编译器,因此无法运行bash。它通过使用特殊的scratch基础镜像来实现,该镜像只是一个完全空的文件系统。

如果你克隆该存储库并使用Dockerfile构建镜像(docker build -t go-image .),它将直接将可执行文件复制到镜像中(注意Dockerfile复制的是可执行文件true-asm,而不是源代码true.go)。然后,如果你使用docker run启动镜像,它将运行该可执行文件(docker run go-image)。

这样说清楚了吗?代码是在本地编译的(或由另一个容器编译),然后将编译后的独立可执行文件放入镜像中。

一般来说,你不应该这样做,尤其是在刚开始学习时 - 使用包含基本工具(如shell)的golangdebian镜像会更容易。

英文:

I'm not sure I entirely follow.

The Dockerfile from the linked project builds an image with nothing in it except an executable - there will be no shell or compiler, so running bash will be impossible. It does this by using the special scratch base image, which is simply a completely empty filesystem.

If you clone the repository and build the image using the Dockerfile (docker build -t go-image .), it will simply copy the executable directly into the image (note the Dockerfile copies the executable true-asm, not the source code true.go). If you then use docker run to start the image, it will run it (docker run go-image).

Does that make sense? The code is compiled locally (or by another container) and the compiled, stand-alone executable is placed by itself into the image.

Generally, you don't want to do this and definitely not when you're beginning - it will be easier for you to use a golang or debian image which will include basic tools such as a shell.

huangapple
  • 本文由 发表于 2015年2月3日 23:25:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/28302557.html
匿名

发表评论

匿名网友

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

确定