Skip to content
Advertisement

Using $() in docker commands doesn’t seem to work

I want to stop all running docker containers with the command sudo docker stop $(docker ps -a -q). But when I run it, docker outputs

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.39/containers/json?all=1: dial unix /var/run/docker.sock: connect: permission denied
"docker stop" requires at least 1 argument.
See 'docker stop --help'.

Usage:  docker stop [OPTIONS] CONTAINER [CONTAINER...]

Stop one or more running containers

Just running docker ps -a -q outputs the Docker IDs, but when I combine it with a Docker command, it doesn’t work. Thank you.

Advertisement

Answer

I didn’t realize that the sudo is required in the command substitution also:

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

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement