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

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

    container status: true/false

    # docker inspect --format '{{json .State.Running}}' container-name
    true
    #
    
    0 讨论(0)
  • 2021-01-30 05:28

    For OS X users (Mojave 10.14.3)

    Here is what i use in my Bash script to test if Docker is running or not

    # Check if docker is running
    if ! docker info >/dev/null 2>&1; then
        echo "Docker does not seem to be running, run it first and retry"
        exit 1
    fi
    
    0 讨论(0)
  • 2021-01-30 05:28

    Run this command in the terminal:

    docker ps
    

    If docker is not running, you wil get this message:

    Error response from daemon: dial unix docker.raw.sock: connect: connection refused

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

    For the answer to your first question refer to this answer - https://stackoverflow.com/a/65447848/4691279

    For your second question - you can use command like docker ps --filter "name=<<<YOUR_CONTAINER_NAME>>>" to check whether a particular container is running or not.

    • If Docker and Container both are running then you will get output like below:

      $ docker ps --filter "name=nostalgic_stallman"
      
      CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES       
      9b6247364a03        busybox             "top"               2 minutes ago       Up 2 minutes                            nostalgic_stallman
      
    • If Docker is not running then you will get an error message saying docker daemon is not running.

    • If Docker running but Container is not running then you will not get the container name in the output of this command.

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

    Any docker command (except docker -v), like docker ps If Docker is running, you'll get some valid response, otherwise you'll get a message that includes "Is your docker daemon up and running?"

    You can also check your task manager.

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

    Run:

    docker version
    

    If docker is running you will see:

    Client: Docker Engine - Community
     Version:           ...
     [omitted]
    
    Server: Docker Engine - Community
     Engine:
      Version:          ...
     [omitted]
    

    If docker is not running you will see:

    Client: Docker Engine - Community
     Version:           ...
     [omitted]
    
    Error response from daemon: Bad response from Docker engine
    
    0 讨论(0)
提交回复
热议问题