How to cleanup docker containers and images on linux machines - docker

We have Linux redhat machine with docker and docker compose
Now we want to clean all containers and images - like we have scratch new docker
As I understand to get that , we need to perform the following procedure with this order:
Am I right with this procedure? , or I missing something?
1. docker stop <CONTAINER ID>
2. docker container rm <CONTAINER ID>
3. docker image rm <IMAGE ID>
example
first find - CONTAINER ID
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
654fa81f4439 confluentinc/cp-enterprise-control-center:5.0.0 "/etc/confluent/dock…" 9 minutes ago Up 9 minutes 0.0.0.0:9021->9021/tcp control-center
1)
stop container
docker stop 654fa81f4439
654fa81f4439
2)
delete container
docker container rm 654fa81f4439
654fa81f4439
3)
find image ID
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
confluentinc/cp-enterprise-control-center 5.0.0 e0bd9a5edb95 15 months ago 617MB
delete image
ocker image rm e0bd9a5edb95
Untagged: confluentinc/cp-enterprise-control-center:5.0.0
Untagged: confluentinc/cp-enterprise-control-center#sha256:2e406ff8c6b1b8be6bf01ccdf68b14be0f0759db27c050dddce4b02ee0894127
Deleted: sha256:e0bd9a5edb9510a326934fa1a80a4875ab981c5007354de28f53bfb3e11bc34a
Deleted: sha256:c23255297f6d75f156baf963786d3ded1d045b726d74ed59c258dc8209bac078
Deleted: sha256:6cab492e72ca2578897b7ceecb196e728671158e262957f3c01e53fd42f6f8b4

In short, yes, it is the correct procedure to clear all the containers and images.
But you can do it more easily. For example:
Stop all containers at
once:
docker container stop $(docker container ls -aq)
Remove all stopped containers: docker container prune --force
Remove all unnused images: docker image prune --all --force
Sometimes Docker volumes are used for containers to persist data. You may want to clean them too (docker volume prune --force).
Others Docker resources may be left on your system (such as networks and build caches).
You can appeal to docker system prune to remove all unused data:
$ docker system prune --all --volumes
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all volumes not used by at least one container
- all images without at least one container associated to them
- all build cache
Are you sure you want to continue? [y/N] y

Related

docker rmi <hash> gives Error: No such image:

