bash 如何以编程方式检测 docker run 是否成功?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24544288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:54:12  来源:igfitidea点击:

How to detect if docker run succeeded programmatically?

bashdocker

提问by Jules Olléon

I'm writing a very simple bash script to quickly check that my container still builds and starts correctly and that the app inside responds to requests.

我正在编写一个非常简单的 bash 脚本来快速检查我的容器是否仍然正确构建和启动,以及里面的应用程序是否响应请求。

Sometimes docker runfails, e.g. because the port I'm trying to bind the container to is already allocated. But when this happens docker run's exit code is still 0 so I can't use the exit code. How can I check programmatically that the container got started correctly?

有时会docker run失败,例如因为我试图将容器绑定到的端口已经分配。但是当这种情况发生时docker run,退出代码仍然是 0,所以我不能使用退出代码。如何以编程方式检查容器是否已正确启动?

The solutions I'm considering are:

我正在考虑的解决方案是:

  • parse the output for errors
  • docker psto see if the container is running
  • 解析输出中的错误
  • docker ps查看容器是否正在运行

but these both seem a little overkill and ugly. Am I missing a better way to check whether docker runsucceeded?

但这两者似乎有点矫枉过正和丑陋。我是否错过了检查是否docker run成功的更好方法?

回答by Jules Olléon

As suggested by Abel Mui?o in comments, this may have been fixed in more recent Docker versions (I'm currently running 0.9.1).

正如 Abel Mui?o 在评论中所建议的那样,这可能已在更新的 Docker 版本中得到修复(我目前运行的是 0.9.1)。

But, if you're temporarily stuck like me with an older version, I did find a decent workaround to check if the container started by using docker inspect.

但是,如果您像我一样暂时使用旧版本,我确实找到了一个不错的解决方法来检查容器是否通过使用docker inspect.

docker inspectreturns a JSON object with a lot of info about the container, and in particular whether the container is currently running or not. The -fflag lets you easily extract the bits needed:

docker inspect返回一个 JSON 对象,其中包含有关容器的大量信息,特别是容器当前是否正在运行。该-f标志可让您轻松提取所需的位:

docker inspect -f {{.State.Running}} $CONTAINER_ID

or

或者

docker inspect -f "{{.State.Running}}" $CONTAINER_ID

will return trueor false.

将返回truefalse

Note that you probably want to sleep 1(or more) between starting the container and checking if it is up. If there's something wrong with your setup it's possible that it would appear as 'running' for a very short time before actually exiting.

请注意,您可能想要sleep 1(或更多)在启动容器和检查它是否已启动之间。如果您的设置有问题,它可能会在实际退出之前显示为“正在运行”很短的时间。

回答by pedroapero

To avoid parsing anything, you could use docker top, which returns 1 if the container is not running:

为避免解析任何内容,您可以使用docker top,如果容器未运行,则返回 1:

id=$(docker run mycontainer)
if ! docker top $id &>/dev/null
then
    echo "Container crashed unexpectedly..."
    return 1
fi

回答by simohe

We could use docker exec $id true 2>/dev/null || echo not running.

我们可以使用docker exec $id true 2>/dev/null || echo not running.

This command does not write to stdout, as "docker top" does. It writes to stderr when the container is not running, the same message as "docker top".

此命令不会像“docker top”那样写入标准输出。它在容器未运行时写入 stderr,与“docker top”消息相同。

回答by Israel

Applying the suggestions aforementioned to a script.

将上述建议应用于脚本。

1 - Create a script keepMyDockerUp.sh:

1 - 创建一个脚本keepMyDockerUp.sh

vi keepMyDockerUp.sh


#!/bin/bash
Container_ID=INSERT_YOUR_CONTAINER_ID HERE
result=$( docker inspect -f {{.State.Running}} $Container_ID)
echo "result is" $result
if [ $result = "true" ]
then
echo "docker is already running"
else
systemctl restart docker
docker start $Container_ID
fi

2 - Then simply add it to cron, so your script verifies whether your Docker container is up from time to time:

2 - 然后只需将它添加到 cron,这样您的脚本就会不时验证您的 Docker 容器是否已启动:

crontab -e

Go to the last line and add your script file. For example:

转到最后一行并添加您的脚本文件。例如:

* * * * * /root/keepMyDockerUp.sh

3 - Save crontab and never worry about your Docker container being down again.

3 - 保存 crontab,再也不用担心你的 Docker 容器会宕机。

Hope it helps...

希望能帮助到你...

;-)

;-)

回答by lvthillo

I had to use:

我不得不使用:

$ docker inspect -f {{.State.Health.Status}} xxx

(the container was in state running but the service inside the container wasn't fully started.

(容器处于运行状态,但容器内的服务未完全启动。

Part of inspect output:

检查输出的一部分:

"State": {
    "Status": "running",
    "Running": true,
    "Paused": false,
    "Restarting": false,
    "OOMKilled": false,
    "Dead": false,
    "Pid": 1618,
    "ExitCode": 0,
    "Error": "",
    "StartedAt": "2019-03-08T10:39:24.061732398Z",
    "FinishedAt": "0001-01-01T00:00:00Z",
    "Health": {
        "Status": "starting",
        "FailingStreak": 0,
        "Log": []