Reputation: 4571
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
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
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
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)"
Upvotes: 1
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
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
Reputation: 14678
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
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
sudo systemctl is-active docker
or sudo status docker
or sudo service docker status
, or checking the service status using Windows utilities.ps
or top
.Upvotes: 6
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
Reputation: 31624
Afford two methods:
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
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