I am brand new to Docker and am trying to understand exactly what a Docker image is. Every single definition of a Docker image uses the term "layer", but does not seem to define what is meant by layer.
From the official Docker docs:
We’ve already seen that Docker images are read-only templates from which Docker containers are launched. Each image consists of a series of layers. Docker makes use of union file systems to combine these layers into a single image. Union file systems allow files and directories of separate file systems, known as branches, to be transparently overlaid, forming a single coherent file system.
So I ask, what is a layer (exactly); can someone give a few concrete examples of them? And how do these layers "snap together" to form an image?
Since Docker v1.10, with introduction of the content addressable storage, the notion of 'layer' became quite different. Layers have no notion of an image or belonging to an image, they become merely collections of files and directories that can be shared across images. Layers and images became separated.
For example, on a locally built image from a base image, let's say,
ubuntu:14.04
, thedocker history
command yields the image chain, but some of the image IDs will be shown as 'missing' because the build history is no longer loaded. And the layers that compose these images can be found viaThe layer content is stored at
/var/lib/docker/aufs/diff
if the storage driver selection isaufs
. But the layers are named with a randomly generated cache ID, it seems the link between a layer and its cache ID is only known to Docker Engine for security reasons. I am still looking for a way to find outThis blog provided much insight.
I might be late, but here's my 10 cents (complementing ashishjain's answer):
Basically, a layer, or image layer is a change on an image, or an intermediate image. Every command you specify (
FROM
,RUN
,COPY
, etc.) in your Dockerfile causes the previous image to change, thus creating a new layer. You can think of it as staging changes when you're using git: You add a file's change, then another one, then another one...Consider the following Dockerfile:
First, we choose a starting image:
rails:onbuild
, which in turn has many layers. We add another layer on top of our starting image, setting the environment variableRAILS_ENV
with theENV
command. Then, we tell docker to runbundle exec puma
(which boots up the rails server). That's another layer.The concept of layers comes in handy at the time of building images. Because layers are intermediate images, if you make a change to your Dockerfile, docker will build only the layer that was changed and the ones after that. This is called layer caching.
You can read more about it here.
Per Docker's image spec
So, essentially, layer is just a set of changes made to the filesystem.
My personal understanding is that we can compare docker layer to github commit. For your base image(your fresh master repo), you make several commits, every commit is changing your master status, it's the same in docker, every layer is doing some operation based on previous intermediate layer. And then, this layer become a new intermediate layer to the next layer.
Thank you @David Castillo for the useful information. I think the layer is some binary change or instruction of a image that can be done or undone easily. They are done step by step that is same as a layer on a layer, so we called "layer".
For more information you can see the "docker history" like this:
A docker container image is created using a dockerfile. Every line in a dockerfile will create a layer. Consider the following dummy example:
This will create a final image where the total number of layers will be X+3