bash 如何检查docker是否正在运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43721513/
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 is running or not
提问by user5517392
I am new to docker. I am writing a simple script for docker. I need to check whether docker is running or not. Is there a command to check with container name
我是码头工人的新手。我正在为 docker 编写一个简单的脚本。我需要检查docker是否正在运行。是否有命令检查容器名称
回答by BMitch
If you are looking for a specific container, you can run:
如果您正在寻找特定的容器,您可以运行:
docker inspect -f '{{.State.Running}}' $container_name
If you want to know if dockerd is running itself on the local machine and you have systemd installed, you can run:
如果你想知道 dockerd 是否在本地机器上运行并且你已经安装了 systemd,你可以运行:
systemctl show --property ActiveState docker
You can also connect to docker with docker info
or docker version
and they will error out if the daemon is unavailable.
您还可以使用docker info
或连接到 docker,docker version
如果守护程序不可用,它们会出错。
回答by randomcontrol
I ended up using
我最终使用
docker info
to check with a bash script if docker engine is running.
使用 bash 脚本检查 docker 引擎是否正在运行。
回答by Hernan Garcia
you can check docker state using: systemctl is-active docker
您可以使用以下方法检查 docker 状态: systemctl is-active docker
? ~ systemctl is-active docker
active
you can use it as:
您可以将其用作:
? ~ if [ "$(systemctl is-active docker)" = "active" ]; then echo "is alive :)" ; fi
is alive :)
? ~ sudo systemctl stop docker
? ~ if [ "$(systemctl is-active docker)" = "active" ]; then echo "is alive :)" ; fi
* empty response *
回答by Senio Caires
List all containers:
列出所有容器:
docker container ls -a
docker container ls -a
ls
= list-a
= all
ls
= 列表-a
= 全部
Check the column "status"
检查列“状态”
回答by Lapin
For OS X users (Mojave 10.14.3)
对于 OS X 用户(Mojave 10.14.3)
Here is what i use in my Bash script to test if Docker is running or not
这是我在 Bash 脚本中用来测试 Docker 是否正在运行的内容
# Check if docker is running
docker_state=$(docker info >/dev/null 2>&1)
if [[ $? -ne 0 ]]; then
echo "Docker does not seem to be running, run it first and retry"
exit 1
fi
回答by Emilia Tyl
Sometimes you don't know the full container name, in this case this is what worked for me:
有时您不知道完整的容器名称,在这种情况下,这对我有用:
if [ $(docker ps | grep keyword | wc -l) -gt 0 ]
then
echo "Running!"
else
echo "Not running!"
exit 1
fi
We list all the running container processes (docker ps -a would show us also not running ones, but that's not what I needed), we search for a specific word (grep part) and simply count the lines of the result (wc -l), if it's greater than 0 it means we found some running containers which names contain our keyword.
我们列出所有正在运行的容器进程(docker ps -a 会显示我们也没有运行,但这不是我需要的),我们搜索一个特定的词(grep 部分)并简单地计算结果的行数(wc -l ),如果它大于 0,则表示我们找到了一些名称包含我们关键字的正在运行的容器。
回答by Glen Pierce
Any docker command (except docker -v
), like docker ps
If Docker is running, you'll get some valid response, otherwise you'll get a message that includes "Is your docker daemon up and running?"
任何 docker 命令(除了docker -v
),例如docker ps
如果 Docker 正在运行,您将得到一些有效的响应,否则您将收到一条消息,其中包括“您的 docker 守护进程是否已启动并正在运行?”
You can also check your task manager.
您还可以检查您的任务管理器。
回答by Naanii
You can check with this command systemctl status docker
it will show the status of the docker. If you want to start you can use systemctl start docker
instead of systemctl
you can try also with service
, service docker status
and service docker start
respectively.
您可以使用此命令检查systemctl status docker
它会显示 docker 的状态。如果你想开始,你可以使用systemctl start docker
而不是systemctl
你也可以分别尝试使用service
,service docker status
和service docker start
。
回答by Marco Lackovic
Run:
跑:
docker version
If docker is runningyou will see:
如果 docker正在运行,您将看到:
Client: Docker Engine - Community
Version: ...
[omitted]
Server: Docker Engine - Community
Engine:
Version: ...
[omitted]
If docker is not runningyou will see:
如果 docker未运行,您将看到:
Client: Docker Engine - Community
Version: ...
[omitted]
Error response from daemon: Bad response from Docker engine
回答by mrgnw
If the underlying goal is "How can I start a container when Docker starts?"
如果基本目标是“Docker 启动时如何启动容器?”
We can use Docker's restart policy
我们可以使用 Docker 的重启策略
To add a restart policy to an existing container:
向现有容器添加重启策略:
Docker: Add a restart policy to a container that was already created
Example:
例子:
docker update --restart=always <container>