51

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.

Mark Estrada
  • 9,013
  • 37
  • 119
  • 186

10 Answers10

59

I made a little Script (Mac Osx) to ensure Docker is running by checking the exit code of 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..
madsonic
  • 763
  • 1
  • 6
  • 9
29

This works for me on Ubuntu

$ systemctl status docker

enter image description here

morpheus
  • 18,676
  • 24
  • 96
  • 159
21
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

Gongora
  • 595
  • 6
  • 10
19

You have a utility called pgrep on almost all the Linux systems.

You can just do:

pgrep -f docker > /dev/null || echo "starting docker"

Replace the echo command with your docker starting command.

Radagast the Brown
  • 3,156
  • 3
  • 27
  • 40
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 7
    I am aware that the question does not ask for this but on mac pgrep always find a process, as `com.docker.vmne` persists when docker is not running – Scott Anderson Feb 03 '20 at 00:23
  • 6
    To @ScottAnderson's point, if 'Docker Desktop' on MacOS is NOT running, `pgrep -f docker` will return a single value with a low (e.g. 3 digit) pid. When 'Docker Desktop' on MacOS is running, the same command will return a list of higher (5 digit) pid. So for the MacOS-specific use case, I think `pgrep -f Docker.app` is what you want. – bcdady May 06 '21 at 00:10
7

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.

Jani Hyytiäinen
  • 5,293
  • 36
  • 45
  • 2
    nice! Short and sweet. I like this for fail-fast sanity checking in scripts: if ! docker version > /dev/null 2>&1; then echo "Docker isn't running."; exit 1; fi – jgreve Mar 26 '22 at 13:23
6

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
}
Vincent
  • 5,063
  • 3
  • 28
  • 39
  • OMG, I found your comment on this gist too! I really like this solution! https://gist.github.com/peterver/ca2d60abc015d334e1054302265b27d9#gistcomment-2642238 – Andrew Jun 22 '21 at 04:13
5

You could also just check for the existence of /var/run/docker.pid.

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18
4

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"
giuliot
  • 156
  • 13
-1

A function could looks so:

isRunning {
    `ps -ef | grep "[d]ocker" | awk {'print $2'}`
}

I created a script to start, stop, restart a mongodb-server. You only need to change some path inside the scripts, and i also works for you: Script

-2

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
RobC
  • 22,977
  • 20
  • 73
  • 80
Ochieng'
  • 129
  • 1
  • 4