如何检查Docker引擎和Docker容器是否正在运行? [英] How to check if the docker engine and a docker container are running?
问题描述
在脚本中,我需要检查:
In a script, I need to check:
a)泊坞窗引擎正在运行吗?
b)给定一个容器名称,该docker容器是否正在运行?
a) Is the docker engine running?
b) Given a container name, is that docker container running?
[此问题的最初措词不明确,有些人将其解释为"check docker engine",而另一些人则将其解释为"check docker container"]
推荐答案
如果您正在寻找特定的容器,则可以运行:
If you are looking for a specific container, you can run:
if [ "$( docker container inspect -f '{{.State.Running}}' $container_name )" == "true" ]; then ...
为避免处于崩溃循环并不断重启的容器出现问题,可以通过检查 Status
字段来改善上述情况:
To avoid issues with a container that is in a crash loop and constantly restarting from showing that it's up, the above can be improved by checking the Status
field:
if [ "$( docker container inspect -f '{{.State.Status}}' $container_name )" == "running" ]; then ...
如果您想知道dockerd是否正在本地计算机上运行并且已经安装了systemd,则可以运行:
If you want to know if dockerd is running itself on the local machine and you have systemd installed, you can run:
systemctl show --property ActiveState docker
您还可以使用 docker info
或 docker version
连接到docker,如果守护程序不可用,它们将出错.
You can also connect to docker with docker info
or docker version
and they will error out if the daemon is unavailable.
这篇关于如何检查Docker引擎和Docker容器是否正在运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!