Remove all stopped containers: “docker rm” require

2019-04-22 19:37发布

问题:

I'm reading a book on docker. It is a couple of years old.

I'll cite:

If you want to get rid of all your stopped containers, you can use
the output of docker ps -aq -f status=exited , which gets the
IDs of all stopped containers. For example:
$ docker rm -v $(docker ps -aq -f status=exited)

When I run this, I get:

michael@michael-desktop:~$ sudo docker rm -v $(docker ps -aq -f status=exited)
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.30/containers/json?all=1&filters=%7B%22status%22%3A%7B%22exited%22%3Atrue%7D%7D: dial unix /var/run/docker.sock: connect: permission denied
"docker rm" requires at least 1 argument(s).
See 'docker rm --help'.

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

Remove one or more containers

Could you help me understand what should I do to gain what is intended.

回答1:

It could simply means that you have no container with a status 'exited'.

The commands becomes then:

sudo docker rm -v

The lack of any parameter would trigger the error message you see.

But today, this would be done with docker container prune anyway.



回答2:

In order to remove all our stopped containers, you can first run

$ docker ps -a

This gives you the list of running and stopped containers, from which you can select what are the containers that you wanted to get rid. But if you want to get rid of all stopped containers, then you need to use

$ docker container prune 

This removes all stopped containers by giving you the following messages.

Warning! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
your container id list will be printed.


回答3:

$ sudo docker rm -v $(docker ps -aq -f status=exited)
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.30/containers/json?all=1&filters=%7B%22status%22%3A%7B%22exited%22%3Atrue%7D%7D:
dial unix /var/run/docker.sock: connect: permission denied
"docker rm" requires at least 1 argument(s).
See 'docker rm --help'.

The permission denied message comes from the embedded docker ps command. It is run by the shell outside of your parent sudo command, and the output is passed to sudo to run the docker rm as root. There are several fixes.

The easy option, run the docker ps with sudo:

$ sudo docker rm -v $(sudo docker ps -aq -f status=exited)

Option two is to run an entire shell as root:

$ sudo -s
# docker rm -v $(docker ps -aq -f status=exited)
# exit

Or you can give your user access to the docker socket so sudo is no longer needed:

$ sudo usermod -aG docker $USER
$ newgrp docker

The above is a one time change, and gives that user root access implicitly with docker. Then you can run:

$ docker rm -v $(docker ps -aq -f status=exited)


回答4:

What seems to be happening is docker was started with different user. Hence, docker ps -aq -f status=exited could not be run due permission issue and as a result got blank result.

Running docker rm complains of missing argument due to blank result.

michael@michael-desktop:~$ sudo docker rm -v $(docker ps -aq -f status=exited)
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock:


回答5:

Actually its an rights issue.... the error message:

Got permission denied while trying to connect to the Docker daemon socket at unix://

Tells you that you cant connect to you docker daemon which is running under root. You should decide if you want to use docker with # sudo or as root user.

Manage Docker as a non-root user

Further as said the docker rm complains about no images found for deletion therefore it wouldn't possible to delete images.

The command docker rm $(docker ps -aq -f status=exited) is just fine with newest docker version 18.09.0 but you could use docker container prune as well that is the more interactive way.



标签: docker