10

I have deleted all the images/containers

ubuntu@ubuntu:/var/lib/docker$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu@ubuntu:/var/lib/docker$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

but I notice that there are still about 15GB inside /var/lib/docker

ubuntu@ubuntu:/var/lib/docker$ sudo du --max-depth=1 -h .
12G     ./volumes
104K    ./aufs
4,0K    ./containers
1,3M    ./image
4,0K    ./trust
4,0K    ./swarm
2,6G    ./tmp
108K    ./network
15G     .

Questions:

  1. How can I free up this space?

  2. Is it safe to remove things inside /var/lib/docker?

tgogos
  • 23,218
  • 20
  • 96
  • 128

3 Answers3

14

Try (from docker 1.13):

docker system df

it shows you size of:

  • Images
  • Containers
  • Local Volumes

and remove local volumes using:

docker volume prune

For older Dockers try:

docker volume rm $(docker volume ls -q)
itiic
  • 3,284
  • 4
  • 20
  • 31
  • Thanks, I will check them as soon as I update my docker version – tgogos Mar 28 '17 at 11:13
  • Yeah, docker version here is important. But I see you've found resolution for your issue. – itiic Mar 28 '17 at 11:22
  • 2
    It would be great to update the answer and mention the minimum Docker versions required to run each of these commands. I have Docker 1.12.1 and none of them work (`system` does not exist, `volume` doesn't have the `prune` subcommand`). – axiac Mar 28 '17 at 11:25
  • Any clue about the `/var/lib/docker/tmp` folder? I found this: [Can I clean /var/lib/docker/tmp?](http://stackoverflow.com/questions/31489802/can-i-clean-var-lib-docker-tmp) but it is not very clear to me if I can delete it. – tgogos Mar 28 '17 at 11:55
5

For my current docker version (1.12.1 for both Client & Server) a way to delete all volumes is by using:

docker volume rm $(docker volume ls -q)

but the following is safer: (thanks Matt for your comment)

$(docker volume ls -qf dangling=true)

Also from version: 1.13.0 (2017-01-18) some commands were added:

$ docker system prune
$ docker container prune
$ docker image prune
$ docker volume prune
$ docker network prune

Changelog: Add new docker system command with df and prune subcommands for system resource management, as well as docker {container,image,volume,network} prune subcommands #26108 #27525 / #27525

tgogos
  • 23,218
  • 20
  • 96
  • 128
  • 1
    Using `$(docker volume ls -qf dangling=true)` is a bit safer in case there are containers still around. In your case it still works the same – Matt Mar 28 '17 at 11:33
1

Most of the space is occupied by docker volume as you can see from your output:

12G ./volumes

Docker volumes are used to persist data for docker container and to share data between containers, and they are independent of the container’s lifecycle. So removing image/container will not free the disk space they occupied. Please refer to their official docs for more details.

If you're using latest version of docker, you can find volume related commands docs for more details(list/remove/create volumes e.g), for older version of docker, you can refer to this script on github for how to clean up volumes.

Hope this could be helpful:-)

shizhz
  • 11,715
  • 3
  • 39
  • 49