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

前端 未结 17 1295
既然无缘
既然无缘 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:13

    List all containers:

    docker container ls -a

    ls = list
    -a = all

    Check the column "status"

    0 讨论(0)
  • 2021-01-30 05:16

    I ended up using

    docker info
    

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

    0 讨论(0)
  • 2021-01-30 05:18

    You can also check if a particular docker container is running or not using following command:

    docker inspect postgres | grep "Running"
    

    This command will check if for example my postgres container is running or not and will return output as "Running": true

    Hope this helps.

    0 讨论(0)
  • 2021-01-30 05:22

    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 *
    
    0 讨论(0)
  • 2021-01-30 05:24

    docker ps -a

    You can see all docker containers whether it is alive or dead.

    0 讨论(0)
  • 2021-01-30 05:25

    on a Mac you might see the image:

    if you right click on the docker icon then you see:

    alternatively:

    docker ps

    and

    docker run hello-world

    0 讨论(0)
提交回复
热议问题