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?