How to check if docker daemon is running?

后端 未结 7 777
庸人自扰
庸人自扰 2020-12-15 04:31

I am trying to create a bash utility script to check if a docker daemon is running in my server. Is there a better way of checking if the docker daemon is running in my serv

相关标签:
7条回答
  • 2020-12-15 04:33

    I'm sure you want to start the docker daemon so here's the code to start it before executing your Docker run statement:

    sudo systemctl start docker
    
    0 讨论(0)
  • 2020-12-15 04:38

    This works for me on Ubuntu

    $ systemctl status docker
    

    0 讨论(0)
  • 2020-12-15 04:39

    I made a little Script (Mac Osx) to ensure Docker is running by checking the exit code of docker stats.

    #!/bin/bash
    #Open Docker, only if is not running
    if (! docker stats --no-stream ); then
      # On Mac OS this would be the terminal command to launch Docker
      open /Applications/Docker.app
     #Wait until Docker daemon is running and has completed initialisation
    while (! docker stats --no-stream ); do
      # Docker takes a few seconds to initialize
      echo "Waiting for Docker to launch..."
      sleep 1
    done
    fi
    
    #Start the Container..
    
    0 讨论(0)
  • 2020-12-15 04:42

    You have a utility called pgrep on almost all the Linux systems.

    You can just do:

    pgrep -f docker > /dev/null || echo "starting docker"
    

    Replace the echo command with your docker starting command.

    0 讨论(0)
  • 2020-12-15 04:45

    You could also just check for the existence of /var/run/docker.pid.

    0 讨论(0)
  • 2020-12-15 04:55

    The following works on macOS and on Windows if git bash is installed. On macOS open /Applications/Docker.app would start the docker deamon. Haven't seen anything similar for Windows however.

    ## check docker is running at all
    ## based on https://stackoverflow.com/questions/22009364/is-there-a-try-catch-command-in-bash
    {
      ## will throw an error if the docker daemon is not running and jump
      ## to the next code chunk     
      docker ps -q
    } || {
      echo "Docker is not running. Please start docker on your computer"
      echo "When docker has finished starting up press [ENTER} to continue"
      read
    }
    
    0 讨论(0)
提交回复
热议问题