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)
Related
I run docker ps and it shows that 5 containers that have been running for three weeks.
I then run docker-compose down but when I run docker ps again, they are all still running.
I have tried the following command but none seems to work
kill
stop
down --rmi local
rm
down
How can I stop these? I tried just bringing up my new docker-compose.yml and ignoring the olde one but I get:
ERROR: for apache Cannot create container for service apache: Conflict. The container name "/apache" is already in use by container "70c570d60e1248292f279a37634fd8b4ce7e2535d2bfa14b2c6e4089652c0152". You have to remove (or rename) that container to be able to reuse that name.
What to try to stop the old container?
You can list containers:
(base) docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c788727f0f7b postgres:14 "docker-entrypoint.s…" 7 days ago Up 7 days 0.0.0.0:5432->5432/tcp, :::5432->5432/tcp dev-db
88e8ddcb7d4e redis "docker-entrypoint.s…" 7 days ago Up 7 days 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp beautiful_neumann
Delete container:
(base) docker rm -f c788727f0f7b # container_id
c788727f0f7b
List containers:
(base) docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
88e8ddcb7d4e redis "docker-entrypoint.s…" 7 days ago Up 7 days 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp beautiful_neumann
As you can see the container got stopped(c788727f0f7b).
You can list stopped containers using:
docker container ls -f 'status=exited'
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
I have docker installed on my mac and when I do docker info I see this information:
Containers: 9
Running: 8
Paused: 0
Stopped: 1
Images: 5
But, when I run docker container ls -a or docker ps -a Then I see nothing running there.
I can see the 5 running Images when I do this: docker images -a ,
REPOSITORY TAG IMAGE ID CREATED SIZE
k8s.gcr.io/kube-apiserver-amd64 v1.10.11 e851a7aeb6e8 3 months ago 228MB
k8s.gcr.io/kube-controller-manager-amd64 v1.10.11 978cfa2028bf 3 months ago 151MB
k8s.gcr.io/kube-scheduler-amd64 v1.10.11 d2c751d562c6 3 months ago 51.2MB
k8s.gcr.io/etcd-amd64 3.1.12 52920ad46f5b 11 months ago 193MB
k8s.gcr.io/pause-amd64 3.1 da86e6ba6ca1 14 months ago 742kB
But I'm unable to delete them.
I tried doing this: docker rmi <image_id> or even this: docker rmi $(docker images -aq)
both gave errors like this:
Error response from daemon: conflict: unable to delete <image_id> (cannot be forced) - image is being used by running container <container_id>
When I try to kill one of these "running" container IDs, like this: docker kill <container_id>, I get the error:
Error response from daemon: Cannot kill container: <container_id>: Container <long_container_id> is not running
Or, if I do docker stop <container_id> then I just get back the container ID meaning the container is already stopped I guess? and yet even After that, trying to remove the images, gives the same error.
So, how do I get rid of these running "ghost" containers and images?
if you don't need the container, you can remove the container the same way you would the image
docker rm <container_id>
then after that you should have better luck removing the image
docker rmi <image_id>
or as you had to remove all images
docker rmi $(docker images -qa)
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)
I have some docker image. In my example it is called sample_nginx:
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
sample_nginx latest 4b34f8307839 34 hours ago 231.6 MB
I can run it docker run sample_nginx and in the colomn "PORTS" I can see what ports are exposed from that image:
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
650d7a9fe46e sample_nginx:latest "/bin/sh -c 'nginx - 3 minutes ago Up 3 minutes 80/tcp sleepy_mclean
Is it possible to find out what ports are exposed from the image without running it?
You can use docker inspect on an image to find this (and a variety of other interesting things):
$ docker inspect redis:latest
The above command will give you a significant amount of detail. You can alternately pass a format argument to return just what you're looking for, as below.
$ docker inspect --format='{{.Config.ExposedPorts}}' redis:latest
map[6379/tcp:map[]]