«

»

Mar 23

Shrinking Docker Images

Size does matter. Docker images can become quite large as each RUN generates a new layer which becomes part of the image, even if it’s not in the final container. This wastes disk space and network bandwidth. The following are some steps for shrinking the size of a docker container in which builds have been performed — they work particularly well for containers which have go executables.

In the Dockerfile

  1. Remove any build archives
  2. Remove any packages which were installed to build/compile which are not needed later

Compact the Image

If you perform a docker export of a container, it produces a tarball of the flattened Docker image. This can then be re-imported, at great size reduction1. For example, assuming the “big” image is named “consul-big” and the small named “consul-small”, executing the following command:

produces something like this:

A reduction of over 250MB. Not too shabby. I might be able to reduce it further, but I’m close to diminishing returns. A chunk of the size is due to dependencies which progrium/consul has, primarily in some accessory shell scripts.

May your Docker images grow ever smaller!


1 Your mileage may vary. The tip is provided without warranty of any kind. No images were harmed in the making of this blog post.

4 comments

3 pings

Skip to comment form

  1. Kyle C. Quest (@kcqon)

    You can get even smaller images with DockerSlim [1]. It’ll keep only what your application needs, so you can use regular distros and you won’t have to use any Dockerfile optimizations or layer tricks.

    [1] http://dockersl.im

  2. Matt Williams

    I’m curious — how do you tell what the application needs? I see that your app prefers http services — do you have to test every path in order to be sure that the code is recognized as being needed? I’m thinking in terms of a case where code is dynamically loaded, but only if it is needed. How are you handling such?
    Thanks!

  3. Rich Moyse

    In short, images are generally larger than necessary, as Dockerfiles include build tooling and an application’s runtime bits within the resultant image. Therefore, composing an image of only runtime artifacts dramatically reduces its size and malware attack surface.

    The bash script dkrcp [1] enables composing an image by simply copying files from other images, containers, stream, or host file system. This ability supports transferring only the runtime bits needed from build images/containers to a resultant image dedicated to running the application. So, if a developer can identify the runtime bits, dkrcp can copy them to construct a minimal image.

    [1] http://tinyurl.com/dkrcp-github

  4. Matt Williams

    Agreed; assuming you can identify all of the pieces required.

  1. TechNewsLetter Vol:9 | Devops Enthusiast

    […] Shrinking Docker Images. […]

  2. Running OBIEE 11g inside Docker | Sasikanth Kotti

    […] of the image can be reduced by following the blog post Shrinking Docker Images by Matt Williams (http://matthewkwilliams.com/index.php/2015/03/23/shrinking-docker-images/) 15.The name of the final image is […]

  3. Minimal docker : run your NodeJS app in <25mb of an image | Dinesh Ram Kali.

    […] the end of the day, one thing is clear: we’d like to shrinkimages as much as possible. Turns out, the easiest solution is, as often, the simplest one: start […]

Leave a Reply

%d bloggers like this: