qkrijger
qkrijger

Reputation: 27256

How to remove old Docker containers

This question is related to Should I be concerned about excess, non-running, Docker containers?.

I'm wondering how to remove old containers. The docker rm 3e552code34a lets you remove a single one, but I have lots already. docker rm --help doesn't give a selection option (like all, or by image name).

Maybe there is a directory in which these containers are stored where I can delete them easily manually?

Upvotes: 1493

Views: 942602

Answers (30)

VonC
VonC

Reputation: 1328002

With Docker 1.13 (Q4 2016), you now have:

docker system prune -a will delete ALL unused data (i.e., in order: containers stopped, volumes without containers and images with no containers).

docker system prune without -a will remove (for images) only dangling images, or images without a tag, as commented by smilebomb.

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 -a

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

As wjv comments,

There is also docker {container,image,volume,network} prune, which may be used to remove unused instances of just one type of object.

Introduced in commit 913e5cb, only for Docker 1.13+.

docker container prune

2023: Taiger points out in the comments:

The correct answer for modern versions of Docker: "Prune unused Docker objects".

Upvotes: 140

Alexei Volkov
Alexei Volkov

Reputation: 859

I have faced with this issue in a CI pipeline tasks and managed this with own solution. It is a self contained binary written in go and packages in a docker container as well.

For example simple usage for self hosted gitlab runner

# /etc/gitlab-runner/config.toml
# ...
[[runners]]
# ...
pre_build_script = "docker run -v /var/run/docker.sock:/var/run/docker.sock  --rm softkot/docker-cleanup --name=whatever --since 5m"

Have a look at public available GitLab sources and Docker Hub Container

Upvotes: 0

Cristian Florescu
Cristian Florescu

Reputation: 1816

A great auto alternative - containrrr/watchtower

It contains images auto cleanup, update and monitoring options

version: "3.9"
services:
  service:
    container_name: watchtower
    image: containrrr/watchtower:latest
    hostname: watchtower
    environment:
      - TZ=Europe/Bucharest
      - WATCHTOWER_CLEANUP=true
      - WATCHTOWER_INCLUDE_RESTARTING=true
      - WATCHTOWER_POLL_INTERVAL=86400
      #- WATCHTOWER_SCHEDULE=0 0 4 * * *
      - WATCHTOWER_LABEL_ENABLE=false
      - WATCHTOWER_MONITOR_ONLY=false
      - WATCHTOWER_NO_PULL=false
      - WATCHTOWER_NO_RESTART=false
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    restart: unless-stopped

Upvotes: 0

Md. Shahariar Hossen
Md. Shahariar Hossen

Reputation: 1715

See all the existing images:

docker images -a

See all the existing containers:

docker ps -a

Delete single image:

docker images -a
docker rmi <IMAGE_ID>

Stop single container:

docker ps -a
docker stop <CONTAINER_ID>

Stop multiple containers:

docker ps -a
docker stop <CONTAINER_ID1> <CONTAINER_ID2>

Delete single container:

docker ps -a
docker rm <CONTAINER_ID>

Delete multiple images:

docker images -a
docker rmi <IMAGE_ID1> <IMAGE_ID2>

Delete multiple stopped containers:

docker ps -a
docker rm <CONTAINER_ID1> <CONTAINER_ID2>

Delete images only in a single command:

docker rmi -f $(docker images -a -q)

Delete both containers and images in a single command:

docker rm $(docker ps -a -q) && docker rmi -f $(docker images -a -q)

To prune all containers:

docker container prune

Upvotes: 11

Aditya Bhuyan
Aditya Bhuyan

Reputation: 458

Use the below command to know the Container ID

docker ps

Then use the below command to stop it.

docker kill <container-id>

Upvotes: 0

Snehal Rajput
Snehal Rajput

Reputation: 436

To remove all the dockers, : docker system prune -a

Upvotes: -2

vesako
vesako

Reputation: 1956

If you do not like to remove all containers, you can select all containers created before or after a specific container with docker ps -f before=<container-ID> or with docker ps -f since=<container-ID>

Let's say you have developed your system, and now it is working, but there are a number of containers left. You want to remove containers created before that working version. Determine the ID of the working container with docker ps.

Remove containers created before an other container

docker rm $(docker ps -f before=9c49c11c8d21 -q)

Another example. You have your database already running on a Docker container. You have developed your application to run on another container and now you have a number of unneeded containers.

Remove containers created after a certain container

docker rm $(docker ps -f since=a6ca4661ec7f -q)

Docker stores containers in /var/lib/docker/containers in Ubuntu. I think extra containers do no other harm, but take up disk space.

Upvotes: 38

levi
levi

Reputation: 22697

UPDATED 2021 (NEWEST)

docker container prune

This - 2017 (OLD) way

To remove ALL STOPPED CONTAINERS

docker rm $(docker ps -a -q)

To remove ALL CONTAINERS (STOPPED AND NON STOPPED)

