How to remove old Docker containers

2019-01-03 19:18发布

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?

标签: docker
30条回答
对你真心纯属浪费
2楼-- · 2019-01-03 19:38

You can use the following command to remove the exited containers:

docker rm $(sudo docker ps -a | grep Exit | cut -d ' ' -f 1)

Here is the full gist to also remove the old images on docker: Gist to remove old Docker containers and images.

查看更多
时光不老,我们不散
3楼-- · 2019-01-03 19:39

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).

查看更多
迷人小祖宗
4楼-- · 2019-01-03 19:40

Updated Answer Use docker system prune or docker container prune now. See VonC's updated answer.

Previous Answer Composing several different hints above, the most elegant way to remove all non-running containers seems to be:

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

  • -q prints just the container ids (without column headers)
  • -f allows you to filter your list of printed containers (in this case we are filtering to only show exited containers)
查看更多
贪生不怕死
5楼-- · 2019-01-03 19:40

Remove all stopped containers.

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

This will remove all stopped containers by getting a list of all containers with docker ps -a -q and passing their ids to docker rm. This should not remove any running containers, and it will tell you it can’t remove a running image.

Remove all untagged images

Now you want to clean up old images to save some space.

sudo docker rmi $(sudo docker images -q --filter "dangling=true")

查看更多
地球回转人心会变
6楼-- · 2019-01-03 19:41

It is now possible to use filtering with docker ps:

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

And for images:

docker rmi $(docker images -q -f "dangling=true")

However, any of those will cause docker rm or docker rmi to throw an error when there are no matching containers. The older docker rm $(docker ps -aq) trick was even worse as it tried to remove any running container, failing at each one.

Here's a cleaner script to add in your ~/.bashrc or ~/.profile :

# Use `docker-cleanup --dry-run` to see what would be deleted.

function docker-cleanup {
  EXITED=$(docker ps -q -f status=exited)
  DANGLING=$(docker images -q -f "dangling=true")

  if [ "$1" == "--dry-run" ]; then
    echo "==> Would stop containers:"
    echo $EXITED
    echo "==> And images:"
    echo $DANGLING
  else
    if [ -n "$EXITED" ]; then
      docker rm $EXITED
    else
      echo "No containers to remove."
    fi
    if [ -n "$DANGLING" ]; then
      docker rmi $DANGLING
    else
      echo "No images to remove."
    fi
  fi
}

Edit: As noted below, original answer was for removing images, not containers. Updated to answer both, including new links to documentation. Thanks to Adrian (and Ryan's answer) for mentioning the new ps filtering.

查看更多
对你真心纯属浪费
7楼-- · 2019-01-03 19:42

Try this command to clean containers and dangling images.

docker system prune -f
查看更多
登录 后发表回答