ENTRYPOINT 指定了在容器启动时始终执行的命令。
CMD 指定将提供给ENTRYPOINT 的参数。
如果你想制作一个专用于特定命令的图像,你将使用ENTRYPOINT ["/path/dedicated_command"]
否则,如果您想为通用目的制作图像,则可以不指定 ENTRYPOINT 并使用 CMD ["/path/dedicated_command"],因为您可以通过向 docker run 提供参数来覆盖该设置。
例如,如果您的 Dockerfile 是:
FROM debian:wheezy
ENTRYPOINT ["/bin/ping"]
CMD ["localhost"]
不带任何参数运行图像将ping localhost:
$ docker run -it test
PING localhost (127.0.0.1): 48 data bytes
56 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.096 ms
56 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.088 ms
56 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.088 ms
^C--- localhost ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.088/0.091/0.096/0.000 ms
现在,使用参数运行图像将 ping 参数:
$ docker run -it test google.com
PING google.com (173.194.45.70): 48 data bytes
56 bytes from 173.194.45.70: icmp_seq=0 ttl=55 time=32.583 ms
56 bytes from 173.194.45.70: icmp_seq=2 ttl=55 time=30.327 ms
56 bytes from 173.194.45.70: icmp_seq=4 ttl=55 time=46.379 ms
^C--- google.com ping statistics ---
5 packets transmitted, 3 packets received, 40% packet loss
round-trip min/avg/max/stddev = 30.327/36.430/46.379/7.095 ms
为了比较,如果你的 Dockerfile 是:
FROM debian:wheezy
CMD ["/bin/ping", "localhost"]
在没有任何参数的情况下运行图像将 ping 本地主机:
$ docker run -it test
PING localhost (127.0.0.1): 48 data bytes
56 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.076 ms
56 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.087 ms
56 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.090 ms
^C--- localhost ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.076/0.084/0.090/0.000 ms
但是运行带有参数的图像会运行参数:
docker run -it test bash
root@e8bb7249b843:/#
有关更多详细信息,请参阅 Brian DeHamer 的这篇文章:
https://www.ctl.io/developers/blog/post/dockerfile-entrypoint-vs-cmd/