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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…