How to remove non-latest image in Docker? - docker

I have a docker image for debian where its tag is not latest
REPOSITORY TAG IMAGE ID CREATED SIZE
debian 9.3 1b3ec9d977fb 12 months ago 100MB
When I run:
docker rmi debian
I get the following:
Error: No such image: debian
How do I remove this image?

Debian is the repository. You can remove by repository:tag
docker rmi debian:9.3

You can use the docker rmi -f flag to untag/remove images by ID.
docker rmi -f 1b3ec9d977fb

Related

Docker - extended options for docker image purge

I am not able to find the way to delete a certain Docker Image using the IMAGE ID and I can't figure it out from the Docker Documentation.
the syntax is: docker image prune --filter "label=key=value"
How can I delete image with ID=24de51a55d98 (second image) from the output below?
root#ubuntu:/home/vg/proj# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
vbgtest2018/node_p latest 0eca292619dd 17 minutes ago 174MB
**None None 24de51a55d98 23 minutes ago 170MB**
node alpine 025c3cbb849f 11 days ago 169MB
use docker rmi command.
rmi - remove image
docker rmi [OPTIONS] IMAGE [IMAGE...]
in your case-
docker rmi 24de51a55d98
or even you can use
docker rmi -f 24de51a55d98
Can be done with this command -
docker image rm {image-id} -f
In you case
docker image rm 24de51a55d98 -f

How to delete a docker image?

My project includes a Dockerfile to build an image. I've made modifications to that Dockerfile and want to see if it works. So i closed my program and listed all docker images in powershell using docker image ls -a. I deletetd all unused images using docker rmi -f $(docker images -a -q) and docker image prune -a.
But my image keeps not getting deleted, but simply 'untagged':
Untagged: my_image:latest
Untagged: my_image#sha256:16eb92a476be0...
All containers are stopped and deleted before trying to remove the image.
When i restart my application to build a new image, the old one keeps getting used:
> docker image ls -a
REPOSITORY TAG IMAGE ID CREATED
/my_image latest 0d79904a74b0 2 months ago
How do i actually physically remove the old image so my application can build a new one?
At first, you need to delete/stop the running container that is using your image(which one you want to remove).
docker ps -a: To see all the running containers in your machine.
docker stop <container_id>: To stop a running container.
docker rm <container_id>: To remove/delete a docker container(only if it stopped).
docker image ls: To see the list of all the available images with their tag, image id, creation time and size.
docker rmi <image_id>: To delete a specific image.
docker rmi -f <image_id>: To delete a docker image forcefully
docker rm -f (docker ps -a | awk '{print$1}'): To delete all the docker container available in your machine
docker image rm <image_name>: To delete a specific image
To remove the image, you have to remove/stop all the containers which are using it.
docker system prune -a: To clean the docker environment, removing all the containers and images.

Difference between docker rm IMAGE vs docker rmi IMAGE

According to Docker documentations, docker image rm "Remove one or more images" [1]. docker rmi has a same description [2], but then it goes on to say "Removes (and un-tags) one or more images from the host node."
Do docker image rm IMAGE and docker rmi IMAGE have an identical effect under any scenario? IMAGE is ID of the particular image that is to be removed.
The man page for docker rmi specifies that docker rmi is an alias for docker image rm. I suppose the documentation from docker is a little bit inconsistent in that regard. They write all the details for docker rmi while the documentation for docker image rm is lackluster.
It looks like docker rm is for containers and docker rmi is for images.
PS C:\Users\3des> docker rm httpd --force
Error: No such container: httpd
PS C:\Users\3des> docker rm httpd
Error: No such container: httpd
______________________________________________
PS C:\Users\3des> docker rmi httpd --force
Untagged: httpd:latest
Untagged: httpd#sha256:2fab99fb3b1c7ddfa99d7dc55de8dad0a62dbe3e7c605d78ecbdf2c6c49fd636
``
1). The docker image rm command "Remove one or more containers" not container image.
to check for container and its ID "docker ps -a" command
2). However, the docker rmi command means rm = remove i = image.
it is used to removing docker images. remembers that containers are created from images.
the "docker images or docker image ls" shows images, repository, TAG, and image ID.
docker rmi <image_ID> to remove image.
NOTE even when it is possible to create a new image from the existing image using the "docker commit" command, such image will come it its unique image id.

