Home >Operation and Maintenance >Docker >How to check whether docker is running normally
In docker, you can use the "Docker ps" command to detect whether docker is running normally. This command is used to list the containers. If the returned result has content, it means it is running. If the return result has no content, it means it is not running. The syntax is " docker ps | grep myimagename".
The operating environment of this tutorial: linux7.3 system, docker-1.13.1 version, Dell G3 computer.
How to check whether docker is running normally
docker ps: list containers
Syntax
docker ps [OPTIONS]
OPTIONS description:
#-a: Display all containers, including those that are not running.
-f : Filter the displayed content based on conditions.
--format: Specify the template file for the return value.
-l : Display recently created containers.
-n: List the recently created n containers.
--no-trunc: Do not truncate the output.
-q: Silent mode, only the container number is displayed.
-s: Display the total file size.
How to determine if they are running? I can easily use docker ps from the terminal like:
docker ps | grep myimagename
If anything is returned, the image is running. If an empty string is returned, the image is not running.
However, I don't understand how to get subprocess.Popen to handle this - it requires a parameter list so something like:
p = subprocess.Popen(['docker', 'ps', '|', 'grep', 'myimagename'], stdout=subprocess.PIPE) print p.stdout
doesn't work because it's trying to get "docker ps" and make It becomes the "docker" and "ps" commands (not supported by docker).
It seems I can't give it the full command either, because Popen tries to run the entire first argument as an executable, so this fails:
p = subprocess.Popen('docker ps | grep myimagename', stdout=subprocess.PIPE) print p.stdout
Recommended study:《 docker video tutorial》
The above is the detailed content of How to check whether docker is running normally. For more information, please follow other related articles on the PHP Chinese website!