Docker Community Forums

Share and learn in the Docker community.

Best practice in order to mantain containers and save space

Hi all and nice to meet you. Docker compose has simplified my developer life, but after a while I’m using it, I’m experiencing a little lack of resources on my macbook.
The main issue is the disk space.
Consider that I have a couple of docker-compose.yml template (sometimes I can try something new just for test) that I use for many projects.
Is there a way to limitate the number of images created, and the container dimensions?
At the moment I need to do a system prune to free space, and start again.

As far as I know, docker system prune do not remove unused images. you can use docker image prune for that. (warning : this will also remove intermediate images cache, so next builds might be slower).

As for limiting the images size, it’s a vast subject. But here is the main points :

  • Try limiting the number of layer your images has
  • Use intermediate build (as in: multiple FROM) and only copy the result in the final image (so the final image dont have the build artifacts nor the build dependencies)
  • Do NOT base your work on top of ubuntu which is, by far, the largest base. Try using scratch base image (for golang apps, or static builds), or alpine (everything else)
  • Install the strict minimum within the final image

hi, i have the same issue. i do always this
look which images exist by docker images and how much disc space is used
remove the unneeded images by docker image rm {image_id} {image_id}

I do the same thing, but I’ve notice that there are much more images then the containers I’ve installed, and many of them have no name, so i don’t know what to delete.
Maybe, I guess, all the process of creation behind docker-compose it’s not so clear for me. I’ve tought that there should be one image for container (every container indicate an image to use line alpine:nginx and. soon) and that image is used during building phase. So, once builded may I delete the image?

The first thing you can to is to list the images that are not related to anything else:

docker image ls -f 'dangling=true'

You can delete them without hesitation. To do it automatically run

docker image rm $(docker image ls -f 'dangling=true' -q)

There are some wrong ideas about getting more space by deleting images. The space you get back is not the image size that is displayed in the list, it depends on how many layers of the image are not shared with others. If it shares 95 % with others that you don’t delete you win nearly nothing.

1 Like