How to check if docker daemon is running?

Viewed 81658

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?

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.

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.

if(docker is not running)

          run docker

end

P.S. I am no linux expert and I just need to do this utility on my own environment.

10 Answers

This works for me on Ubuntu

$ systemctl status docker

enter image description here

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.

## 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
}

You can simply:

docker version > /dev/null 2>&1

The exit code of that command will be stored to $? so you can check if it's 0, then docker is running.

docker version will exit 1 if daemon is not running. If other issues are encountered, such as docker not being installed at all, the exit code will vary.

But in the end of the day, if docker is installed and daemon is running, the exit code will be 0.

The 2>&1 will redirect stderr to stdout and > /dev/null will redirect stdout to /dev/null practically silencing the output no matter what was the result of the execution.

if curl -s --unix-socket /var/run/docker.sock http/_ping 2>&1 >/dev/null
then
  echo "Running"
else
  echo "Not running"
fi

Ref: Docker api v1.28

Following @madsonic, I went for the following

#!/bin/bash
if (! docker stats --no-stream 2>/dev/null); then
  # On Mac OS this would be the terminal command to launch Docker
  open /Applications/Docker.app
  echo -n "Waiting for Docker to launch"
  sleep 1
  # Wait until Docker daemon is running and has completed initialisation
  while (! docker stats --no-stream >/dev/null 2>&1); do
    # Docker takes a few seconds to initialize
    echo -n "."
    sleep 1
  done
fi
echo
echo "Docker started"

I'm sure you want to start the docker daemon so here's the code to start it before executing your Docker run statement:

sudo systemctl start docker
Related