I believe I have tried every command recommended to remove the last few images from my sever, but I can't get rid of them.
I'm considering uninstalling docker and then remove /var/lib/docker and finally reinstalling docker.
Would that work?
Here's what I've been trying.
[root#s-app-t54 docker]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
[root#jenkins-agent-1 docker]# docker -v
Docker version 20.10.13, build a224086
[root#s-app-t54 docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root#jenkins-agent-1 docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis 6.0.10 621ceef7494a 14 months ago 104MB
tomcat 8.5 37bdd9cb0d0e 14 months ago 533MB
adoptopenjdk/openjdk8-openj9 jdk8u275-b01_openj9-0.23.0-alpine-slim 440045c41637 15 months ago 142MB
busybox latest dc3bacd8b5ea 16 months ago 1.23MB
redis 6 62f1d3402b78 17 months ago 104MB
httpd latest 3dd970e6b110 18 months ago 138MB
redis 5 f60d84d4d72c 20 months ago 98.3MB
stilliard/pure-ftpd latest 07037e0784e0 23 months ago 169MB
redis latest de25a81a5a0b 2 years ago 98.2MB
[root#jenkins-agent-1 docker]# docker volume ls
DRIVER VOLUME NAME
[root#jenkins-agent-1 docker]# docker rmi -f 621ceef7494a 37bdd9cb0d0e 440045c41637 \
dc3bacd8b5ea 62f1d3402b78 3dd970e6b110 f60d84d4d72c 07037e0784e0 de25a81a5a0b
Error: No such image: 621ceef7494a
Error: No such image: 37bdd9cb0d0e
Error: No such image: 440045c41637
Error: No such image: dc3bacd8b5ea
Error: No such image: 62f1d3402b78
Error: No such image: 3dd970e6b110
Error: No such image: f60d84d4d72c
Error: No such image: 07037e0784e0
Error: No such image: de25a81a5a0b
[root#jenkins-agent-1 docker]# docker system prune -a --volumes
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all volumes not used by at least one container
- all images without at least one container associated to them
- all build cache
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B
[root#jenkins-agent-1 docker]#
What will make it work will depend on what is actually causing the problem. But here are some possible solutions:
Solution 1: Restart the docker daemon:
# in case you are using Linux
sudo systemctl stop docker
sudo systemctl start docker
and then try to remove the images once again after that.
Solution 2: Try to remove the images as pointed out in this article
Solution 3: If nothing of that works, take a look at some answers replied on this other question. The accepted answer there has some side effects (read the comments related to it before doing that).
Try:- docker rmi -f $(docker image ls)
If it still says Error: No such image , then most probably your images are removed and you can verify again with: docker image ls
Edit - 1 :
I found the problem , sometimes the stopped containers store the images being used to create them.
First remove all the containers with : docker rm -f $(docker ps -a -q)
Then remove all the docker images with : docker rmi -f $(docker image ls)

docker already have container running

When I launch docker, it launch by default a few containers that I have build in the past.(I've use docker-compose at the time, but deleted the repo since)
I kill them, but each time i restart docker, they are back.
What can I do ?
I know there is something like "docker system prune",
but I would like to delete the less possible .
You can try running docker ps -a to get a list of all containers including the ones which are not running but stopped.
You can then docker rm each container you do not wish to start on each docker restart.
Use docker ps to see what containers are running.
Use this command to kill/stop all running containers.
docker rm $(docker ps -a -q)
Use docker images to get list of all images.
Use docker rmi <image_id> to delete desired image.

Zombie docker container that can't be killed

I use docker-compose to create a bunch of containers and link them together. For some of the container definitions, I might have restart: always as the restart policy.
Now I have a postgres container that respawns back to life if stopped.
$docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a8bb2b781630 postgres:latest "docker-entrypoint.s…" About an hour ago Up About an hour 5432/tcp dcat_postgres.1.z3pyl24kiq2n4clt0ua77nfx5
docker stop a8bb2b781630
a8bb2b781630
$ docker rm -f a8bb2b781630
a8bb2b781630
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
93fa7b72c2ca postgres:latest "docker-entrypoint.s…" 12 seconds ago Up 4 seconds 5432/tcp dcat_postgres.1.oucuo5zg3y9ws3p7jvlfztflb
Using docker-compose down in the dir that started the service doesn't work either.
$ docker-compose down
Removing dcat_postgres_1 ... done
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7ee7fb0e98cd postgres:latest "docker-entrypoint.s…" 13 seconds ago Up 5 seconds 5432/tcp dcat_postgres.1.jhv1q6vns1avakqjxdusmbb78
How can I kill a container and keep it from coming back to life?
EDIT: The container respawns even after restarting the Docker service.
Docker - 18.06.1-ce-mac73 (26764)
macOS High-Sierra, (10.13.6)
I figured it out. Turns out it was related to docker swarm. I had experimented with it at some point without fully understanding what it is and what it does and apparently it just stayed there.
All I had to do was:
docker swarm leave --force
and it worked like a head-shot to an actual zombie.
Can you try an option like moby/moby issue 10032:
docker stop $(docker ps -a -q) &
docker update --restart=no $(docker ps -a -q) &
systemctl restart docker
(this assume here you have only one running container: the one you cannot prevent to start)
A docker rm -f should be enough though, unless you are using docker with a provision tool like Puppet.
As it turned out, another process (other than docker itself) was responsible for the container to restart (here docker swarm)
Update 2020/2021: For multiple containers, possibly without having to restart the docker daemon
docker ps -a --format="{{.ID}}" | \
xargs docker update --restart=no | \
xargs docker stop
Check if you need, as in the issue, remove the images as well ( | xargs docker rmi $(docker images -qa) --force)

Find which container uses a Docker Volume

In running docker volume ls the response contains:
DRIVER VOLUME NAME
local 09033b4b22135832411ff485d5adc90e778316c1b9ba27bf8032cb65b8de557b
local 2ae85f4d2270716f936c4ef06e1a9408f90826258c7489a150125ff9d13ce79c
local 58497ac069495708d3bd17aab2b16b9a02badd245de4614a05a7133bdf0efb34
local eb958485be4ae9c8a71a01a4f03c7cbf9f76c3a8fb2622ded58cf96542373b0d
local efb5d345f272b32f409800e4804e22a93759c5668c8cb43e1887b5943ea217f9
How can I correlate those volume names to the containers using them?
I'm trying to understand where the volumes came from and if I can remove them.
Simple example starting a centos container using a named volume
docker run --rm -dit -v my-vol:/my-vol centos
You can make use of the --filter flag to see which container(s) the volume is being used by[1]:
docker ps -a --filter volume=my-vol
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f70a651c472 centos "/bin/bash" 24 seconds ago Up 23 seconds distracted_swirles
If you want to remove volumes that are not being used by any containers, you can use docker volume prune
docker volume prune
WARNING! This will remove all local volumes not used by at least one container.
Are you sure you want to continue? [y/N]
[1] https://docs.docker.com/engine/reference/commandline/ps/#filtering
[2] https://docs.docker.com/engine/reference/commandline/volume_prune/

Kill docker-compose containers without docker-compose.yml file

I have some docker containers provisioned by docker-compose with restart: always flag. But I accidentally delete docker-compose.yml file.
How do I delete those containers so that they will not restart automatically?
Without the compose file, docker-compose can't guess the name of the containers that need to be killed. You need to do that manually using docker command:
docker container ls
docker rm -f <container-name-or-id>
You can use docker ps -a to list all the running containers. After you can use docker stop container-name to stop individually the containers you want and after you can use docker rm $(docker ps -a -q) to delete and remove all the stopped containers.
Also if you want to delete the docker images as well you can use docker images to list the existing images and after docker rmi image-name to delete the image.
I had a similar issue - I didn't want to fetch the .yml files just to remove the containers. As docker-compose adds labels to the containers, we can use these to filter the matching containers and then remove them.
To remove all the containers that docker-compose created for project ProjectName (normally the name of the folder where the docker-compose.yml was) in shell (bash or similar), execute the following:
docker ps --filter 'label=com.docker.compose.project=ProjectName' --format {{.ID}} | xarg -n 1 docker rm --force --volumes
docker ps with the filter provides the list of containers, the format is provided so that only container IDs are printed
xargs -n 1 - takes the list of container IDs and executes the target command on each of them providing the container ID as the argument to the target
docker rm removes existing containers
--force - to also stop running containers (otherwise running containers would not be stopped or removed)
--volumes - to also remove any anonymous volumes associated with the containers

Resources