Can't stop and remove docker containers - docker

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

Related

How to stop running always unknown docker container

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 ?

What is the difference between "docker container prune" vs "docker rm $(docker container ls -aq)"

I'm reading through the Docker documentation and I don't understand the difference between:
docker container prune
and
docker rm $(docker container ls -aq)
Note that in the link, the second command I've listed is docker rm $(docker ps -a -q), but there is no difference between that and what I've written. container ls is just the newer version of the ps command.
It seems that both of these commands remove all stopped containers. Is there more to it than that, or are these just synonyms?
I don't think there is substantial difference. This -a though means list all containers and as a result docker rm ... will also try to remove running containers. This gives the error that you see below:
Error response from daemon: You cannot remove a running container [...] Stop the container before attempting removal or force remove
example:
$ docker container run --rm -itd alpine:latest
0ec4d7459d35749ecc24cc5c6fd748f4254b0782f73f1ede76cf49b1fc53b2d4
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0ec4d7459d35 alpine:latest "/bin/sh" 4 seconds ago Up 1 second jovial_ritchie
$ docker rm $(docker container ls -aq)
Error response from daemon: You cannot remove a running container 0ec4d7459d35749ecc24cc5c6fd748f4254b0782f73f1ede76cf49b1fc53b2d4. Stop the container before attempting removal or force remove
$ docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B
But... difference when --force, -f is used:
In this case, the commands do 2 different things:
docker rm -f ... Forces the removal of a running container (uses SIGKILL) which means that it will remove running containers.
$ docker rm -f $(docker container ls -aq)
0ec4d7459d35
docker container prune -f will remove all stopped containers without asking for confirmation (no [y/N] prompt will be printed).
$ docker container prune -f
Total reclaimed space: 0B
The effects of the two commands are indeed similar, but there are some nuances to consider:
docker container prune can be used with the --filter option.
docker container prune has a synchronous protection that blocks concurrent prune executions on the daemon.
docker container prune attempts to remove only the containers that are not running, instead of trying to delete all containers and relying on the daemon to throw an exception for those that are not stopped, therefore is quicker and does not generate unnecessary error logs in case someone is tracking the daemon logs.
docker container prune builds a report at the end of its execution, providing the reclaimed space. The report is added in daemon.EventsService and implicitly displayed on the screen.
docker container prune is shorter
In the end of this answer I have a question: Why would someone type 15 additional characters to get the same result or worse?
docker system prune -f : to remove all the stopped containers (docker do not touch the running containers)
docker system prune -a : to remove all the stopped containers (docker do not touch the running containers) + unused images
docker rm <container_id> : remove a specific container, it should be stopped before (docker stop <container_id>)

Unable to delete docker images

I'm newby in Devops culture and also eager to learn and use but I get stuck every time when i try something new and now i can't delete images.
It says; it's being used by running container, stop it and then.....
See the screenshots:
enter image description here
enter image description here
enter image description here
Get running containers
docker ps
Get all running and stopped container
docker ps -a
Stop single container
docker stop <container_id>
Stop all containers
docker stop $(docker ps -aq)
Remove single container
docker rm <container_id>
Remove all containers
docker rm $(docker ps -aq)
Remove single image
docker rmi <image_id>
Remove all images
docker rmi $(docker images -q)
Remove everything from Docker host machine(use with caution because will delete everything like images, containers,networks etc)
docker system prune
You can just force remove the image even when there is a container that is still using it, if you don't mind doing that.
docker image rm <image-name> --force
Best way to delete all stopped containers is
docker container prune
As for the running containers, you should be able to list them with
docker container ls
add (--all) to see all (running/stopped) containers
docker container ls --all
Use docker ps -a to list all your running containers. You will find the ones still running. Stop them by using docker stop NAMEOFTHECONTAINER and remove them with docker rm NAMEOFTHECONTAINER.

Unable to run friendlyhello tutorial in Docker

I am trying to follow some tutorial in docker but sadly I stop in this situation. I followed step by step of this tutorial https://docs.docker.com/get-started/part2/#share-your-image. Now, when I try to run the command
docker run -d -p 4000:80 friendlyhello I got this error :
docker: Error response from daemon: driver failed programming external connectivity on endpoint gracious_poitras
> (791d1921b5d01b6e476c748de76c43ec3f92cd2bcd8069f4e36982f2b2e61759):
> Error starting userland proxy: mkdir
> /port/tcp:0.0.0.0:4000:tcp:172.17.0.2:80: input/output error
I am really new in docker and doesn't have any ideas on how to solve it.
Have a look at this thread on GitHub, particularly this comment. It appears that restarting the Docker daemon (and potentially upgrading) is fixing the issue for most people.
You can do this by running:
docker stop $(docker ps -a -q) to stop all running Docker containers, then restarting Docker on your machine.
Stop all the running containers docker stop $(docker ps -a -q) then
Stop the Docker on your machine & restart it.
For windows 10, docker stop $(docker ps -a -q) is not working. I did following steps.
get list of containers by calling docker ps -a -q
append containers list with stop query docker stop 0ab2490f8d9c 9dff282e510e 99f64a57b68f b0e9e4d273b1
restart docker from task tray.

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