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

前端 未结 17 1314
既然无缘
既然无缘 2021-01-30 05:03

In a script, I need to check:

a) Is the docker engine running?
b) Given a container name, is that docker container running?

[the initial wording of this

17条回答
  •  心在旅途
    2021-01-30 05:35

    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.

提交回复
热议问题