Skip to content

Instantly share code, notes, and snippets.

@rheinwein
Last active October 20, 2016 16:32
Show Gist options
  • Save rheinwein/23a713795f7d92e547e2 to your computer and use it in GitHub Desktop.
Save rheinwein/23a713795f7d92e547e2 to your computer and use it in GitHub Desktop.
Introducing ImageLayers

ImageLayers.io is a new project from the team at CenturyLink Labs. This utility provides a visualization in the browser of user-specified Docker Images and their layers. This visualization provides key information on the composition of a Docker Image and any commonalities between them. ImageLayers.io allows Docker users to easily discover best practices for image construction, and aid in determining which images are most appropriate to use in their specific use cases.

##What are Layers? Docker uses a union file system for managing images. Each layer of your image is the result of some action taken during the build. This could be installing a package, copying files from a local or remote source, or setting an environment variable. Each action creates a new layer in the file system; layers reference their parents and are cached, so they may be used as a part of an entirely different image.

There is a direct correlation between your Dockerfile and the number of layers in your image. Docker offers some advice on Dockerfile best practices, and the team at CenturyLink Labs has also done work around optimizing Docker images. ImageLayers grew from the need to see all layers in an imagine in order to make optimization decisions.

##Creating Smarter Docker Images The layered file system is similar to a tree, and Docker is smart enough to branch from any layer if it sees the same work being done. There are two basic strategies for optimizing your Docker images: chaining commands and using smart base images.

###Chaining Commands to reduce the number of layers

Here's a sample Dockerfile that pulls down a base image and installs a package.

FROM debian:wheezy
WORKDIR /tmp

RUN wget -nv
RUN tar -xvf someutility-v1.0.0.tar.gz
RUN mv /tmp/someutility-v1.0.0/someutil /usr/bin/someutil
RUN rm -rf /tmp/someutility-v1.0.0
RUN rm /tmp/someutility-v1.0.0.tar.gz

Because there is a 1-to-1 correlated between the Dockerfile and the layers in the image, this image would have many. A new layer is created for each of the RUN commands.

Since each of the RUN commands above is related to the same package, we can chain them together and execute all of the commands on the same layers. That looks like this:

FROM debian:wheezy
WORKDIR /tmp

RUN wget -nv && tar -xvf someutility-v1.0.0.tar.gz \
  && mv /tmp/someutility-v1.0.0/someutil /usr/bin/someutil \
  && rm -rf /tmp/someutility-v1.0.0 \
  && rm /tmp/someutility-v1.0.0.tar.gz

The resulting image has fewer layers and therefore takes up less space than the first example. ImageLayers lets you see each layer and its instruction more readily, making it easier for you to identify resource-sucking parts of your Dockerfile.

###Extracting shared work to a base image Understanding Space Performance

IL Badge Demo

Don’t have the only image without one!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment