Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if docker daemon is running?

Tags:

bash

shell

docker

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.

like image 844
Mark Estrada Avatar asked May 15 '17 11:05

Mark Estrada


People also ask

Is the Docker daemon running Docker?

Another way to check for a running Docker daemon is by inspecting its process ID file. The daemon writes its process ID to /var/run/docker. pid each time it starts up. When this file exists, Docker should be running and ready for CLI connections.

How do I make sure Docker daemon is running in Windows?

To start Docker in daemon mode, choose Application > Start "Docker Daemon". The state should transition to "Running" after a few seconds and Docker Daemon should be accessible over the remote bridge. That's it! Next time your computer boots, Docker Daemon will start up immediately, before anyone logs on.

How do I run a Docker container in daemon mode?

The -d flag is used with docker run command to run a container in detached mode. What you're looking for might be docker-machine start : docker-machine start [arg...]


2 Answers

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.. 
like image 189
madsonic Avatar answered Sep 23 '22 07:09

madsonic


This works for me on Ubuntu

$ systemctl status docker 

enter image description here

like image 37
morpheus Avatar answered Sep 21 '22 07:09

morpheus