I want to delete the remains of some Docker-operations from within Jenkins.
But somehow the following line does not work...
The issue seems to be with the parenthesis.
Any advice?
if [ docker images -f dangling=true -q|wc -l > 0 ]; then docker rmi --force $(docker images -f dangling=true -q);fi
Newer versions of Docker now have the system prune command.
To remove dangling images:
$ docker system prune
To remove dangling as well as unused images:
$ docker system prune --all
To prune volumes:
$ docker system prune --volumes
To prune the universe:
$ docker system prune --force --all --volumes
docker image prune deletes all dangling images. Docker image prune -a deletes unused images too. This thread explains what dangling and unused images are.
In short: Dangling image --> No tag, unused images --> no container attached.
Remove Dangling Images
Use -xargs will need --no-run-if-empty (-r) to bypass executing docker rmi with no arguments
docker images --quiet --filter=dangling=true | xargs --no-run-if-empty docker rmi
Use normar bash comand to check and delete
if docker images -f "dangling=true" | grep ago --quiet; then
docker rmi -f $(docker images -f "dangling=true" -q)
fi
I would store the output of the docker images command and then use it:
images=$(docker images -f dangling=true -q); if [[ ${images} ]]; then docker rmi --force ${images}; fi
Related
The following commands used to work before, but as of Docker version 19.03.8 - build afacb8b they are not working.
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q) -f
docker rmi $(docker images -f "dangling=true" -q)
This is the error I'm getting for the first docker command to stop all containers:
unknown shorthand flag: 'a' in -a
See 'docker stop --help'.
In case of using Windows OS, Faced similar issue, got it working when executed the command from windows PowerShell, preferably with admin privilege's.
I need to dynamically delete all docker images in a server, except for the postgres image and container.
Now I need a dynamic way to get the id of that docker image so i will know to avoid it, using:
docker rmi $(docker images -q | grep -v $<id_of_postgres_container>)
For the container part, i managed to find this:
docker ps -aqf "name=postgres"
which returns only the id of the postgres container. Is there any way to do the same with images without getting too deep into bash scripting?
or any better suggestions?
docker images --format="{{.Repository}} {{.ID}}" |
grep "^postgres " |
cut -d' ' -f2
Get docker images in the format repository<space>id, then filter lines starting with postgres<space>, then leave only id.
docker images --format="{{.Repository}} {{.ID}}" |
grep "^postgres " |
cut -d' ' -f2 |
xargs docker rmi
But, if the postgres container and image is currently running or used, you can just:
docker system prune --force --all
You can just use:
$ docker images -q [image_name]
Where image_name can contain tags (appended after :), registry username with / (if applicable), etc.
The image has to be downloaded for this to work, for example:
$ docker pull hello-world
...
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
$ docker images -q hello-world
d1165f221234
$ docker images -q hello-world:latest
d1165f221234
If the image is not locally available, the only alternative I can think of is to manually query the registry, e.g. like this.
docker rmi will never delete an image that corresponds to a running container. So if you have a container based on postgres running, and you want to delete every other image on your system, the age-old incantations will do what you want; I’m too old-school for docker system but the “get all of the image IDs, then try to delete them all” I know is
docker images -q | xargs docker rmi
Which will print out some errors, but will delete all of the images it can.
Such a feature is useful when running multiple docker commands in one that follow this pattern:
docker do_smth $(docker query_smth)
For example:
docker stop $(docker ps -q)
or
docker rm $(docker ps -a -q)
or
docker network rm $(docker inspect ... --format ...)
If the inner docker command returns an empty list, the outer command will fail because and will display the help.
"docker stop" requires at least 1 argument.
See 'docker stop --help'.
Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...] [flags]
Stop one or more running containers
Is there a way to silence docker or make docker not complain on empty lists? Something like: "Kill everybody. If there is no one, job done."
This would be similar to mkdir -p exiting_directory vs mkdir exiting_directory where the former will not complain if the directories exist.
For scripting where the result may be empty, I prefer to use xargs --no-run-if-empty:
docker ps -aq | xargs --no-run-if-empty docker rm
I have bash script which stops and remove all docker container by image name.
I can stop and remove all docker container by image name by single command
docker rm $(docker stop $( docker ps -a -q --filter ancestor=image_name))
But if container not exist this expression docker ps -a -q --filter ancestor=image_name not returns nothing and docker stop command fails. How can I remove all containers by image name and do nothing if it already removed?
Try this...
for i in $(docker ps -a -q --filter ancestor=image_name); do docker rm $(docker stop $i); done
It should only call docker rm if docker ps returns results. Works in my environment.
This question explains how to stop Docker containers started from an image.
But if there are no running containers I get the error docker stop requires a minimum of one argument. Which means I can't run this command in a long .sh script without it breaking.
How do I change these commands to work even if no results are found?
docker stop $(docker ps -q --filter ancestor="imagname")
docker rm `docker ps -aq` &&
(I'm looking for a pure Docker answer if possible, not a bash test, as I'm running my script over ssh so I don't think I have access to normal script tests)
Putting this in case we can help others:
To stop containers using specific image:
docker ps -q --filter ancestor="imagename" | xargs -r docker stop
To remove exited containers:
docker rm -v $(docker ps -a -q -f status=exited)
To remove unused images:
docker rmi $(docker images -f "dangling=true" -q)
If you are using a Docker > 1.9:
docker volume rm $(docker volume ls -qf dangling=true)
If you are using Docker <= 1.9, use this instead:
docker run -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/docker:/var/lib/docker --rm martin/docker-cleanup-volumes
Docker 1.13 Update:
To remove unused images:
docker image prune
To remove unused containers:
docker container prune
To remove unused volumes:
docker volume prune
To remove unused networks:
docker network prune
To remove all unused components:
docker system prune
IMPORTANT: Make sure you understand the commands and backup important data before executing this in production.