Bub Espinja
Bub Espinja

Reputation: 4571

How to check if Docker is running on Windows?

I'd like to know how to check if Docker is running on Windows by means of the command line (cmd or powershell).

Although, I've found several posts indicating the solution to this, they are for Linux environments:

How to check if docker is running or not

How to check if docker daemon is running?

I couldn't get the answer for Windows systems.

Upvotes: 41

Views: 118142

Answers (8)

Matt
Matt

Reputation: 27005

In a Windows batch script (.cmd or .bat file extension), you can check this way if docker is running or not (for Linux or Powershell, check out KyleMit's answer):

docker ps 2>&1 | find /I /C "error" > NUL
if %errorlevel% EQU 0 (
    ECHO IMPORTANT: Run DOCKER before you start this script!
    TITLE DOCKER NOT RUNNING
    timeout 30
)

It redirects the error stream to normal output and then uses the pipe | to find the "error" in the message, then checking via the errorlevel if it was found or not.

If an error is found, it means docker is not running and the script then displays the message "Run DOCKER before you start this script!".

I have seen answers suggesting to use docker --version: this will always be successful if you have installed docker, but you can't check this way if it is running or not. But it might be useful to check whether you have docker on your machine or not (using a pattern like the one above).

Upvotes: -1

Yeo
Yeo

Reputation: 11794

If you need a exact string only, you can use the go-lang formatting. Example: using docker info

 C:> docker info -f "{{.OSType}}"
 windows

 C:> docker info -f "{{.Architecture}}"
 x86_64

Example: using docker version

 C:> docker version -f "{{.Server.Os}}"
 windows

 C:> docker version -f "{{.Client.Os}}"
 windows

 C:> docker version -f "{{.Server.Arch}}"
 amd64

 C:> docker version -f "{{.Client.Arch}}"
 amd64

Upvotes: 2

KyleMit
KyleMit

Reputation: 30027

Running docker ps when docker is not running will print to the error stream. You might want to capture both the error and input stream, and then test for an error.

The following will return true/false without writing anything to the console:

$dockerIsRunning = (docker ps 2>&1 | Out-String) -match "^(?!error)"

Further Reading

Upvotes: 1

ABusy_developerT
ABusy_developerT

Reputation: 544

Try running either of these commands on Powershell or cmd, if docker is installed, you should get a error free response:

docker --version

OR

docker-compose --version

OR

docker ps

Upvotes: 32

Venryx
Venryx

Reputation: 18029

If using wsl2 on Windows, you can use:

wsl -l -v

Output will be something like the below:

  NAME                   STATE           VERSION
* Ubuntu                 Stopped         1
  docker-desktop         Running         2
  docker-desktop-data    Running         2

Upvotes: 2

hagrawal7777
hagrawal7777

Reputation: 14678

docker info

  • The operating-system independent way to check whether Docker is running is to ask Docker, using the docker info command. This option will work for both Windows and Linux distributions.
  • If Docker is running when you will get result as shown below, otherwise you will get an error message:
C:\Users\himanshu.agrawal>docker info
Client:
 Debug Mode: false
 Plugins:
  scan: Docker Scan (Docker Inc., v0.3.4)

Server:
 Containers: 1
  Running: 0
  Paused: 0
  Stopped: 1
 Images: 2
 Server Version: 19.03.13
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 8fba4e9a7d01810a393d5d25a3621dc101981175
 runc version: dc9208a3303feef5b3839f4323d9beb36df0a9dd
 init version: fec3683
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 5.4.39-linuxkit
 Operating System: Docker Desktop
 OSType: linux
 Architecture: x86_64
 CPUs: 2
 Total Memory: 1.915GiB
 Name: docker-desktop
 ID: HHIB:HQRB:7VBA:LBUY:HKVJ:LFZ3:FSWZ:4ARP:74ZB:TIWO:WTMG:LHZH
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false
 Product License: Community Engine

Get-Process 'com.docker.proxy'

  • This option can only be used with Windows Power Shell (just Power Shell, not even with CMD). If Docker is running you will get output as below, otherwise you will get an error message:
PS C:\Users\himanshu.agrawal> Get-Process 'com.docker.proxy'

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
    178      15    22984      25172       0.77  14224   1 com.docker.proxy

Other options - Linux specific

  • You can also use operating system utilities, such as sudo systemctl is-active docker or sudo status docker or sudo service docker status, or checking the service status using Windows utilities.
  • Finally, you can check in the process list for the "dockerd" process, using commands like ps or top.

Source 1

Upvotes: 6

TimFinnegan
TimFinnegan

Reputation: 613

I had the same issue while writing some scripts to work with Docker containers on Windows.

The answers given were not applicable for me because they just took too much time. I assumed that "is Docker running on Windows" also meant that the default VM was running. So instead the other answers I checked for the Docker default machine IP 192.168.99.100 which should normally be running when Docker is on. I then just pinged it:

ping -n <numberOfPings> -w <waitingTimeInMilliSeconds> 192.168.99.100

#Example:
ping -n 1 -w 1000 192.168.99.100

I found when Docker is running I normally get a response in less than 1ms which means the check is quite fast. This also means that it should be very robust to wait for even less than 1000ms if the Docker default machine is not running.

Upvotes: 1

atline
atline

Reputation: 31624

Afford two methods:

  1. docker version

    This method works both for cmd & powershell, but if for cmd, you need to use echo %errorlevel% to check the result.

    If docker daemon is running, it will be like next:

    PS C:\> docker version
    Client: Docker Engine - Community
    Version:           18.09.2
    API version:       1.39
    Go version:        go1.10.8
    Git commit:        6247962
    Built:             Sun Feb 10 04:12:31 2019
    OS/Arch:           windows/amd64
    Experimental:      false
    
    Server: Docker Engine - Community
     Engine:
      Version:          18.09.2
      API version:      1.39 (minimum version 1.12)
      Go version:       go1.10.6
      Git commit:       6247962
      Built:            Sun Feb 10 04:13:06 2019
      OS/Arch:          linux/amd64
      Experimental:     false
    PS C:\> echo $?
    True
    

    If docker daemon not running, it will be next:

    PS C:\> docker version
    Client: Docker Engine - Community
    Version:           18.09.2
    API version:       1.39
    Go version:        go1.10.8
    Git commit:        6247962
    Built:             Sun Feb 10 04:12:31 2019
    OS/Arch:           windows/amd64
    Experimental:      false
    Error response from daemon: An invalid argument was supplied.
    PS C:\> echo $?
    False
    
  2. Get-Process:

    This method just works for powershell.

    If docker daemon is running, it will be next:

    PS C:\> Get-Process 'com.docker.proxy'
    Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
    -------  ------    -----      -----     ------     --  -- -----------
        205      10    11416      18860       0.13  12620   2 com.docker.proxy
    PS C:\> echo $?
    True
    

    If docker daemon is not running, it will be next:

    PS C:\> Get-Process 'com.docker.proxy'
    Get-Process : Cannot find a process with the name "com.docker.proxy". Verify the process name and call the cmdlet
    again.
    At line:1 char:1
    + Get-Process 'com.docker.proxy'
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (com.docker.proxy:String) [Get-Process], ProcessCommandException
        + FullyQualifiedErrorId : NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.GetProcessCommand
    
    PS C:\> echo $?
    False
    

Upvotes: 19

Related Questions