docker cannot remove images?

docker images
lists
test latest e10eb3d3a067 7 days ago 1.094 GB
But i tried to remove it using
docker rmi e10eb3d3a067
docker rmi -f <same_id>
It says, No such id :<different id, not the one i provided> and Error: failed to remove
Any suggestions to remove it?
I reffered this But i don't have any running contianers and no dependency between images that i want to remove.
Use the below command to list down all the containers
docker ps -a --format="container:{{.ID}} image:{{.Image}}"
Try to remove the image which you want
sudo docker rmi <image-id>
If you still get the same error then try to remove the containers associated with that image.
sudo docker rm <container-id>
Now try to remove the image

How does one remove a Docker image?

I'm running Docker under Vagrant under OS X 10.8.4 (Mountain Lion), and whenever I try to delete a saved image, I get an error:
$ docker rmi some-image-id
2013/07/15 hh:mm:ss unexpected JSON input
According to the rmi help, the proper syntax is docker rmi IMAGE [IMAGE...], and I'm not sure what to make of that.
How can I delete an image?
$ docker version
Client version: 0.4.8
Server version: 0.4.8
Go version: go1.1
 
$docker info
Containers: 1
Images: 3
Interestingly, when I run docker ps, no containers show up at all. Running docker images shows four (4) base images and one (1) node image.
Try docker rmi node. That should work.
Seeing all created containers is as simple as docker ps -a.
To remove all existing containers (not images!) run docker rm $(docker ps -aq)
The following are some of the ways to remove docker images/containers:
Remove single image
docker rmi image_name:version/image-id
Remove all images
docker rmi $(docker images -qf "dangling=true")
Kill containers and remove them:
docker rm $(docker kill $(docker ps -aq))
Note: Replace kill with stop for graceful shutdown
Remove all images except "my-image"
Use grep to remove all except my-image and ubuntu
docker rmi $(docker images | grep -v 'ubuntu\|my-image' | awk {'print $3'})
Or (without awk)
docker rmi $(docker images --quiet | grep -v $(docker images --quiet ubuntu:my-image))
Delete all docker containers
docker rm $(docker ps -a -q)
Delete all docker images
docker rmi $(docker images -q)
To remove an image from Docker using the image ID:
Get the list of all Images
docker images
Identify the image ID of the image you want to delete, for example:
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
kweku360/java latest 08d3a9b8e166 2 weeks ago 5.733 GB`
Finally remove the image using the image ID (only the first three digits are required)
docker rmi 08d
Image:
List images
docker images
Remove one image
docker rmi image_name
Force remove one image
docker rmi -f image_name
Container:
List all containers
docker ps -a
Remove one container
docker rm container_id
Force remove one container
docker rm -f container_id
Update, as commented by VonC in How to remove old Docker containers.
With Docker 1.13 (Q4 2016), you now have:
docker system prune will delete ALL unused data (i.e., in order: containers stopped, volumes without containers and images with no containers).
See PR 26108 and commit 86de7c0, which are introducing a few new commands to help facilitate visualizing how much space the Docker daemon data is taking on disk and allowing for easily cleaning up "unneeded" excess.
docker system prune
WARNING! This will remove:
- all stopped containers
- all volumes not used by at least one container
- all images without at least one container associated to them
Are you sure you want to continue? [y/N] y
Removing Containers
To remove a specific container
docker rm CONTAINER_ID CONTAINER_ID
For single image
docker rm 70c0e19168cf
For multiple images
docker rm 70c0e19168cf c2ce80b62174
Remove exited containers
docker ps -a -f status=exited
Remove all the containers
docker ps -q -a | xargs docker rm
Removing Images
docker rmi IMAGE_ID
Remove specific images
for single image
docker rmi ubuntu
for multiple images
docker rmi ubuntu alpine
Remove dangling images
Dangling images are layers that have no relationship to any tagged images as the Docker images are constituted of multiple images.
docker rmi -f $(docker images -f dangling=true -q)
Remove all Docker images
docker rmi -f $(docker images -a -q)
Removing Volumes
To list volumes, run docker volume ls
Remove a specific volume
docker volume rm VOLUME_NAME
Remove dangling volumes
docker volume rm $(docker volume ls -f dangling=true -q)
Remove a container and its volumes
docker rm -v CONTAINER_NAME
docker rm container_name
docker rmi image_name
docker help
rm Remove one or more containers
rmi Remove one or more images
docker rmi 91c95931e552
Error response from daemon: Conflict, cannot delete 91c95931e552 because the container 76068d66b290 is using it, use -f to force
FATA[0000] Error: failed to remove one or more images
Find container ID,
# docker ps -a
# docker rm daf644660736
First of all, we have to stop and remove the Docker containers which are attached with the Docker image that we are going to remove.
So for that, first
docker stop container-id - To stop the running container
docker rm container-id - To delete/remove the container
then,
docker rmi image-id - To delete/remove the image
For versions 1.13 and higher:
docker image rm [OPTIONS] IMAGE [IMAGE...]
Comparing:
the documention of docker image rm and
the documentation of docker rmi,
the [OPTIONS] seem to have no difference.
--force , -f Force removal of the image
--no-prune Do not delete untagged parents
From: Introducing Docker 1.13
CLI restructured
In Docker 1.13, we regrouped every command to sit under the logical object it’s interacting with. For example list and start of containers are now subcommands of docker container and history is a subcommand of docker image.
These changes let us clean up the Docker CLI syntax, improve help text and make Docker simpler to use. The old command syntax is still supported, but we encourage everybody to adopt the new syntax.
Docker provides some command to remove images.
Show/Remove Images:
docker images
docker images -a # All images
docker images --no-trunc # List the full length image IDs
docker images --filter "dangling=true" // Show unstage images
docker rmi $(docker images -f "dangling=true" -q) # Remove on unstages images
docker rmi <REPOSITORY> or <Image ID> # Remove a single image
docker image prune # Interactively remove dangling images
docker image prune -a # Remove all images
or
docker rmi -f $(sudo docker images -a -q)
Also, you can also use filter parameters to remove set of images at
once:
Example:
$docker images --filter "before=<hello-world>" // It will all images before hello-world
$docker images --filter "since=<hello-world>" // It will all images since hello-world
So you can delete that filter images like this:
docker rmi $(docker images --filter "since=<hello-world>")
docker rmi $(docker images --filter "before=<hello-world>")
Delete all of them using
Step 1: Kill all containers
for i in `sudo docker ps -a | awk '{ print $1 }'`; do sudo docker kill $i ; done
Step 2: RM them first
for i in `sudo docker ps -a | awk '{ print $1 }'`; do sudo docker rm $i ; done
Step 3: Delete the images using force
for i in `sudo docker images | awk '{ print $3}'`; do sudo docker rmi --force $i ; done
Use the step 1 in case you are getting error saying it cant be deleted owing to child dependencies
If you want to automatically/periodically clean up exited containers and remove images and volumes that aren't in use by a running container you can download the image meltwater/docker-cleanup.
I use this on production since we deploy several times a day on multiple servers, and I don't want to go to every server to clean up (that would be a pain).
Just run:
docker run -d -v /var/run/docker.sock:/var/run/docker.sock:rw -v /var/lib/docker:/var/lib/docker:rw --restart=unless-stopped meltwater/docker-cleanup:latest
It will run every 30 minutes (or however long you set it using DELAY_TIME=1800 option) and clean up exited containers and images.
More details: https://github.com/meltwater/docker-cleanup/blob/master/README.md
Here's a shell script to remove a tagged (named) image and it's containers.
Save as docker-rmi and run using 'docker-rmi my-image-name'
#!/bin/bash
IMAGE=$1
if [ "$IMAGE" == "" ] ; then
echo "Missing image argument"
exit 2
fi
docker ps -qa -f "ancestor=$IMAGE" | xargs docker rm
docker rmi $IMAGE
In my case the probleme is that I have tow images withe same name
the solution is to add the tag after the name or the id
sudo docker rmi <NAME>:<TAG>
ex:
sudo docker rmi php:7.0.4-fpm
Why nobody mentioned docker-compose! I 've just been using it for one week, and I cannot survive without it. All you need is writing a yml which takes only several minutes of studying, and then you are ready to go. It can boot images, containers (which are needed in so-called services) and let you review logs just like you use with docker native commands. Git it a try:
docker-compose up -d
docker-compose down --rmi 'local'
Before I used docker-compose, I wrote my own shell script, then I had to customize the script whenever needed especially when application architecture changed. Now I don't have to do this anymore, thanks to docker-compose.
For me the following worked fine:
> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
debian jessie 86baf4e8cde9 3 weeks ago 123MB
ubuntu yakkety 7d3f705d307c 3 weeks ago 107MB
alpine 3.5 074d602a59d7 7 weeks ago 3.99MB
Then go ahead and remove an image by running some like these:
> docker rmi debian:jessie
> docker rmi ubuntu:yakkety
> docker rmi alipine:3.5
List images:
ahanjura#ubuntu:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
88282f8eda00 19 seconds ago 308.5 MB
13e5d3d682f4 19 hours ago 663 MB
busybox2 latest 05fe66bb1144 20 hours ago 1.129 MB
ubuntu 16.04 00fd29ccc6f1 5 days ago 110.5 MB
ubuntu 14.04 67759a80360c 5 days ago 221.4 MB
python 2.7 9e92c8430ba0 7 days ago 680.7 MB
busybox latest 6ad733544a63 6 weeks ago 1.129 MB
ubuntu 16.10 7d3f705d307c 5 months ago 106.7 MB
Delete images:
ahanjura#ubuntu:~$ sudo docker rmi 88282f8eda00
Deleted: sha256:88282f8eda0036f85b5652c44d158308c6f86895ef1345dfa788318e6ba31194
Deleted: sha256:4f211a991fb392cd794bc9ad8833149cd9400c5955958c4017b1e2dc415e25e9
Deleted: sha256:8cc6917ac7f0dcb74969ae7958fe80b4a4ea7b3223fc888dfe1aef42f43df6f8
Deleted: sha256:b74a8932cff5e61c3fd2cc39de3c0989bdfd5c2e5f72b8f99f2807595f8ece43
ahanjura#ubuntu:~$ sudo docker rmi 13e5d3d682f4
Error response from daemon: conflict: unable to delete 13e5d3d682f4 (must be forced) - image is being used by stopped container 5593e25eb638
Delete by force:
ahanjura#ubuntu:~$ sudo docker rmi -f 13e5d3d682f4
Deleted: sha256:13e5d3d682f4de973780b35a3393c46eb314ef3db45d3ae83baf2dd9d702747e
Deleted: sha256:3ad9381c7041c03768ccd855ec86caa6bc0244223f10b0465c4898bdb21dc378
Deleted: sha256:5ccb917bce7bc8d3748eccf677d7b60dd101ed3e7fd2aedebd521735276606af
Deleted: sha256:18356d19b91f0abcc04496729c9a4c49e695dbfe3f0bb1c595f30a7d4d264ebf
First list all your images which are present by using :
docker images
For removing single image
Use docker rmi [image-name (or) image-id]
// This will remove only that specific image
For removing All images
Use docker rmi -f $(docker images -a -q)

Resources