How to stop running always unknown docker container - docker

Hello I'm on a PC that someone else used and when I run docker ps, there are a lot of containers listed.
I tried to stop all of them using :
docker stop $(docker ps -a -q) && docker rm $(docker ps -aq)
but they are recreated automatically.
Can someone help because the PC is really slow ?

Related

Docker rm unable to remove all containers

I am trying to remove all stopped containers to free up some space on an AWS Ubuntu server that I am using. Docker documentation says to use docker rm $(docker ps -a -q) : https://docs.docker.com/engine/reference/commandline/rm/#remove-all-stopped-containers
However, I am getting the error below:
"docker rm" requires at least 1 argument.
See 'docker rm --help'.
Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
Any suggestions?
Adding sudo in front doesn't help. I am able to remove individual containers using docker rm 343e43ac4e86, but I don't want to spend a lot of time trying to figure out which containers are from older releases and removing them one by one.
I think you could also try docker ps -aq | xargs docker rm if the substitution doesn't work out.
Please use these command in your terminal:
docker stop $(docker ps -aq)
docker rm $(docker ps -aq)
docker network prune -f
docker rmi -f $(docker images --filter dangling=true -qa)
docker volume rm $(docker volume ls --filter dangling=true -q)

Docker stop all containers

I've seen many times the follwoing command to stop all docker containers:
docker stop $(docker ps -a -q)
There are two things that are not clear to me:
docker ps -a prints all containers, not only running ones, so what is the point to stop containers that are already stopped?
If docker ps returns/prints nothing (there are no running images) then docker stop blaims that it's not enough arguments.
What do I miss here? What is the best approach to cleanup an environment after docker?
use this will not run if the docker ps is empty:
docker ps -q | xargs --no-run-if-empty docker stop
normally you use rm and system prune if you really want to cleanup
You can also use this oneliner:
docker stop $(docker ps -aq | tr '\n' ' ')

Remove multiple containers from docker from Windows cmd

I want to remove multiple containers at a time using windows cmd I have used docker rm | docker ps -a -q but not working. Anyone, please help me on this.
docker rm $ (docker ps -a -q) this is not working on windows.
it's removing only stopped containers. You first stop the containers and then remove them using your command or forcefully remove them, also, check this out, this thread, as well docker system prune
$ docker stop $(docker ps -a -q)
$ docker rm $ (docker ps -a -q)
Hope this works on Windows cmd as well.
Use Windows powershell to run this command docker rm $ (docker ps -a -q)

Failing to remove stopped Docker container

I am trying to forcefully stop and remove all Docker images:
docker stop $(docker ps -a -q) && docker rm -f $(docker ps -a -q) && docker rmi -f $(docker images -a -q)
However, I receive:
Error response from daemon: conflict: unable to delete 3b5b05d98767 (cannot be forced) - image is being used by running container deedefb82e27.
As far as I understand, the container is restarting faster than the command tries to delete it.
The error is in removing the image, not the container. This is either a race condition from the container not being completely deleted yet, or you have something else starting containers on the system like swarm mode.
For a race condition, just add a few seconds between the commands to give the rm time to finish on the server. Also there's no need for a stop since you're doing an rm -f:
docker rm -f $(docker ps -a -q) \
&& sleep 2 && docker rmi -f $(docker images -a -q)
If you have containers running in swarm mode, first remove your stacks and services that you don't want to have running:
# something like this, will only work if you have stacks defined
docker stack rm $(docker stack ls --format '{{.Name}}')
# similar command for services
docker service rm $(docker service ls -q)
Each of those may take 10 seconds for the containers to exit, plus a few more seconds for the swarm manager to send the command, so you may want a sleep 15 after they both return to give the server time to complete the request.
You may have to preface all of your commands with sudo, or ensure that you are already in a root shell.
For example:
sudo docker stop $(sudo docker ps -a -q) && sudo docker rm -f $(sudo docker ps -a -q) && sudo docker rmi -f $(sudo docker images -a -q)

Can't stop and remove docker containers

I'm trying to stop and remove some docker containers on my machine, but something really weird is happening here.
I tried to stop:
docker stop $(docker ps -a -q)
This works for a moment, because the docker ps shows no container running. But few seconds later the images appear again with a new container ID.
I tried to stop (last command) then remove:
docker rm $(docker ps -a -q)
But I received an error:
Error: No such container: 0f57644645eb
I also tried this command on the running containers after repeat the stop command:
docker update --restart=no container-id
The command is successful, but the container are still restarting.
I tried to remove all images after stop them:
docker stop $(docker ps -a -q)
docker rmi $(docker images -q)
But I received another error:
Error response from daemon: conflict: unable to delete 0f57644645eb (cannot be forced) - image is being used by running container dcef9cdb703c
What I'm missing here? I would like to stop and remove these containers to leave Docker as it was after a fresh install.
I'm using Windows 10 and Docker version 17.12.0-ce-win47 (15139).
The behaviour indicates that the container was started as a service. The service will try to restart in order to fulfil the specification of running instances.
The services can be checked with docker service ls.
It is necessary to remove or reconfigure the service to permanently stop the container.
Could you just try to use the force flag with your removal commands?
docker rm -f $(docker ps -aq)
docker rmi -f $(docker images -q)
This worked for me:
docker rm CONTAINER_ID

Resources