Skip to content

Instantly share code, notes, and snippets.

@argvader
Forked from rheinwein/imagelayers.md
Last active August 29, 2015 14:20
Show Gist options
  • Save argvader/9680c737a311b3a721bd to your computer and use it in GitHub Desktop.
Save argvader/9680c737a311b3a721bd to your computer and use it in GitHub Desktop.

(IL screenshot header image)

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.

(image - wordpress:latest showing several run commands in row)

ImageLayers lets you see each layer and its instruction more readily, making it easier for you to identify resource-sucking parts of your Dockerfile. For example, the analysis of the wordpress:latest image shows several run commands which could be concatenated using the technique above.

###Extracting shared work to a base image (image of shared layers) Using images with shared layers within your application will reduce download times and the overall filesystem size. A layer is only downloaded once regardless if many images require it. Therefore, choosing a good base image for your application can be a performance boon. Using ImageLayers to investigate images on the DockerHub before they are downloaded is a quick way to find which layers may be shared. Within ImageLayers a shared layer spans the image columns so it is easy to identify which images a layer may have in common. The more layers in common the fewer requests are required to download the images.

###ImageLayers Badge (image of a badge) Don't be the last image on the Docker Hub to have an ImageLayers badge! Use the 'Get an Embed Badge' feature to create an html link or a markdown fragment which can be embedded into a document. Even the docker Hub (link to one of ours Panamax?).

###Conclusion ImageLayers was created to provide visual insight into the way an individual is created and how many images can share layers. This information can be used to understand how the Docker unified file system behaves or fine tune an application composed of many images. Create an analysis of some of your favorite images and share the url with your friends.

@rossjimenez
Copy link

make sure "ImageLayers.io" in first sentence is a hyperlink.

@rossjimenez
Copy link

@rossjimenez
Copy link

If you reference an image- link it: wordpress:latest -> https://imagelayers.io/?images=wordpress:latest

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