Remove docker images and containers that are taking disk space - docker

I've been using Docker for a while... I usually the command "docker run" to run containers. Today, I had the problem of saving data to disk on a database:
No space left on device
According to various documentation online, the solution is to use the following command:
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
However, it fails in a lot of entries... Is there a proper way to clean up containers and images?

The latest version of Docker supports the following command:
$ docker rmi $(docker images -q --filter "dangling=true")
The command will properly remove the images... This is described at https://www.calazan.com/docker-cleanup-commands/, and some comments from http://jimhoskins.com/2013/07/27/remove-untagged-docker-images.html.

Related

Docker rm unable to remove all containers

I am trying to remove all stopped containers to free up some space on an AWS Ubuntu server that I am using. Docker documentation says to use docker rm $(docker ps -a -q) : https://docs.docker.com/engine/reference/commandline/rm/#remove-all-stopped-containers
However, I am getting the error below:
"docker rm" requires at least 1 argument.
See 'docker rm --help'.
Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
Any suggestions?
Adding sudo in front doesn't help. I am able to remove individual containers using docker rm 343e43ac4e86, but I don't want to spend a lot of time trying to figure out which containers are from older releases and removing them one by one.
I think you could also try docker ps -aq | xargs docker rm if the substitution doesn't work out.
Please use these command in your terminal:
docker stop $(docker ps -aq)
docker rm $(docker ps -aq)
docker network prune -f
docker rmi -f $(docker images --filter dangling=true -qa)
docker volume rm $(docker volume ls --filter dangling=true -q)

docker still builds from cache even after system produce

I run the following commands
docker system prune -a -f
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
docker rmi $(docker images -q)
Then I do
docker build .
It still not picking up my code changes.
When I run the built container it still showing me old code.
Where is the docker stores the build cache?

Deletion of all my local docker images and containers is not working

I am unable to delete all my local docker containers or images and below is the error I am getting. Can you please suggest what am I doing wrong ?
docker rmi $(docker ps -a -q)
unknown shorthand flag: 'a' in -a
See 'docker rmi --help'.<\br>
docker ps is pulling a list of the containers you have, not the images, and rmi is used for removing images, not containers.
Use docker rm $(docker ps -a -q)
If you want to clean up your docker space, check out the prune command, for example:
docker system prune
docker image prune
docker container prune

Docker Compose Could not open file build: No such file or directory

After moving my docker project on my disc I can no longer run it. Whenever I try to run docker-compose build or docker-compose up I get an error
Could not open file build: No such file or directory
I've tried purging all docker resources but am still receiving this error.
docker kill $(docker ps -q)
docker rm $(docker ps -a -q)
docker rmi $(docker images -q -f dangling=true)
docker rmi $(docker images -q)
How can I either purge all docker data entirely, or fix the issue after moving my project?
My docker-compose.yml is unedited from this project's dc file.
You can prune all docker data on the system by using the command docker system prune.

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