Windows Docker执行docker stop $(docker ps -q)报错求助
docker stop $(docker ps -q) Fails on Windows Command Line Great question! The issue here boils down to shell syntax differences between Linux/macOS bash and Windows' default command shells. Let's break it down:
When you run docker stop $(docker ps -q) in bash (like on Linux or Git Bash), the $() part tells the shell to first run docker ps -q, capture its output (the list of running container IDs), and pass those IDs as arguments to docker stop.
But Windows' default Command Prompt (cmd.exe) doesn't understand the $() syntax. Instead, it treats $(docker as a single argument to docker stop, followed by ps and -q) as separate arguments. That's why Docker throws the error about the unknown -q flag—it's seeing -q) as part of the command, not the valid -q option for docker ps.
Fixes for Different Windows Shells
1. Command Prompt (cmd.exe)
Use a for /f loop to capture the output of docker ps -q and pass each container ID to docker stop:
for /f "tokens=*" %i in ('docker ps -q') do docker stop %i
If you're putting this in a batch file, you'll need to double the percent signs:
for /f "tokens=*" %%i in ('docker ps -q') do docker stop %%i
2. PowerShell
PowerShell does support $() syntax, so your original command should work. But if you run into any odd parsing issues, you can use this pipe-based alternative which is more idiomatic for PowerShell:
docker ps -q | ForEach-Object { docker stop $_ }
3. Git Bash/WSL (Windows Subsystem for Linux)
If you're using a bash-compatible shell on Windows, your original command will work exactly as it does on Linux:
docker stop $(docker ps -q)
That should resolve the error you're seeing!
内容的提问来源于stack exchange,提问作者user3691223

