I need execute from the Python code to see if the docker` daemon is running OS independently.
Is it possible to achieve? Otherwise, it will be also okay to read the OS and execute for each platform individually.
If it was some linux system i would try to launch systemctl status docker
to check of if service is running.
To make this platform independent you can make call to some docker function which needs docker daemon running like docker ps
. It should return table of running processes when daemon is running otherwise it will show message:
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
To launch this commands use Popen from subprocess library. About running commands and retrieving output you can read here.
I have a partial solution where I check if the docker daemon is running using a bash script and if not, the python code will make it run.
docker.sh
#!/usr/bin/env bash
#!/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
echo "docker daemon is open for the local machine"
done
fi
#Start the Container.
Now check from the Python code and OS X if the daemon is running,
# check if the docker daemon is running in os x and if not run it
if(os.name == "posix"):
subprocess.call("bin/docker.sh", shell=True)