How to Check if the Docker Container Is Running or Not
In Docker, there are multiple ways to check our containers’ status. When this information is displayed, we can also check whether the Docker container is running.
This article will discuss commands to check whether the Docker container is running.
Check if the Docker Container Is Running or Not
In Docker, we have multiple commands to check the status of all created containers. In the following section, we will list various examples of these commands.
docker ps
In Docker, we have a command called docker ps
, which lists all containers. If you have trained for Docker, docker ps
might have been part of the basic Docker lifecycle.
The docker ps
command has multiple options; however, this section will discuss the two most important ones.
The first one is the command --all
or -a
option which shows all containers. By default, running the command with no -a
option will only show the running containers.
Example Code:
docker ps -a
docker ps
Output:
Also, we can use an additional option to only display running containers. For example, we can use the --filter
option and only find containers with statuses equal to running
.
Example Code:
docker ps -a --filter status=running
Output:
The above command is similar to the docker container ls -a
command, which lists all containers and their statuses at the container level.
Bash and docker inspect
Another approach that we can use to display running containers is programmatic. For example, we can use docker inspect
to list a container’s properties.
Since the said command has a JSON output, we can use it together with bash
.
Example Code:
if [ $(docker inspect -f '{{.State.Running}}' "zen_dirac") = "true" ]; then echo Running; else echo NotRunning; fi
The snippet of code below searches for a specific container named zen_dirac
and its property of State.Running
. If the property equates to True
, the command will display a final output of Running
, else NotRunning
.
This code snippet is helpful if we manage hundreds of running containers and only need information for a single container. In this case, our zen_dirac
container is running, so it should yield an output of Running
in our command line.
Output:
Running
docker info
Additionally, if we need a high-level summary report of our containers, we can use the docker info
command. The docker info
command displays system-wide information that includes several running containers.
This command is helpful if we do not need to input a container’s name but want to check whether containers are running.
Example Code:
docker info
Output:
Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.
LinkedIn