Dockerfile 中的 CMD 和 ENTRYPOINT 有什么区别? [英] What is the difference between CMD and ENTRYPOINT in a Dockerfile?

查看:37
本文介绍了Dockerfile 中的 CMD 和 ENTRYPOINT 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Dockerfiles 中有两个命令看起来和我很相似:CMDENTRYPOINT.但我想它们之间存在(微妙的?)区别 - 否则对于同一件事有两个命令是没有任何意义的.

In Dockerfiles there are two commands that look similar to me: CMD and ENTRYPOINT. But I guess that there is a (subtle?) difference between them - otherwise it would not make any sense to have two commands for the very same thing.

CMD

CMD 的主要目的是为正在执行的容器提供默认值.

The main purpose of a CMD is to provide defaults for an executing container.

对于ENTRYPOINT:

ENTRYPOINT 可帮助您配置可作为可执行文件运行的容器.

An ENTRYPOINT helps you to configure a container that you can run as an executable.

那么,这两个命令有什么区别?

So, what's the difference between those two commands?

推荐答案

Docker 有一个默认入口点,它是 /bin/sh -c 但没有默认命令.

Docker has a default entrypoint which is /bin/sh -c but does not have a default command.

当你像这样运行 docker 时:docker run -i -t ubuntu bash入口点是默认的/bin/sh -c,图像是ubuntu,命令是bash.

When you run docker like this: docker run -i -t ubuntu bash the entrypoint is the default /bin/sh -c, the image is ubuntu and the command is bash.

命令通过入口点运行.即,实际执行的是 /bin/sh -c bash.这使得 Docker 能够依靠 shell 的解析器快速实现 RUN.

The command is run via the entrypoint. i.e., the actual thing that gets executed is /bin/sh -c bash. This allowed Docker to implement RUN quickly by relying on the shell's parser.

后来,人们要求能够对此进行自定义,因此引入了 ENTRYPOINT--entrypoint.

Later on, people asked to be able to customize this, so ENTRYPOINT and --entrypoint were introduced.

上例中 ubuntu 之后的所有内容都是命令并被传递到入口点.当使用 CMD 指令时,就像你在执行 docker run -i -t ubuntu 一样. 将是入口点的参数.

Everything after ubuntu in the example above is the command and is passed to the entrypoint. When using the CMD instruction, it is exactly as if you were doing docker run -i -t ubuntu <cmd>. <cmd> will be the parameter of the entrypoint.

如果您改为键入此命令 docker run -i -t ubuntu,您也会得到相同的结果.由于 ubuntu Dockerfile 指定了一个默认 CMD:CMD ["bash"]

You will also get the same result if you instead type this command docker run -i -t ubuntu. You will still start a bash shell in the container because of the ubuntu Dockerfile specified a default CMD: CMD ["bash"]

当一切都传递到入口点时,您可以从图像中获得非常好的行为.@Jiri 示例很好,它展示了如何将图像用作二进制".当使用 ["/bin/cat"] 作为入口点然后执行 docker run img/etc/passwd 时,你得到它,/etc/passwdcode> 是命令并传递到入口点,因此最终结果执行只是/bin/cat/etc/passwd.

As everything is passed to the entrypoint, you can have a very nice behavior from your images. @Jiri example is good, it shows how to use an image as a "binary". When using ["/bin/cat"] as entrypoint and then doing docker run img /etc/passwd, you get it, /etc/passwd is the command and is passed to the entrypoint so the end result execution is simply /bin/cat /etc/passwd.

另一个例子是将任何 cli 作为入口点.例如,如果你有一个 redis 映像,而不是运行 docker run redisimg redis -H something -u toto get key,你可以简单地使用 ENTRYPOINT ["redis", "-H", "something", "-u", "toto"] 然后像这样运行以获得相同的结果:docker run redisimg get key.

Another example would be to have any cli as entrypoint. For instance, if you have a redis image, instead of running docker run redisimg redis -H something -u toto get key, you can simply have ENTRYPOINT ["redis", "-H", "something", "-u", "toto"] and then run like this for the same result: docker run redisimg get key.

这篇关于Dockerfile 中的 CMD 和 ENTRYPOINT 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