# Shell形式的ENTRYPOINT示例

可以使用**ENTRYPOINT**指定一个纯字符串形式的命令，它会&#x5728;**/bin/sh -c**中执行。这样形式将使用**shell**处理来进行环境变量的替换，并且它会忽视任何在**CMD**或**docker run**命令行中指定的参数。为了确保**docker stop**可以给长期运行的**ENTRYPOINT**可执行程序正确发送信号，我们需要记住通过**exec**来执行程序:

```
FROM ubuntu
ENTRYPOINT exec top -b
```

当运行这个镜像时，我们将看到只存一个**PID 1**进程。

```bash
$ docker run -it --rm --name test top

Mem: 1704520K used, 352148K free, 0K shrd, 0K buff, 140368121167873K cached
CPU:   5% usr   0% sys   0% nic  94% idle   0% io   0% irq   0% sirq
Load average: 0.08 0.03 0.05 2/98 6
  PID  PPID USER     STAT   VSZ %VSZ %CPU COMMAND
    1     0 root     R     3164   0%   0% top -b
```

通过**docker stop**来退出运行的进程：

```bash
$ /usr/bin/time docker stop test

test
real    0m 0.20s
user    0m 0.02s
sys    0m 0.04s
```

如果我们忘记在**shell**形式的**ENTRYPOINT**中添加**exec**来执行程序：

```
FROM ubuntu
ENTRYPOINT top -b
CMD --ignored-param1
```

我们提供一个参数来运行这个镜像：

```bash
$ docker run -it --name test top --ignored-param2

Mem: 1704184K used, 352484K free, 0K shrd, 0K buff, 140621524238337K cached
CPU:   9% usr   2% sys   0% nic  88% idle   0% io   0% irq   0% sirq
Load average: 0.01 0.02 0.05 2/101 7
  PID  PPID USER     STAT   VSZ %VSZ %CPU COMMAND
    1     0 root     S     3168   0%   0% /bin/sh -c top -b cmd cmd2
    7     1 root     R     3164   0%   0% top -b
```

我们从输出可以看到**top**进程并不是**PID 1**。

如果我们运行**docker stop test**，容器并不会干净地退出。当退出超时以后，**stop**命令将强制发送一个**SIGKILL**信号来终止进程：

```bash
docker exec -it test ps aux

PID   USER     COMMAND
    1 root     /bin/sh -c top -b cmd cmd2
    7 root     top -b
    8 root     ps aux

$ /usr/bin/time docker stop test

test
real    0m 10.19s
user    0m 0.04s
sys    0m 0.03s
```


---

# 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/shell-xing-shi-de-entrypoint-shi-li.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.
