Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if the docker engine and a docker container are running?

Tags:

bash

docker

People also ask

How do I test a docker engine?

To see the highest version of the API your Docker daemon and client support, use docker version : $ docker version Client: Docker Engine - Community Version: 20.10. 0 API version: 1.41 Go version: go1.

How do I show docker running containers?

In order to list the Docker containers, we can use the “docker ps” or “docker container ls” command. This command provides a variety of ways to list and filter all containers on a particular Docker engine.

Is Docker Engine same as docker?

Docker Engine is the core product of Docker, including its daemon (dockerd) as well as its CLI (docker). Docker Daemon is simply a part of Docker Engine. Quoting the Docker engine overview page: Docker Engine is an open source containerization technology for building and containerizing your applications.


If you are looking for a specific container, you can run:

if [ "$( docker container inspect -f '{{.State.Running}}' $container_name )" == "true" ]; then ...

To avoid issues with a container that is in a crash loop and constantly restarting from showing that it's up, the above can be improved by checking the Status field:

if [ "$( docker container inspect -f '{{.State.Status}}' $container_name )" == "running" ]; then ...

If you want to know if dockerd is running itself on the local machine and you have systemd installed, you can run:

systemctl show --property ActiveState docker

You can also connect to docker with docker info or docker version and they will error out if the daemon is unavailable.


I ended up using

docker info

to check with a bash script if docker engine is running.

EDIT: which can be used to fail your script if docker isn't running, like so:

#!/usr/bin/env bash
if ! docker info > /dev/null 2>&1; then
  echo "This script uses docker, and it isn't running - please start docker and try again!"
  exit 1
fi

List all containers:

docker container ls -a

ls = list
-a = all

Check the column "status"


you can check docker state using: systemctl is-active docker

➜  ~  systemctl is-active docker
active

you can use it as:

➜  ~  if [ "$(systemctl is-active docker)" = "active" ]; then echo "is alive :)" ; fi
is alive :)

➜  ~  sudo systemctl stop docker

➜  ~  if [ "$(systemctl is-active docker)" = "active" ]; then echo "is alive :)" ; fi
 * empty response *

For OS X users (Mojave 10.14.3)

Here is what i use in my Bash script to test if Docker is running or not

# Check if docker is running
if ! docker info >/dev/null 2>&1; then
    echo "Docker does not seem to be running, run it first and retry"
    exit 1
fi