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条回答
The star\"
2楼-- · 2019-01-03 19:26
docker rm --force `docker ps -qa`
查看更多
Viruses.
3楼-- · 2019-01-03 19:27

https://github.com/HardySimpson/docker-cleanup

Docker cleanup

A tiny all-in-one shell, which removes:

  • Containers that not running more than one day ago
  • Images that don't belong to any remaining container

Intend to run as a crontab job

Feature

  • It will remove all <none>:<none> images
  • If the image has multiple repo:tag references to it, it will remove all repo:tag except with running a container. Actually it is a nature of "docker rmi".
  • Many error message will be show on screen, and you can decide to 2>/dev/null or not
  • Learn something from docker-gc, and fix its problem (it can not remove image that has mutliple repo:tag)
查看更多
smile是对你的礼貌
4楼-- · 2019-01-03 19:29

First, stop running containers before attempting to remove them

Remove running containers

docker rm $(docker stop -t=1 $(docker ps -q))

You could use kill instead of stop. In my case I prefer stop since I tend to rerun them vs. creating a new one every time so I try to shut them down nicely.

Note: Trying to stop a container will give you an error:

Error: Impossible to remove a running container, please stop it first

Remove all containers

docker rm $(docker ps -a -q)
查看更多
对你真心纯属浪费
5楼-- · 2019-01-03 19:30

For anyone interested, I took the example from qkrijger and turned it into a clear all (stop and remove all)

docker stop `docker ps --no-trunc -aq` ; docker rm `docker ps --no-trunc -aq`
查看更多
可以哭但决不认输i
6楼-- · 2019-01-03 19:31

UPDATED 2017 (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)
查看更多
乱世女痞
7楼-- · 2019-01-03 19:31

Here is my docker-cleanup script, which removes untagged containers and images. Please check the source for any updates.

#!/bin/sh
# Cleanup docker files: untagged containers and images.
#
# Use `docker-cleanup -n` for a dry run to see what would be deleted.

untagged_containers() {
  # Print containers using untagged images: $1 is used with awk's print: 0=line, 1=column 1.
  docker ps -a | awk '$2 ~ "[0-9a-f]{12}" {print $'$1'}'
}

untagged_images() {
  # Print untagged images: $1 is used with awk's print: 0=line, 3=column 3.
  # NOTE: intermediate images (via -a) seem to only cause
  # "Error: Conflict, foobarid wasn't deleted" messages.
  # Might be useful sometimes when Docker messed things up?!
  # docker images -a | awk '$1 == "<none>" {print $'$1'}'
  docker images | tail -n +2 | awk '$1 == "<none>" {print $'$1'}'
}

# Dry-run.
if [ "$1" = "-n" ]; then
  echo "=== Containers with uncommitted images: ==="
  untagged_containers 0
  echo

  echo "=== Uncommitted images: ==="
  untagged_images 0

  exit
fi

# Remove containers with untagged images.
echo "Removing containers:" >&2
untagged_containers 1 | xargs --no-run-if-empty docker rm --volumes=true

# Remove untagged images
echo "Removing images:" >&2
untagged_images 3 | xargs --no-run-if-empty docker rmi

Source: https://github.com/blueyed/dotfiles/blob/master/usr/bin/docker-cleanup

查看更多
登录 后发表回答