Docker cannot remove all images - docker

i'm using Docker Desktop for Windows. When I try to remove all of my images by this command:
docker rmi $(docker images -q)
I got this error message in command prompt:
unknown shorthand flag: 'q' in -q)
I'm running Docker on Window 10 Pro

Syntax is unix specific, so you change your command for Windows and you'll need to run the whole command in PowerShell or CMD.
remove containers:
powershell "docker ps -a -q | foreach-object { $i = $PSItem.ToString(); $cmd = 'docker'; & $cmd 'rm' '-f' $i }"
remove images:
powershell "docker images -a -q | foreach-object { $i = $PSItem.ToString(); $cmd = 'docker'; & $cmd 'rmi' '-f' $i }"

Old question, but if you are on Windows switch from the cmd to PowerShell. cmd is misinterpreting some commands and tries to attach the -q to the outter rmi command.

Use the docker system prune -a or docker image prune -a commands to delete any unused or dangling images.

Look at similar issue. The syntax you are using is unix specific docker rmi $(command). This is called command substitution and probably will not work in windows.

In the windows, you should windows command.
In powershell, it'll be something like:
docker images -a -q | foreach-object { $i = $PSItem.ToString(); $cmd = "docker"; & $cmd "rmi" $i }
Where
docker images -a -q
gets the IDs of all images (including intermediate), and
foreach-object { $i = $PSItem.ToString(); $cmd = "docker"; & $cmd "rmi" $i }
delete them one-by-one

That's a bash shell syntax which would work on Linux installs of docker. To get it to work on Windows, try installing the bash shell on Windows and running it from inside that shell. The Windows command prompt and powershell won't understand that syntax.

Windows 10 Powershell, it is easy.
Following command shows all images:
docker -a -q
outputs this:
PS C:\Users\UserName> docker images -a -q
86fa138dd93d
7c3a8b83a719
6636e7059d8d
then, for each image do
docker rmi ##########
Where ##### is the image id

Save this in docker-clear.bat and execute to clear containers, images, unused data and volumes
#echo off
REM Clear all the containers
FOR /f "tokens=*" %%i IN ('docker ps -aq') DO docker stop %%i && docker rm %%i
REM Clear all the images
FOR /f "tokens=*" %%i IN ('docker images --format "{{.ID}}"') DO docker rmi %%i -f
REM Clear unused data
docker system prune -f
REM Clear all volumes
docker volume prune -f

You forgot the -a. To delete all the images, use this:
docker image prune -a -f
And remember that you have to remove all the associated containers before removing all the images.
To delete all containers in use:
docker container prune -f

Related

Stop all docker containers at once on Windows

How can I stop all docker containers running on Windows?
docker stop is for 1 container only.
Any command/script to make it stop all containers?
You could create a batch-file (.bat or .cmd) with these commands in it:
#ECHO OFF
FOR /f "tokens=*" %%i IN ('docker ps -q') DO docker stop %%i
If you want to run this command directly in the console, replace %%i with %i, like:
FOR /f "tokens=*" %i IN ('docker ps -q') DO docker stop %i
In Git Bash or Bash for Windows you can use this Linux command:
docker stop $(docker ps -q)
Note: this will fail if there are no containers running
For PowerShell, the command is very similar to the Linux one:
docker ps -q | % { docker stop $_ }
For those who are interested this can be accomplished in Powershell using
docker ps -q | % { docker stop $_ }
In PowerShell, you could also use this syntax
docker container stop $(docker container list -q)
If the motivation of the question is to recover the memory occupied by Docker (in my case, this was why I arrived at this page), I found that the only way was to stop Docker Desktop completely. You do that by right-clicking the whale icon in the notification area (bottom right) > Quit Docker Desktop.
When you restart Docker Desktop, all the containers reappear, and Docker even sets them to up again automatically.
My two cents.
If you want to stop them filtered by some criteria
docker ps -a -q --filter "name=container_name" --format="{{.ID}}" | ForEach-Object -Process {docker stop $_ }
or if you want to stop and remove them all together
docker ps -a -q --filter "name=container_name" --format="{{.ID}}" | ForEach-Object -Process {docker rm $_ -f}
By using pipe and foreach I avoid the error returned when there are no containers of this kind on the specific machine because docker stop or docker rm require at least one argument.
This script is used with combination of
docker container run image_tag --name=container_name
in order to use the filter later on when you want to stop and remove the containers.
docker stop $(docker ps -aq)
to stop all running containers.
docker rm $(docker ps -aq)
to delete all containers.

