Monday, May 30, 2016

Deal with Docker


Dockers containers built with a Dockerfile can get quite large. We slim down our containers by these simple tricks before publishing.

Cleaning APT

The obvious apt-clean for cleaning out the apt-cache and cleaning out tmp.
RUN apt-get clean
RUN rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

Flatten an image

A Dockerfile builds your container in many individual steps. Resulting in a container with some overhead. Whenever you want to remove the excess overhead you can flatten your image by exporting and importing and creating an new image. This is a handy trick for creating base images.
ID=$(docker run -d image-name /bin/bash)
docker export $ID | docker import – flat-image-name
Thanks for the tip Kryztoval!
If you want to save it for backup you can use gzip to compress the image.
ID=$(docker run -d image-name /bin/bash)
(docker export $ID | gzip -c > image.tgz)
gzip -dc image.tgz | docker import - flat-image-name
Got any better ways to slim down your containers? Please post in the comments!

No comments:

Post a Comment