dockerfile 基础


Author: yifei / Created: June 27, 2017, 11:54 a.m. / Modified: June 27, 2017, 12:33 p.m. / Edit

Dockerfile 列出了构建一个docker image 的可复现步骤。比起一步一步通过 docker commit 来制作一个镜像,dockerfile更适用于CI自动测试等系统。

Dockerfile 命令

几个比较容易混淆的

COPY vs ADD

ADD 会自动解压压缩包,在不需要特殊操作的时候,最好使用COPY。

ENTRYPOINT vs CMD

entrypoint 指定了 Docker 镜像要运行的二进制文件(当然也包括参数),而 cmd 则指定了运行这个二进制文件的参数。不过因为默认 entrypoint 是 bash -c,所以实际上 CMD 指定的也是要运行的命令。

另外,docker run 时候包含命令行参数,会执行命令行参数,而不是 CMD 的内容。如果使用 /bin/bash 作为命令行的指令,这样便替换掉 CMD 的内容,从而进入镜像中查看编译出的镜像究竟是什么样的。

个人倾向于只使用 CMD,而不使用 ENTRYPOINT

参考:https://stackoverflow.com/a/34245657/1061155

使用Dockerfile 还是 commit 来构建镜像

如果可能的话,尽量使用 dockerfile

I've been wondering the same thing, and my impression (which could be totally wrong) it that it's really the same case as with VMs --> you don't want to not know how to recreate the vm image. In my case I have regular .sh scripts to install, and am wondering why I can't just maintain these, run docker and effectively call these, and create the golden version image that way. My scripts work to get it installed on a local PC, and the reason I want to use docker is to deal with conflicts of multiple instances of programs/have clean file system/etc https://stackoverflow.com/questions/26110828/should-i-use-dockerfiles-or-image-commits


有任何问题可以发邮件到 kongyifei (at) gmail.com 讨论