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:21

Update: As of Docker version 1.13 (released January 2017), you can issue the following command to clean up stopped containers, unused volumes, dangling images and unused networks:

docker system prune

If you want to insure that you're only deleting containers which have an exited status, use this:

docker ps -aq -f status=exited | xargs docker rm

Similarly, if you're cleaning up docker stuff, you can get rid of untagged, unnamed images in this way:

docker images -q --no-trunc -f dangling=true | xargs docker rmi
查看更多
劫难
3楼-- · 2019-01-03 19:21
  1. Remove all docker processes:

    docker rm $(docker ps -a -q)
    
  2. Remove specific container:

    $ docker ps -a (lists all old containers)
    
    $ docker rm container-Id
    
查看更多
冷血范
4楼-- · 2019-01-03 19:22

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
查看更多
【Aperson】
5楼-- · 2019-01-03 19:22

Remove 5 oldest containers:

docker rm `docker ps -aq | tail -n 5`

See how many containers there are left:

docker ps -aq | wc -l
查看更多
相关推荐>>
6楼-- · 2019-01-03 19:24

So, personally I recommend doing this as part of your deploy script for both images and containers, keeping only the most recent n containers and images. I tag my Docker images with the same versioning schema I use with git tag as well as always tagging the latest Docker image with "latest." This means that without cleaning up anything, my Docker images wind up looking like:

REPOSITORY              TAG       IMAGE ID        CREATED         VIRTUAL SIZE
some_repo/some_image    0.0.5     8f1a7c7ba93c    23 hours ago    925.4 MB
some_repo/some_image    latest    8f1a7c7ba93c    23 hours ago    925.4 MB
some_repo/some_image    0.0.4     0beabfa514ea    45 hours ago    925.4 MB
some_repo/some_image    0.0.3     54302cd10bf2    6 days ago      978.5 MB
some_repo/some_image    0.0.2     0078b30f3d9a    7 days ago      978.5 MB
some_repo/some_image    0.0.1     sdfgdf0f3d9a    8 days ago      938.5 MB

Now, of course I don't want to keep all my images (or containers) going back to perpetuity on all my production boxes. I just want the last 3 or 4 for rollbacks and to get rid of everything else. Unix's tail is your best friend here. Since docker images and docker ps both order by date, we can just use tail to select all but the top three and remove them:

docker rmi $(docker images -q | tail -n +4)

Run that along with your deploy scripts (or locally) to always keep just enough images to comfortably roll back without taking up too much room or cluttering stuff up with old images.

Personally, I only keep one container on my production box at any time, but you can do the same sort of thing with containers if you want more:

docker rm $(docker ps -aq | tail -n +4)

Finally, in my simplified example we're only dealing with one repository at a time, but if you had more, you can just get a bit more sophisticated with the same idea. Say I just want to keep the last three images from some_repo/some_image. I can just mix in grep and awk and be on my way:

docker rmi $(docker images -a | grep 'some_repo/some_image' | awk '{print $3}' | tail -n +4)

Again, the same idea applies to containers, but you get it by this point so I'll stop giving examples.

查看更多
相关推荐>>
7楼-- · 2019-01-03 19:25

Another method, which I got from Guillaume J. Charmes (credit where it is due):

docker rm `docker ps --no-trunc -aq`

will remove all containers in an elegant way.

And by Bartosz Bilicki, for Windows:

FOR /f "tokens=*" %i IN ('docker ps -a -q') DO docker rm %i

For PowerShell:

docker rm @(docker ps -aq)

An update with Docker 1.13 (Q4 2016), credit to VonC (later in this thread):

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
查看更多
登录 后发表回答