How to remove <none> images after building successfull - docker

After I build my image, there are a bunch of images. When I try to delete them I get “image has dependent child images” errors. Is there anyway to clean this up?
These do NOT work:
docker rmi $(docker images -q)
docker rmi $(docker images | grep “^” | awk “{print $3}”)
docker rmi $(docker images -f “dangling=true” -q)

docker rmi `docker images | grep "<none>" | awk {'print $3'}`

Try this:
docker rmi $(docker images | grep none | awk '{print $3}')
You can add -f to force image removal but I do not recommend doing so. If you cannot remove the images with the command above that means it is in partially in use or stopped containers are using the images you are trying to remove.
You can always check the stopped or Existed containers with
docker ps -a
Here, the Status column will indicate the container status.

docker rmi docker images -a | grep "<none>" | awk {'print $3'}
Adding -a is required.

Related

Remove docker images by repository

I have a lot of docker images from the same repository, for example, docker.io/mycompany/myimage.
How can I delete all images from a particular repository?
docker images --format '{{.Repository}}:{{.Tag}}' | grep "^docker.io/mycompany/" | xargs -r docker rmi
To see the images that will be deleted without actually deleting them:
docker images --format '{{.Repository}}:{{.Tag}}' | grep "^docker.io/mycompany/" | xargs -r echo docker rmi

How to free wasted space used by docker?

I set up docker on my server. I use Jenkins for CI. Those tools are really cool. But, unfortunately, I still can't figure out how to get the wasted space back.
I tried different things proposed on the web and by using them I can only free a part of the wasted space. I need no any history and etc.
I tried different things, like rude docker rm $(docker ps -aq) and docker rmi $(docker images -q) while some of containers are running that make them prevented from deletion.
I also tried this script and similar.
#!/bin/bash
# remove exited containers:
docker ps --filter status=dead --filter status=exited -aq | xargs -r docker rm -v
# remove unused images:
docker images --no-trunc | grep '<none>' | awk '{ print $3 }' | xargs -r docker rmi
# remove unused volumes:
find '/var/lib/docker/volumes/' -mindepth 1 -maxdepth 1 -type d | grep -vFf <(
docker ps -aq | xargs docker inspect | jq -r '.[] | .Mounts | .[] | .Name | select(.)'
) | xargs -r rm -fr
I would like to expect that after first build of all images I have fixed size of the stuff by applying the clean up commands above. In other words I want the subsequent builds with clean up applied doesn't make the wasted space grow.
But anyway every execution of docker build occupies new space that I can't free up.
The directory /var/lib/docker/overlay grows up and doesn't consider my clean ups eventually.
Where am I wrong?
In the latest versions of Docker, you can now use docker system prune command (with --all option for complete cleanup).
https://docs.docker.com/engine/reference/commandline/system_prune/
In order to remove volumes use:
docker volume prune
In order to remove networks use:
docker network prune
In order to remove all stopped containers use:
docker container prune
In order to remove unused images use:
docker image prune

Remove all Docker containers except one

I have script that stops containers and then removes them
docker stop $(docker ps -q)
docker rm $(docker ps -a -q)
But I don't want to remove the docker container with name "my_docker".
How can I remove all containers except this one?
You can try this, which will
Filter out the unwanted item (grep -v), and then
returns the first column, which contains the container id
Run this command:
docker rm $(docker ps -a | grep -v "my_docker" | awk 'NR>1 {print $1}')
To use cut instead of awk, try this:
docker rm $(docker ps -a | grep -v "my_docker" | cut -d ' ' -f1)
Examples for awk/cut usage here: bash: shortest way to get n-th column of output
The title of the question asks for images, not containers. For those stumbling across this question looking to remove all images except one, you can use docker prune along with filter flags:
docker image prune -a --force --filter "label!=image_name"
replacing image_name with the name of your image.
You can also use the "until=" flag to prune your images by date.
This is what's actually happening docker rm $(List of container Ids). So it's just a matter of how you can filter the List of Container Ids.
For example: If you are looking to delete all the container but one with a specific container Id, then this docker rm $(docker ps -a -q | grep -v "my_container_id") will do the trick.
I achieved this by the following command:
docker image rm -f $(docker images -a | grep -v "image_repository_name" | awk 'NR>1 {print $1}')
For those, who want to exclude more than 1 container just add
grep -v "container_name2" |
after the grep -v "container_name1" command.
The final command might look like
docker rm $(docker ps -a | grep -v "my_docker1" | grep -v "my_docker2" | cut -d ' ' -f1)
I would prefer to test the container name using something along the lines of (untested)
docker inspect --format '{{ .Name }}' $(docker ps -aq)
this will give the names of the (running or not) containers, and you can filter and
docker rm
using this information
Using docker ps with --filter name=<my_docker>
docker rm $(docker ps -a -q | grep -v `docker ps -a -q --filter "name=my_docker"`)
Old question, but I like reviving posts.
For such case you could use Spotify's Docker GC: https://github.com/spotify/docker-gc#excluding-containers-from-garbage-collection
You could do:
echo "my_docker" >> /tmp/docker-gc-exclude-containers
echo '*' > /tmp/docker-gc-exclude
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v /etc:/etc:ro -v /tmp/docker-gc-exclude-containers:/etc/docker-gc-exclude-containers:ro -v /tmp/docker-gc-exclude:/etc/docker-gc-exclude:ro spotify/docker-gc
(if you would like to get your images cleaned off too, you can avoid mounting the docker-gc-exclude file)
To stop
docker stop $(docker ps -a -q | grep -v "my_container_id")
To remove
docker rm $(docker ps -a -q | grep -v "my_container_id")
Deletes all the images except node image
sudo docker rmi -f $(sudo docker image ls -a | grep -v "node" | awk 'NR>1 {print $3}')
I know its little late response but it can help any windows user. Here is the command I prepared:
docker rmi $(
docker image list -a --no-trunc --format "table {{.ID}},{{.Repository}}" |
Where-Object {
$_ -NOTMATCH "mcr.microsoft.com/dotnet/core/aspnet" -and
$_ -NOTMATCH "ubuntu" -and
$_ -NOTMATCH "busybox" -and
$_ -NOTMATCH "alpine"
} | Select-Object -Skip 1 | %{ $_.Split(',')[0];}
)
Maybe you can start all the containers you don't want to prune. Then, run:
docker container prune -a

