How to check if a docker instance is running?

2024/10/14 12:28:17

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?
Answer

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'] == RUNNINGreturn container_is_runningmy_container_name = "asdf"
print(is_running(my_container_name))
https://en.xdnf.cn/q/69415.html

Related Q&A

Retrieving facets and point from VTK file in python

I have a vtk file containing a 3d model,I would like to extract the point coordinates and the facets.Here is a minimal working example:import vtk import numpy from vtk.util.numpy_support import vtk_to_…

Tensorflow: feed dict error: You must feed a value for placeholder tensor

I have one bug I cannot find out the reason. Here is the code:with tf.Graph().as_default():global_step = tf.Variable(0, trainable=False)images = tf.placeholder(tf.float32, shape = [FLAGS.batch_size,33,…

Pass many pieces of data from Python to C program

I have a Python script and a C program and I need to pass large quantities of data from Python script that call many times the C program. Right now I let the user choose between passing them with an AS…

Parse JavaScript to instrument code

I need to split a JavaScript file into single instructions. For examplea = 2; foo() function bar() {b = 5;print("spam"); }has to be separated into three instructions. (assignment, function ca…

Converting all files (.jpg to .png) from a directory in Python

Im trying to convert all files from a directory from .jpg to .png. The name should remain the same, just the format would change.Ive been doing some researches and came to this:from PIL import Image im…

AssertionError: Gaps in blk ref_locs when unstack() dataframe

I am trying to unstack() data in a Pandas dataframe, but I keep getting this error, and Im not sure why. Here is my code so far with a sample of my data. My attempt to fix it was to remove all rows whe…

Python does not consider distutils.cfg

I have tried everything given and the tutorials all point in the same direction about using mingw as a compiler in python instead of visual c++.I do have visual c++ and mingw both. Problem started comi…

Is it possible to dynamically generate commands in Python Click

Im trying to generate click commands from a configuration file. Essentially, this pattern:import click@click.group() def main():passcommands = [foo, bar, baz] for c in commands:def _f():print("I a…

Different accuracy between python keras and keras in R

I build a image classification model in R by keras for R.Got about 98% accuracy, while got terrible accuracy in python.Keras version for R is 2.1.3, and 2.1.5 in pythonfollowing is the R model code:mod…

Named Entity Recognition in aspect-opinion extraction using dependency rule matching

Using Spacy, I extract aspect-opinion pairs from a text, based on the grammar rules that I defined. Rules are based on POS tags and dependency tags, which is obtained by token.pos_ and token.dep_. Belo…