0

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.

adrtam
  • 6,991
  • 2
  • 12
  • 27
Arefe
  • 11,321
  • 18
  • 114
  • 168

2 Answers2

1

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.

Konrad
  • 952
  • 10
  • 25
  • I needed to run it from the Python code (not terminal). I provided a solution which only work in Mac OS (atm). For the record, the `systemctl` is only works in linux/ – Arefe Mar 27 '19 at 15:35
  • 1
    Yes but you can run terminal command in python also. Read the ending of the answer carefully. – Konrad Mar 27 '19 at 15:44
  • I didn't see the last line... sorry. But you could have provided a complete answer, though using the Python code. – Arefe Mar 27 '19 at 15:46
0

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)
Arefe
  • 11,321
  • 18
  • 114
  • 168