docker rm  -f $(docker ps -a -q)

Upvotes: 131

edencorbin
edencorbin

Reputation: 2949

I wanted to add this simple answer as I didn't see it, and the question is specifically "old" not "all".

sudo docker container prune --filter "until=24h"

Adjust the 24h for whatever time span you want to remove containers that are older than.

Upvotes: 7

Pal
Pal

Reputation: 990

Try this command to clean containers and dangling images.

docker system prune -a

Upvotes: 4

LeYAUable
LeYAUable

Reputation: 1752

On windows (powershell) you could do something like this:

  1. $containers = docker ps -a -q
  2. foreach ($container in $containers) {docker rm $container -f }

Upvotes: 0

Raghav
Raghav

Reputation: 9628

For Windows you can run this to stop and delete all containers:

@echo off
FOR /f "tokens=*" %%i IN ('docker ps -q') DO docker stop %%i
FOR /f "tokens=*" %%i IN ('docker ps -aq') DO docker rm %%i
FOR /f "tokens=*" %%i IN ('docker images --format "{{.ID}}"') DO docker rmi %%i

You can save it as a .bat file and can run to do that:

enter image description here

Upvotes: 1

pickup limes
pickup limes

Reputation: 445

To remove ALL stopped docker containers, run:-

$ docker container prune

You'll have to be careful with this command since it removes all stopped containers indiscriminately so make sure there's no stopped/currently unused container that you may still have use of.

A more precise alternative is to remove the container by ID. You'll have to first list the stopped containers using this command:-

docker ps --filter "status=exited"

You'll then copy the ID of the container you want to remove. Thereafter, execute the following command that removes a single container,

docker container rm <container ID>

or the one below to remove multiple containers at once by running:-

docker container rm <container ID 1> <container ID 2> <container ID n>

Upvotes: 3

Vivek Anand
Vivek Anand

Reputation: 1384

If you want to remove all containers then use

docker container prune

This command will remove all containers

Why not clean whole docker system with

docker system prune

Upvotes: 1

Felipe
Felipe

Reputation: 17181

To remove ALL containers:

sudo docker ps -a | grep -v CONTAINER | awk '{print $1}' | xargs --no-run-if-empty sudo docker rm -f

Explanation:

sudo docker ps -a

Returns a list of containers.

awk '{print $1}'

Gets the first column which is the container ID.

grep -v CONTAINER

Remove the title.

The last pipe sends the IDs to sudo docker rm -f safely.

Upvotes: 2

Mostafa Ghadimi
Mostafa Ghadimi

Reputation: 6766

You can remove the containers using multiple ways that I will explain them in the rest of the answer.

  1. docker container prune. This command removes the all of the containers that are not working right now. You can find out which containers are not working by comparing the output of docker ps and docker ps -a. The containers that are listed in docker ps -a and not exist in docker ps are not working right now, but their containers aren't removed.

  2. docker kill $(docker ps -aq). What this command does is that by executing $(docker ps -aq) it returns the list of ids of all containers and kill them. Sometime this command doesn't work because it is being using by the running container. To make that work, you can use --force option.

  3. docker rm $(docker ps -aq). It has the same definition as the second command. The only difference of them is that it removes the container (as same as docker prune), while the docker kill doesn't.

  4. Sometimes it is needed to remove the image, because you have changed the configuration of the Dockerfile and need to remove it to rebuild it. For this purpose you can see all of the images by running docker images and then copy the ID of the image that you want to remove. It can be deleted simply by executing docker image rm <image-id>.

PS: You can use docker ps -a -q instead of docker ps -aq and there is no differences. Because in unix-based operating system, you can join the options like the above example.

Upvotes: 1

Karan Khanna
Karan Khanna

Reputation: 2147

To get rid of your stopped container you can use:

docker rm <cointainer_id>

You can also use the name of the container:

docker rm <name>

If you want to get rid of all the stopped containers you can use the:

docker container prune

Or you can also use:

docker rm $(docker ps -aq -f status=exited)

You can use -v argument to delete any docker managed volumes that are not referenced any further

docker rm -v $(docker ps -aq -f status=exited)

You can also use --rm with the docker run. This will delete the container and the associated files when the container exists.

Upvotes: 1

Vikram Biwal
Vikram Biwal

Reputation: 2826

List all containers (only IDs)

docker ps -aq

Stop all running containers

docker stop $(docker ps -aq)

Remove all containers

docker rm $(docker ps -aq)

Remove all images

docker rmi $(docker images -q)

Upvotes: 1

Ken Cochrane
Ken Cochrane

Reputation: 77395

Since Docker 1.13.x you can use Docker container prune:

docker container prune

This will remove all stopped containers and should work on all platforms the same way.

There is also a Docker system prune:

docker system prune

which will clean up all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes, in one command.


For older Docker versions, you can string Docker commands together with other Unix commands to get what you need. Here is an example on how to clean up old containers that are weeks old:

