Ubuntu docker rm $(docker ps -a -q) Got permission denied - docker

I am trying to remove all the stopped container using the command sudo docker rm $(docker ps -a -q)
But its returns
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.35/containers/json?all=1: dial unix /var/run/docker.sock: connect: permission denied
"docker rm" requires at least 1 argument.
See 'docker rm --help'.
Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...] [flags]
Anyone can suggest me how can i remove all the stopped container.

You ran sudo for your outer docker command, but not for the inner one. Your bash shell expands that inner command before the sudo runs. So you either need to run the entire command in a root shell, or call sudo each time, e.g.:
sudo docker rm $(sudo docker ps -a -q)
You could also add your user to the docker group to avoid the need to sudo every command.

Related

How to remove all containers in the docker (permession denied) "all container's is active"

i am new use with docker, and I can't delete active containers, and I can't stop containers
using docker rm -v -f $(docker ps -qa) AND docker stop $(docker ps -a -q) && docker rm $(docker ps -a -q) and continuos error: Error response from daemon: cannot stop container: beef39...: permission denied or rror response from daemon: Could not kill running container 16c..., cannot remove - permission denied
full container's
enter image description here
I managed to solve this problem as follows!
execute: sudo aa-remove-unknown
and run standard docker commands to stop and then kill the container
he will clean the apparmor in linux, it's a bug with apparmor and blocks actions that shut down created dockers
Answering as I understand the question,
By looking at your error I think you don't have permission to run docker commands.
So,
Add user user account to that group
sudo usermod -aG docker $USER
Change the current real group ID
sudo newgrp docker
after that try to run your commands again.

Removing docker container list throws error in commandpromt

I am using Docker on windows and trying to remove all containers with names starting with 'test' using below command
docker rm -f $(docker ps -a -q -f name=test)
It throws exception
unknown shorthand flag: 'a' in -a
See 'docker rm --help'.
I also tried the post on stack overflow.
docker ps -a -q -f name=test | xargs docker rm
Here I am getting an exception
'xargs' is not recognized as an internal or external command,
operable program or batch file.
To remove docker image first you need to stop the container that is attached to that image. After doing that you can simply run
To Stop all container
docker container stop $(docker container ls -aq)
docker rmi $(docker images -a -q)
This will remove all docker images from your system. If you are not in root you need to use
sudo docker rmi $(docker images -a -q)

How to correct docker in makefile which requires at least 1 argument for remove all containers command

The docker command "docker container rm $(docker ps -aq) -f" works fine from the command line. However, when I try to run it from a makefile using the following target ("remove_all_containers")...
remove_all_containers:
docker container rm $(docker ps -aq) -f
I get the error message:
host_name$ make remove_all_containers
docker container rm -f
"docker container rm" requires at least 1 argument.
See 'docker container rm --help'.
Usage: docker container rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
make: *** [remove_all_containers] Error 1
Clearly, when executed from within the makefile, the "docker ps" command is not being properly being properly executed in a way where its results can be collected and passed into the "container rm" command.
My Question: How do I get the "docker ps" command to run correctly from within the makefile and pass its results correctly into the "docker rm" command, also within the makefile?
Thanks, in advance, for any assistance you can offer.
You need a second $ in your recipe:
remove_all_containers:
docker container rm $$(docker ps -aq) -f
# ^
The single $ is expanded as a makefile variable when the makefile is parsed. It expands to blank. Make therefore passes docker container rm -f to your shell. The second $ sign causes make to expand $$ to $, and it will pass docker container rm $(docker ps -aq) -f to bash, which I'm guessing is what you want.
Notice, if you put the shell in there as #EricMd proposed, it will run a shell command, but that command will be run at Makefile read time, as opposed to the time that the recipe is executed. If the docker ps -aq command is dependent on any other artifacts of your build it would not work.
Sounds like you don't have any containers in docker to remove. I sometimes use a different syntax for this scenario:
remove_all_containers:
docker container ls -aq | xargs --no-run-if-empty docker container rm -f
The xargs syntax will not run docker container rm if there are no containers to delete.
According to the documentation, docker ps -a should list all containers.
You obtained this message "docker container rm" requires at least 1 argument certainly because you forgot to prepend the command at stake with Make's shell builtin:
remove_all_containers:
docker container rm $(shell docker ps -aq) -f
Note also that the docker ps admits a filtering feature: the online doc describes the various flavors of the corresponding -f flag.
For example, below are three Bash alias examples that can be useful to (i) stop all containers, (ii) remove all stopped containers; and (iii) remove dangling images−that would be tagged as <none> when doing docker images ls:
alias docker-stop='docker stop $(docker ps -a -q)'
alias docker-clean='docker rm $(docker ps -a -q -f status=exited)'
alias docker-purge='docker rmi $(docker images -q -f dangling=true)'
I tested for 2 way follow bellow answer:
remove_all_containers:
docker container rm $$(docker ps -aq) -f
remove_all_containers:
docker container rm $(shell docker ps -aq) -f

Remove all stopped containers: "docker rm" requires at least 1 argument

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.
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.
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.
$ 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)
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:
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.

Unable to stop or remove a container in Docker, Permission denied is displayed

I have run several containers from an image in different ports, but I cant stop or remove these containers. I have run this command:
# sudo docker rm -f f85956145f61
And no message are displayed, however container is still displayed.
I have tried with these commands:
# docker stop $(docker ps -a -q)
# docker rm $(docker ps -a -q)
But I get the following error:
# Get http:///var/run/docker.sock/v1.15/containers/json?all=1: dial unix /var/run/docker.sock: permission denied
So, how can I remove all containers from my docker?
I don't know if you are trying to stop the container as root, but if not try to stop/kill the container as root or using sudo.
$ sudo docker kill $(docker ps -q)
$ sudo docker rm $(docker ps -a -q)
If this does not work try restarting docker service
$ sudo service docker restart
and then try again to stop and delete the container. If it doesn't work you can try to kill the process(es) running inside the container from your host machine.
And to reference the container in docker kill, docker stop, docker rm,... and so on, you can either specify the container id or the container name, both are valid.
Another reason for this happening is that the daemon is busy committing the container.

Resources