0

When I run docker stop $(docker ps -a -q) to stop all Docker containers. It works fine. It stops all running containers.

But if I add to an NPM script, like:

package.json

"scripts": {
  "docker:stop-all": "docker:stop-all": "docker stop $(docker ps -a -q)"
}

And I run: npm run docker:stop-all

I'm getting this error:

> docker stop $(docker ps -a -q)

unknown shorthand flag: 'a' in -a    

See 'docker stop --help'.  

How can I execute that stop-all command by using an NPM script?

cbdeveloper
  • 27,898
  • 37
  • 155
  • 336
  • You can create a `stop-all.sh` script containing `docker stop $(docker ps -a -q)`, then call it by `docker:stop-all": "bash ./path/to/stop-all.sh"`. – Brian Lee Nov 23 '20 at 15:48
  • Thanks. Is this the easier way to go? Will it work on Windows PowerShell? Or is it a Linux-specific solution? – cbdeveloper Nov 23 '20 at 15:50
  • It's *nix specific as it is, but if you're using the WSL it should be cross platform. – Brian Lee Nov 23 '20 at 15:52
  • @DigitalDrifter I got this: `The command 'docker' could not be found in this WSL 2 distro. We recommend to activate the WSL integration in Docker Desktop settings.` – cbdeveloper Nov 23 '20 at 15:53
  • I don't use Windows myself, so I can't say for certain, but I would assume it would work if you install docker desktop and ensure the docker executable is in your PATH variable. – Brian Lee Nov 23 '20 at 15:56
  • @DigitalDrifter it works on Git bash. But does not work on PowerShell. – cbdeveloper Nov 23 '20 at 15:56
  • Will research further. Thank you! – cbdeveloper Nov 23 '20 at 15:56

1 Answers1

0

Just found out what was the problem.

When you call npm run, npm itself will run a shell to run those instructions.

And that shell should be able to understand the instructions.

If npm calls PowerShell (on Windows), for example, it will not understand $(docker ps -a -q) since it's a Bash specific instruction.

So you need to config npm so it will execute bash or git-bash instead of Windows cmd or PowerShell.

I found out how to set that config on this question.

npm config set script-shell "C:\\Program Files\\git\\bin\\bash.exe"

You can list your npm configs to check that it worked:

npm config ls
cbdeveloper
  • 27,898
  • 37
  • 155
  • 336