Find the docker containers using an image?

If I have the ID of an image how can I find out which containers are using this image? When removing an image that is still used you get an error message:
$ docker rmi 77b0318b76b3
Error response from daemon: Conflict, cannot delete 77b0318b76b3 because the container 21ee2cbc7cec is using it, use -f to force
But how could I find this out in an automated way without trying to remove it?
You can try this:
docker ps -a | grep `docker images | grep IMAGE_ID | awk '{print $1":"$2}'` | awk '{print $1}'
With docker ps -a you get the list of all the containers including
the ones you are interested in.
The problem is that you have the IMAGE NAME there but you need the IMAGE ID.
You can use docker images to get the IMAGE ID for a given IMAGE NAME and that is what you use
in your grep to filter by your IMAGE ID.
Finally you get the first column to show only the CONTAINER IDs.
Example:
docker ps -a \
| grep `docker images | grep 3a041c1b0a05 | awk '{print $1":"$2}'` \
| awk '{print $1}'
Output:
4d6fb8a7149f
2baa726b1aa5
Hope this helps.
This will list all containers using your $IMAGE_ID.
$IMAGE_ID can be 77b0318b76b3 or namespace/repo:tag.
E.g.: $IMAGE_ID="77b0318b76b3"
docker container ls --all --filter=ancestor=$IMAGE_ID --format "{{.ID}}"
f you want to list the images of the active containers docker inspect -f '{{ .Config.Image}}' $(docker ps -q) and for all the containers docker inspect -f '{{ .Config.Image}}' $(docker ps -qa)

how to physically remove untagged docker images

when I run a command such as sudo docker rmi me/myimage I get the responce ...image untagged, but, when I rerun sudo docker images I can see that this "untagged" image is still there, and, if I run df -h I can see that the actual files still exist and occupy the file system space.
What command or procedure can I use to physically remove the unneeded images?
You should be able to remove untagged Docker images using the "dangling=true" flag:
sudo docker rmi $(sudo docker images -f "dangling=true" -q)
source:
https://docs.docker.com/engine/reference/commandline/images/
First you need to remove exited containers, then remove dangling images.
docker rm $(docker ps -q -f status=exited)
docker rmi $(docker images -q -f dangling=true)
After all, I created the below script as ~/bin/dclean and have been using it.
#!/bin/sh
processes=$(docker ps -q -f status=exited)
if [ -n "$processes" ]; then
docker rm $processes
fi
images=$(docker images -q -f dangling=true)
if [ -n "$images" ]; then
docker rmi $images
fi
If John Petrone solution doesn't work, try removing those images referring explicitly the IMAGE ID you see when you run docker images. You can remove all of them with one command
for i insudo docker images | grep \ | awk '{print $3}'; do sudo docker rmi $i; done
PD: I don't know John Petrone answer. It works nicely with Docker 1.4.1
This command will remove all the dangling images and containers from docker.
docker system prune -f
you can delete single images by their image id...
docker images
docker rmi <image-id>
This commands also work
docker rmi $(docker images | grep "^<none>" | awk '{print $3}')
Delete images with force to forgo stopped containers that might be using image
docker rmi -f $(docker images | grep "^<none>" | awk '{print $3}')
In my case, i have removed the untagged image by the
below command
# find untagged images
IMAGE_IDS=$(sudo docker images | grep "^<none>" | awk '{print $"3"}')
# in case of no untagged images found do nothing
if [ -n "$IMAGE_IDS" ]; then
sudo docker rmi $IMAGE_IDS > /dev/null 2>&1
fi

Resources