How to check if a docker instance is running?

11,860

Solution 1

One option is to use subprocess.check_output setting shell=True (thanks slezica!):

s = subprocess.check_output('docker ps', shell=True)
print 'Results of docker ps' + s

if the docker ps command fails (for example you don't start your docker-machine) then check_output will throw an exception.

A simple find can then verify your container is found / not-found:

if s.find('containername') != -1:
    print 'found!'
else:
    print 'not found.'

I would recommend using the container hash id and not container name in this case, too, as the name may be duplicated in the image name or other results of the docker ps.

Solution 2

You can use the python docker client:

import docker
DOCKER_CLIENT = docker.DockerClient(base_url='unix://var/run/docker.sock')
RUNNING = 'running'

def is_running(container_name):
    """
    verify the status of a sniffer container by it's name
    :param container_name: the name of the container
    :return: Boolean if the status is ok
    """
    container = DOCKER_CLIENT.containers.get(container_name)

    container_state = container.attrs['State']

    container_is_running = container_state['Status'] == RUNNING

    return container_is_running

my_container_name = "asdf"
print(is_running(my_container_name))
Share:
11,860
enderland
Author by

enderland

Farewell Stack Exchange. o7 to everyone I've shared labors with over these many years. Take care.

Updated on June 30, 2022

Comments

  • enderland
    enderland almost 2 years

    I am using Python to start docker instances.

    How can I identify if they are running? I can pretty easily use docker ps from terminal like:

    docker ps | grep myimagename
    

    and if this returns anything, the image is running. If it returns an empty string, the image is not running.

    However, I cannot understand how to get subprocess.Popen to work with this - it requires a list of arguments so something like:

        p = subprocess.Popen(['docker', 'ps', '|', 'grep', 'myimagename'], stdout=subprocess.PIPE)
        print p.stdout
    

    does not work because it tries to take the "docker ps" and make it "docker" and "ps" commands (which docker doesn't support).

    It doesn't seem I can give it the full command, either, as Popen tries to run the entire first argument as the executable, so this fails:

        p = subprocess.Popen('docker ps | grep myimagename', stdout=subprocess.PIPE)
        print p.stdout
    

    Is there a way to actually run docker ps from Python? I don't know if trying to use subprocess is the best route or not. It is what I am using to run the docker containers, however, so it seemed to be the right path.

    • How can I determine if a docker instance is running from a Python script?
  • Ashish Bainade
    Ashish Bainade almost 6 years
    I am getting bewlo error on executing s = subprocess.check_output('docker ps', shell=True) subprocess.CalledProcessError: Command 'docker ps' returned non-zero exit status 1