How to check if the docker engine and a docker container are running?
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 bashif ! docker info > /dev/null 2>&1; then echo "This script uses docker, and it isn't running - please start docker and try again!" exit 1fi
you can check docker state using: systemctl is-active docker
➜ ~ systemctl is-active dockeractive
you can use it as:
➜ ~ if [ "$(systemctl is-active docker)" = "active" ]; then echo "is alive :)" ; fiis alive :)➜ ~ sudo systemctl stop docker➜ ~ if [ "$(systemctl is-active docker)" = "active" ]; then echo "is alive :)" ; fi * empty response *