# 理解CMD和ENTRYPOINT是如何交互的

**CMD**和**ENTRYPOINT指令**定义了容器运行时应该执行什么命令。有一些规则描述了它们彼此之间的协作。

1. **Dockerfile**中至少应该指定一个**CMD**或者**ENTRYPOINT**命令
2. 当将容器作为可执行命令运行时，需要一个**ENTRYPOINT**
3. **CMD**应该作为定义**ENTRYPOINT**默认参数的一种方式或者在容器中作为一个**ad-hoc**命令。
4. 在运行带有替代参数的容器时，**CMD**将被覆盖。

下面的表列出了针对不同的**ENTRYPOINT/CMD组合**所执行的命令：

|                                   | **No ENTRYPOINT**                | **ENTRYPOINT exec\_entry p1\_entry** | **ENTRYPOINT \[“exec\_entry”, “p1\_entry”]**           |
| --------------------------------- | -------------------------------- | ------------------------------------ | ------------------------------------------------------ |
| **No CMD**                        | **error, not allowed**           | **/bin/sh -c exec\_entry p1\_entry** | **exec\_entry p1\_entry**                              |
| **CMD \[“exec\_cmd”, “p1\_cmd”]** | **exec\_cmd p1\_cmd**            | **/bin/sh -c exec\_entry p1\_entry** | **exec\_entry p1\_entry exec\_cmd p1\_cmd**            |
| **CMD \[“p1\_cmd”, “p2\_cmd”]**   | **p1\_cmd p2\_cmd**              | **/bin/sh -c exec\_entry p1\_entry** | **exec\_entry p1\_entry p1\_cmd p2\_cmd**              |
| **CMD exec\_cmd p1\_cmd**         | **/bin/sh -c exec\_cmd p1\_cmd** | **/bin/sh -c exec\_entry p1\_entry** | **exec\_entry p1\_entry /bin/sh -c exec\_cmd p1\_cmd** |

**NOTE:**

如果**CMD**是在基础镜像中定义的，设置**ENTRYPOINT**将重置**CMD**为空值。在这种情形下，**CMD**在当前的镜像中必须拥有一个值。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://camelgemonion.gitbook.io/docker/dockerfile-zhi-ling/entrypoint/li-jie-cmd-he-entrypoint-shi-ru-he-jiao-hu-de.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
