bash 如何检查docker守护进程是否正在运行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43978837/
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
How to check if docker daemon is running?
提问by Mark Estrada
I am trying to create a bash utility script to check if a docker daemon is running in my server. Is there a better way of checking if the docker daemon is running in my server other than running a code like this?
我正在尝试创建一个 bash 实用程序脚本来检查我的服务器中是否正在运行 docker 守护进程。除了运行这样的代码之外,是否有更好的方法来检查 docker 守护进程是否在我的服务器中运行?
ps -ef | grep docker
root 1250 1 0 13:28 ? 00:00:04 /usr/bin/dockerd --selinux-enabled
root 1598 1250 0 13:28 ? 00:00:00 docker-containerd -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --shim docker-containerd-shim --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --runtime docker-runc
root 10997 10916 0 19:47 pts/0 00:00:00 grep --color=auto docker
I would like to create a bash shell script that will check if my docker daemon is running. If it is running then do nothing but if it is not then have the docker daemon started.
我想创建一个 bash shell 脚本来检查我的 docker 守护进程是否正在运行。如果它正在运行,那么什么都不做,但如果它没有运行,则启动 docker 守护进程。
My pseudocode is something like this. I am thinking of parsing the output of my ps -ef but I just would like to know if there is a more efficient way of doing my pseudocode.
我的伪代码是这样的。我正在考虑解析 ps -ef 的输出,但我只想知道是否有更有效的方法来执行我的伪代码。
if(docker is not running)
run docker
end
如果(泊坞窗没有运行)
run docker
结尾
P.S. I am no linux expert and I just need to do this utility on my own environment.
PS 我不是 linux 专家,我只需要在我自己的环境中执行此实用程序。
采纳答案by anubhava
You have a utility called pgrep
on almost all the Linux systems.
pgrep
几乎所有 Linux 系统上都有一个实用程序。
You can just do:
你可以这样做:
pgrep -f docker > /dev/null || echo "starting docker"
Replace the echo
command with your docker starting command.
将echo
命令替换为您的 docker 启动命令。
回答by madsonic
I made a little Script (Mac Osx) to ensure Docker is running by checking the exit code of docker stats
.
我制作了一个小脚本(Mac Osx),通过检查docker stats
.
#!/bin/bash
#Open Docker, only if is not running
if (! docker stats --no-stream ); then
# On Mac OS this would be the terminal command to launch Docker
open /Applications/Docker.app
#Wait until Docker daemon is running and has completed initialisation
while (! docker stats --no-stream ); do
# Docker takes a few seconds to initialize
echo "Waiting for Docker to launch..."
sleep 1
done
fi
#Start the Container..
回答by Raman Sailopal
You could also just check for the existence of /var/run/docker.pid
.
您也可以只检查/var/run/docker.pid
.
回答by Vincent
The following works on macOS and on Windows if git bash is installed. On macOS open /Applications/Docker.app
would start the docker deamon. Haven't seen anything similar for Windows however.
如果安装了 git bash,以下内容适用于 macOS 和 Windows。在 macOS 上open /Applications/Docker.app
会启动 docker 守护进程。然而,在 Windows 上还没有看到类似的东西。
## check docker is running at all
## based on https://stackoverflow.com/questions/22009364/is-there-a-try-catch-command-in-bash
{
## will throw an error if the docker daemon is not running and jump
## to the next code chunk
docker ps -q
} || {
echo "Docker is not running. Please start docker on your computer"
echo "When docker has finished starting up press [ENTER} to continue"
read
}
回答by Ochieng'
I'm sure you want to start the docker daemon so here's the code to start it before executing your Docker run statement:
我确定您想启动 docker 守护进程,所以这里是在执行 Docker run 语句之前启动它的代码:
sudo systemctl start docker