How do I delete all running Docker containers?

I remember using
docker rm -f `docker ps -aq`
to chain the commands without an issue a few months ago, but now this isn't working, and I'm getting the following output:
unknown shorthand flag: 'a' in -aq`
See 'docker rm --help'.
What changed? How can I delete all Docker running containers in one line? In case it helps, I'm using Docker for Windows (native with Hyper-V, not with VirtualBox) on Windows 10, and the command I used has worked fine with my previous Windows 8 Docker toolbox installation.
Till now (Docker version 1.12) we are using the following command to delete all the running containers (also, if we want to delete the volumes, we can do that manually using its respective tag -v in the following command),
Delete all Exited Containers
docker rm $(docker ps -q -f status=exited)
Delete all Stopped Containers
docker rm $(docker ps -a -q)
Delete All Running and Stopped Containers
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
Remove all containers, without any criteria
docker container rm $(docker container ps -aq)
But, in version 1.13 and above, for complete system and cleanup, we can directly user the following command,
docker system prune
All unused containers, images, networks and volumes will get deleted. Also, individually i.e. separately, we can do that using the following commands, that clean up the components,
docker container prune
docker image prune
docker network prune
docker volume prune
I had this issue when running in cmd. Switched to PowerShell and it worked!
Use:
docker rm -f $(docker ps -aq)
If anybody needs the Windows Shell Command (stop and remove container), here it is:
for /F %c in ('docker ps -a -q') do (docker stop %c)
for /F %c in ('docker ps -a -q') do (docker rm %c)
If you put it in a batch file, just add % to %c:
for /F %%c in ('docker ps -a -q') do (docker stop %%c)
I've had the same problem: I was on a Windows machine and used Docker within VirtualBox and the command docker rm -f ${docker ps -aq} worked well. Then I switched to Docker for Windows and the command didn't work on the Windows command line.
But using Cygwin or Git Bash solved the problem for me.
Try using this command.
docker rm -f $(docker ps | grep -v CONTAINER | awk '{print $1}')
$ docker rm $(docker ps --filter status=created -q)
Tested on Docker version 19.03.5, build 633a0ea on Mac OS Mojave.
Run docker commands in Windows PowerShell will execute and run most of the commands
Hope you also remember to stop running containers first before running the delete command
docker stop $(docker ps -aq)
Use this:
docker rm -f $(docker ps | grep -v CONTAINER | awk '{print $1}')
If you want to include previously stopped containers:
docker rm -f $(docker ps -a | grep -v CONTAINER | awk '{print $1}')
we can delete all running containers in docker ENV by the following the command -
docker container rm -f $(docker container ls -aq)
It should to the magic
if we have run our docker container using docker-compose.yaml then
docker-compose -f /path/to/compose/file down
should work
Single command to delete all stop and running containers (first stop and then just prune/remove them. Works for me all the time.
docker stop $(docker ps -a -q) && docker container prune -a
If the container is running, you cannot delete the image. First stop all the containers using the following command.
docker stop $(docker ps -aq)
you are saying running stop against the output of docker ps -aq.
'a' - get me all the containers
'q' - return only the container id.
Then run the following command to remove all the containers.
docker rm $(docker ps -aq)

Stopping Docker containers by image name - Ubuntu

On Ubuntu 14.04 (Trusty Tahr) I'm looking for a way to stop a running container and the only information I have is the image name that was used in the Docker run command.
Is there a command to find all the matching running containers that match that image name and stop them?
If you know the image:tag exact container version
Following issue 8959, a good start would be:
docker ps -a -q --filter="name=<containerName>"
Since name refers to the container and not the image name, you would need to use the more recent Docker 1.9 filter ancestor, mentioned in koekiebox's answer.
docker ps -a -q --filter ancestor=<image-name>
As commented below by kiril, to remove those containers:
stop returns the containers as well.
So chaining stop and rm will do the job:
docker rm $(docker stop $(docker ps -a -q --filter ancestor=<image-name> --format="{{.ID}}"))
If you know only the image name (not image:tag)
As Alex Jansen points out in the comments:
The ancestor option does not support wildcard matching.
Alex proposes a solution, but the one I managed to run, when you have multiple containers running from the same image is (in your ~/.bashrc for instance):
dsi() { docker stop $(docker ps -a | awk -v i="^$1.*" '{if($2~i){print$1}}'); }
Then I just call in my bash session (after sourcing ~/.bashrc):
dsi alpine
And any container running from alpine.*:xxx would stop.
Meaning: any image whose name is starting with alpine.
You might need to tweak the awk -v i="^$1.*" if you want ^$1.* to be more precise.
From there, of course:
drmi() { docker rm $(dsi $1 | tr '\n' ' '); }
And a drmi alpine would stop and remove any alpine:xxx container.
The previous answers did not work for me, but this did:
docker stop $(docker ps -q --filter ancestor=<image-name> )
You could start the container setting a container name:
docker run -d --name <container-name> <image-name>
The same image could be used to spin up multiple containers, so this is a good way to start a container. Then you could use this container-name to stop, attach... the container:
docker exec -it <container-name> bash
docker stop <container-name>
docker rm <container-name>
This code will stop all containers with the image centos:6. I couldn't find an easier solution for that.
docker ps | grep centos:6 | awk '{print $1}' | xargs docker stop
Or even shorter:
docker stop $(docker ps -a | grep centos:6 | awk '{print $1}')
Two ways to stop running a container:
1. $docker stop container_ID
2. $docker kill container_ID
You can get running containers using the following command:
$docker ps
Following links for more information:
https://docs.docker.com/engine/reference/commandline/stop/
https://docs.docker.com/v1.8/reference/commandline/kill/
This will only stop all containers with image = "yourImgName" :
sudo docker stop $(sudo docker ps | grep "yourImgName" | cut -d " " -f 1)
This will stop and remove all containers with image = "yourImgName" :
sudo docker rm $(sudo docker stop $(sudo docker ps -a | grep "yourImgName" | cut -d " " -f 1))
I made a /usr/local/bin/docker.stop that takes in the image name (assumes you only have one running).
docker stop $(docker ps -q -f "name=$1")
Stop docker container by image name:
imagename='mydockerimage'
docker stop $(docker ps | awk '{split($2,image,":"); print $1, image[1]}' | awk -v image=$imagename '$2 == image {print $1}')
Stop docker container by image name and tag:
imagename='mydockerimage:latest'
docker stop $(docker ps | awk -v image=$imagename '$2 == image {print $1}')
If you created the image, you can add a label to it and filter running containers by label
docker ps -q --filter "label=image=$image"
Unreliable methods
docker ps -a -q --filter ancestor=<image-name>
does not always work
docker ps -a -q --filter="name=<containerName>"
filters by container name, not image name
docker ps | grep <image-name> | awk '{print $1}'
is problematic since the image name may appear in other columns for other images
list all containers with info and ID
docker ps
docker stop CONTAINER ID
For Docker version 18.09.0
I found that format flag won't be needed
docker rm $(docker stop $(docker ps -a -q -f ancestor=<image-name>))
I was trying to wrap my Docker commands in gulp tasks and realised that you can do the following:
docker stop container-name
docker rm container-name
This might not work for scenarios where you have multiple containers with the same name (if that's possible), but for my use case it was perfect.
In my case --filter ancestor=<image-name> was not working, so the following command cleaned up the Docker container for me:
docker rm $(docker stop $(docker ps -a -q --filter "name=container_name_here" --format="{{.ID}}"))
Adding on top of #VonC superb answer, here is a ZSH function that you can add into your .zshrc file:
dockstop() {
docker rm $(docker stop $(docker ps -a -q --filter ancestor="$1" --format="{{.ID}}"))
}
Then in your command line, simply do dockstop myImageName and it will stop and remove all containers that were started from an image called myImageName.
use: docker container stop $(docker container ls -q --filter ancestor=mongo)
(base) :~ user$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d394144acf3a mongo "docker-entrypoint.s…" 15 seconds ago Up 14 seconds 0.0.0.0:27017->27017/tcp magical_nobel
(base) :~ user$ docker container stop $(docker container ls -q --filter ancestor=mongo)
d394144acf3a
(base) :~ user$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
(base) :~ user$
This is my script to rebuild docker container, stop and start it again
docker pull [registry]/[image]:latest
docker build --no-cache -t [localregistry]/[localimagename]:latest -f compose.yaml context/
docker ps --no-trunc | grep [localimagename] | awk '{print $1}' | xargs docker stop
docker run -d -p 8111:80 [localregistry]/[localimagename]:latest
note --no-trunc argument which shows the image name or other info in full lenght in the output
Here's a concise command which doesn't require you to specify the image tag (as most of these answers do):
docker stop $(docker ps -a | awk -v i="^${image_name}.*" '{if($2~i){print$1}}')
docker stop $(docker ps -a | grep "zalenium")
docker rm $(docker ps -a | grep "zalenium")
This should be enough.
If you want to prefer a simple AWK approach, here Is my take:
docker rm -f $(docker ps | awk '{ if($2 == "<your image name>") { print $NF}}')
$(docker ps | awk '{ if($2 == "<your image name>") { print $NF}}') - prints the docker container names based on input image
docker ps - list all containers
awk '{ if($2 == "<your-image-name>") { print $NF}}' - The second parsed column of docker ps gives the image name. Comparing it with your image name will execute print $NF which prints the container name.
docker rm -f removes the containers
For example, removing all running containers of ubuntu image, can be done simply as:
docker rm -f $(docker ps | awk '{ if($2 == "ubuntu:latest") { print $NF}}')
PS: Remember to include the image tag in AWK, since it's a equal comparator.
if you know a part of the container name you can use AWK with docker as following :
CONTAINER_IDS=$(docker ps -a | awk '($2 ~ /container.*/) {print $1}')
if [ -z "$CONTAINER_IDS" -o "$CONTAINER_IDS" == " " ]; then
echo "No containers available for deletion"
else
docker rm -f $CONTAINER_IDS
fi
image: docker
services:
- docker:dind
stages:
- deploy
step-deploy-prod:
stage: deploy
tags:
- docker
script:
- container_id=$(docker ps -q -a -f status=running --filter name=todoapp)
- if [ -n "$container_id" ]; then
docker stop $container_id;
docker rm -f $container_id;
fi
- container_id=$(docker ps -q -a -f status=exited --filter name=todoapp)
- if [ -n "$container_id" ]; then
docker rm -f $container_id;
fi
- docker build -t app/creative .
- docker run -d -p 8081:80 --name todoapp app/creative
First, check for a running container with the command docker ps -q -a -f status=running --filter name=todoapp , if it finds one it stops and deletes the running container then check for any containers that are stopped and have the name todoapp using the command docker ps -q -a -f status=exited --filter name=todoapp, then it will remove the container if it's found.
Then it will build a new image and start a new container with the new build image.
As I have found out, if you stop the container, it can't be found with docker rm just incase anyone stumbles across this if you are wanting to replace a newly deployed image via gitlab-ci
There is an option in docker ps command -f status=exited which shows all the containers which are in stopped state.
container_id=$(docker ps -q -a -f status=exited --filter name=todoapp)
This command would only return container ids that are stopped and has name todoapp
Also, a better way to remove the stopped container is by using the -f or --force option with the docker rm command. This option will remove the container even if it is in a stopped state.
You can use the ps command to take a look at the running containers:
docker ps -a
From there you should see the name of your container along with the container ID that you're looking for. Here's more information about docker ps.

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

how do I clean up my docker host machine

As I create/debug a docker image/container docker seems to be leaving all sorts of artifacts on my system. (at one point there was a 48 image limit) But the last time I looked there were 20-25 images; docker images.
So the overarching questions are:
how does one properly cleanup?
as I was manually deleting images more started to arrive. huh?
how much disk space should I really allocate to the host?
will running daemons really restart after the next reboot?
and the meta question... what questions have I not asked that need to be?
Here's how I periodically purge my docker host:
Kill running containers:
docker kill $(docker ps -qa)
Delete all containers (and their associated volumes):
docker rm -v $(docker ps -qa)
Remove all images:
docker rmi $(docker images -q)
Update
Delete only the containers that are not running. Parse the "ps" output for the "Exited" string:
docker ps -a | awk '/Exited/ {print $1}' | xargs docker rm -v
Not perfect... Don't give your container the name "Exited" :-)
Update
Docker 1.9 has a new volume command that can be used to purge old volumes
docker volume rm $(docker volume ls -qf dangling=true)
Update
Latest community edition of docker has a new "system prune" command
docker system prune --volumes
This purged unused networks, kill stopped containers, dangling images and any unused volumes.
It can also be helpful to remove "dangling" images
docker rmi $(docker images -f "dangling=true" -q)
I would also like to contribute to this with some commands that were added to version 1.13.0:
$ docker system prune
$ docker container prune
$ docker image prune
$ docker volume prune
$ docker network prune
see changelog: 1.13.0 (2017-01-18)
Add new docker system command with df and prune subcommands for system resource management, as well as docker {container,image,volume,network} prune subcommands #26108 #27525 / #27525
I'm using docker-machine with VirtualBox and after deleting all containers and all images, the docker VirtualBox image is still consuming many gigabytes of disk space.
To also clean up the disk space, it helps to delete and re-create the docker machine. E.g.:
docker-machine rm default
docker-machine create --driver virtualbox default
Update:
There are built-in filters in the docker cli that let one properly display containers that meet certain criteria. These bash functions are taken from one of the core maintainers' dotfiles:
dcleanup(){
docker rm -v $(docker ps --filter status=exited -q 2>/dev/null) 2>/dev/null
docker rmi $(docker images --filter dangling=true -q 2>/dev/null) 2>/dev/null
}
del_stopped(){
local name=$1
local state=$(docker inspect --format "{{.State.Running}}" $name 2>/dev/null)
if [[ "$state" == "false" ]]; then
docker rm $name
fi
}
Original Answer
A helper script I've created for my own use:
#!/bin/bash
# options:
# remove stopped containers and untagged images
# $ dkcleanup
# remove all stopped|running containers and untagged images
# $ dkcleanup --reset
# remove containers|images|tags matching {repository|image|repository\image|tag|image:tag}
# pattern and untagged images
# $ dkcleanup --purge {image}
# everything
# $ dkcleanup --nuclear
if [ "$1" == "--reset" ]; then
# Remove all containers regardless of state
docker rm -f $(docker ps -a -q) 2>/dev/null || echo "No more containers to remove."
elif [ "$1" == "--purge" ]; then
# Attempt to remove running containers that are using the images we're trying to purge first.
(docker rm -f $(docker ps -a | grep "$2/\|/$2 \| $2 \|:$2\|$2-\|$2:\|$2_" | awk '{print $1}') 2>/dev/null || echo "No containers using the \"$2\" image, continuing purge.") &&\
# Remove all images matching arg given after "--purge"
docker rmi $(docker images | grep "$2/\|/$2 \| $2 \|$2 \|$2-\|$2_" | awk '{print $3}') 2>/dev/null || echo "No images matching \"$2\" to purge."
else
# This alternate only removes "stopped" containers
docker rm -f $(docker ps -a | grep "Exited" | awk '{print $1}') 2>/dev/null || echo "No stopped containers to remove."
fi
if [ "$1" == "--nuclear" ]; then
docker rm -f $(docker ps -a -q) 2>/dev/null || echo "No more containers to remove."
docker rmi $(docker images -q) 2>/dev/null || echo "No more images to remove."
else
# Always remove untagged images
docker rmi $(docker images | grep "<none>" | awk '{print $3}') 2>/dev/null || echo "No untagged images to delete."
fi
exit 0
source
To your questions:
how does one properly cleanup?
no official way yet, just helper scripts and functions like the above.
as I was manually deleting images more started to arrive. huh?
you might have been deleting images that were built on top of others that became "untagged" when you tried to delete them.
how much disk space should I really allocate to the host?
depends on the types of images you plan to use. Know that running a 500 mb image multiple times doesn't use (500mb X number of containers) space. The containers reuse the same image and just add whatever they change when running on top. So think from an image storing perspective, not a container runtime one regarding storage.
will running daemons really restart after the next reboot?
By default, they are stopped when the host reboots. You need to run with docker run --restart=True to automatically start up again when the host reboots.
Sometimes you wont have Status, it'll just be blank.
here is my version:
docker rm -f $(docker ps -a | env -i grep -v Up | tail -n+2 | cut -d ' ' -f 1)

Resources