How to check if docker daemon is running?

I am trying to create a bash script utility to check if docker daemon is running on my server. Is there a better way to check if the docker daemon is working on my server besides running such code?

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 script shell that will check if my docker daemon is working. If it works, do nothing, but if it is not, the docker daemon will start.

My pseudo code is something like this. I am thinking about parsing the output of my ps -ef, but I just wanted to know if there is a more efficient way to make my pseudo-code.

if (docker is not running)

  run docker 

end

PS I'm not a Linux specialist, and I just need to make this utility in my own environment.

+19
bash shell docker
source share
6 answers

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

You can simply do:

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

Replace the echo command with your docker launch command.

+9
source share

I did a bit of Script (Mac Osx) to make sure Docker was running by checking the docker stats exit code.

 #!/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.. 
+7
source share

You can also just check for the presence of / var / run / docker.pid

+3
source share

The following works on macOS and Windows if git bash is installed. On macOS open /Applications/Docker.app , the deamon docker is launched. However, Windows did not see anything like it.

 ## 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 } 
+1
source share

A function might look like this:

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

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

0
source share

I'm sure you want to start the docker daemon, so give the code to run it before executing the Docker launch statement:

 sudo systemctl start docker 
0
source share

All Articles