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

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

问题描述

在Dockerfiles中有两个类似于我的命令: CMD ENTRYPOINT 。但是我猜这两者之间有一个微妙的区别 - 否则,对于同一件事情来说,有两个命令是没有意义的。

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有一个默认的entrypoint,它是 / 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
entrypoint是默认的 / 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.

该命令通过entrypoint运行。即实际执行的是 / bin / sh -c bash 。这允许docker通过依赖shell的解析器快速实现 RUN
以后,人们要求能够自定义,所以 ENTRYPOINT -entrypoint 已经被引入。

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. Later on, people asked to be able to customize this so ENTRYPOINT and -entrypoint has been introduced.

上述示例中的 ubuntu 之后的所有内容都是命令,并传递给entrypoint。当使用 CMD 指令时,就像您在执行 docker run -i -t ubuntu< cmd> < cmd> 将成为entrypoint的参数。

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,您仍然会在容器中启动一个bash shell: 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"]

随着一切都传递到entrypoint,你可以有一个非常好的行为从你的图像。 @Jiri示例很好,它显示了如何使用图像作为二进制。当使用 [/ bin / cat] 作为入口点,然后执行 docker运行img / etc / passwd 得到它, / etc / passwd 是命令,并传递给entrypoint,所以最终的结果执行只是 / 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作为entrypoint。例如,如果你有一个redis图像,而不是运行 docker运行redisimg redis -H something -u toto获取密钥,你可以简单地具有 ENTRYPOINT [redis,-H,something,-u,toto] 然后像这样运行相同的结果: docker run redisimg get键

An other 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.

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

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