$ docker ps --filter "status=exited" | grep 'weeks ago' | awk '{print $1}' | xargs --no-run-if-empty docker rm

To give credit, where it is due, this example is from https://twitter.com/jpetazzo/status/347431091415703552.

Upvotes: 1833

SAB
SAB

Reputation: 315

Here is a script to remove both running and exited containers created longer than 2 days:

#!/bin/bash
# This script will kill and remove containers older than 2 days.
#
docker ps -aq > /scripts/containers.txt
today=`date +%Y-%m-%d`
oldate=`date --date="2 day ago" +%Y-%m-%d`
while read p; do
    cont=`docker inspect -f '{{ .Created }}' $p | cut -c 1-10`
    echo " Created date of $p is $cont"
    k=`echo $(( ( $(date -ud $today +'%s') - $(date -ud $cont +'%s'))/60/60/24 ))`
    echo $k
    if [ $k -ge 2 ];
    then
            echo "Killing docker container $p"
            docker kill $p
            echo "Removing docker container $p"
            docker rm $p
    else
            echo "Docker container $p is not one day old, so keeping the container."
    fi
done </scripts/containers.txt

Upvotes: 2

Sijo M Cyril
Sijo M Cyril

Reputation: 780

I am using following commands to delete Exited and Restarting docker containers

docker stop --force $(docker ps -a|grep Exited| awk '{print $1}')
docker rm --force $(docker ps -a|grep Exited| awk '{print $1}')
docker stop --force $(docker ps -a|grep Restarting| awk '{print $1}')
docker rm --force $(docker ps -a|grep Restarting| awk '{print $1}')

Using below command to remove images named as none

docker image rm --force $(docker image ls  |grep none |awk '{print $3}')

Upvotes: 3

Anish Varghese
Anish Varghese

Reputation: 3615

Use the docker management tool Portainer

We can manage all the old containers, non using volumes and images by using this tool

Its a simple management UI for dockers

HOW TO DEPLOY PORTAINER

Use the following Docker commands to deploy Portainer:

$ docker volume create portainer_data
$ docker run -d -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer

You'll just need to access the port 9000 of the Docker engine where portainer is running using your browser.

Note: the -v /var/run/docker.sock:/var/run/docker.sock option can be used in Linux environments only.

Upvotes: 1

giapnh
giapnh

Reputation: 3278

To delete a specify container

docker container rm container_id

If the container is running, you have to stop it before to delete it

docker container stop container_id

And this command is to delete all existing containers

docker container rm $(docker container -a -q)

Upvotes: 1

Siena
Siena

Reputation: 777

I am suggesting you to stop the images first and then remove.

You could go like:

$ docker stop $(docker ps -a)
$ docker rm $(docker ps -a)

Upvotes: -2

Rahul Bagad
Rahul Bagad

Reputation: 123

Use the following nested commands:

$ sudo docker stop $(sudo docker ps -a -q)

This command stops all running containers.

$ sudo docker rm $(sudo docker ps -a -q)

This command remove all containers.

Upvotes: 2

Pinaki Mukherjee
Pinaki Mukherjee

Reputation: 1656

I found the below command is very handy to stop and remove all the containers. You don't need to stop explicitly with another command if you are using the -f (force) flag as it stops all running containers:

docker rm -f $(docker ps -a -q)

Upvotes: 2

Jitendra Bhalothia
Jitendra Bhalothia

Reputation: 407

This process contains two steps (stop and remove):

  1. Stop the container that is being used by any running microservices

    docker stop $(docker ps -a -q)
    
  2. Remove all the containers

    docker rm $(docker ps -a -q)
    
  3. Remove single container

    docker rm container-ID
    

Upvotes: 2

Murali Manchadikkal
Murali Manchadikkal

Reputation: 1147

The basic steps to stop/remove all containers and images

  1. List all the containers

    docker ps -aq

  2. Stop all running containers

    docker stop $(docker ps -aq)

  3. Remove all containers

    docker rm $(docker ps -aq)

  4. Remove all images

    docker rmi $(docker images -q)

Note: First you have to stop all the running containers before you remove them. Also before removing an image, you have to stop and remove its dependent container(s).

Upvotes: 3

Harshal Vaidya
Harshal Vaidya

Reputation: 179

Removing older Docker containers is very easy.

List all the containers:

docker ps -a
docker ps (To list the running containers)

Once you hit docker ps -a, it will give you list of containers along with the container id (which is unique and combination of a-z, A-Z and 0-9). Copy the container id you wanted to remove and simply hit the below.

docker rm container_id

Your container will be removed along with the layers created in /var/lib/docker/aufs (if you are using Ubuntu).

Upvotes: 1

Jagatveer Singh
Jagatveer Singh

Reputation: 195

docker rm -f $(docker ps -a -q) will do the trick. It will stop the containers and remove them too.

Upvotes: 1

Related Questions