How to check if docker is running or not

2020-05-15 08:09发布

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

标签: bash docker
12条回答
孤傲高冷的网名
2楼-- · 2020-05-15 08:26

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.

查看更多
我想做一个坏孩纸
3楼-- · 2020-05-15 08:29

List all containers:

docker container ls -a

ls = list
-a = all

Check the column "status"

查看更多
趁早两清
4楼-- · 2020-05-15 08:29

on a Mac you might see the image:

enter image description here

if you right click on the docker icon then you see:

enter image description here

alternatively:

docker ps

and

docker run hello-world

查看更多
老娘就宠你
5楼-- · 2020-05-15 08:30

You can also check if a particular docker container is running or not using following command:

docker inspect postgres | grep "Running"

This command will check if for example my postgres container is running or not and will return output as "Running": true

Hope this helps.

查看更多
Rolldiameter
6楼-- · 2020-05-15 08:31

For OS X users (Mojave 10.14.3)

Here is what i use in my Bash script to test if Docker is running or not

# 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
查看更多
唯我独甜
7楼-- · 2020-05-15 08:34

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.

查看更多
登录 后发表回答