What are the "layers" of a docker image?

I am new to Docker and trying to understand what a Docker image is. Literally every definition of a Docker image uses the term "layer", but surprisingly, it does not actually define what is meant by a layer. From the official Dockers docs :

We have already seen that Docker images are read-only templates from which Docker containers are launched. Each image consists of a series of layers. Docker uses union file systems to combine these layers into a single image. Union system file systems allow file files and directories of individual file systems, called branches, to overlap transparently to form a single, consistent file system.

So, I ask: What is a layer (for sure); can someone give some concrete examples of them? And how do these layers โ€œcombineโ€ to form an image?

+133
docker
Jul 04 '15 at 15:38
source share
6 answers

Maybe I'll be late, but here are my 10 cents (complementary answer from ashishjain):

Basically, an image layer or layer is a change in an image or an intermediate image . Each command you specify ( FROM , RUN , COPY , etc.) in your Docker file changes the previous image, creating a new layer. You can think of it as permutations when you use git: you add a file change, then one more and then another ...

Consider the following Docker file:

 FROM rails:onbuild ENV RAILS_ENV production ENTRYPOINT ["bundle", "exec", "puma"] 

First, we select the initial image: rails:onbuild , which, in turn, has many layers . Add another layer on top of our start image by setting the environment variable RAILS_ENV with the ENV command. Then we say that docker launches bundle exec puma (which loads the rails server). This other layer.

The concept of layers comes in handy when creating images. Since layers are intermediate images, if you make changes to your Docker file, the docker will only create the layer that was changed and those that were after that. This is called layer caching.

You can read about it here .

+108
Nov 20 '15 at 22:06
source share

An image of a docker container is created using dockerfile . Each line in the dockerfile will create a layer. Consider the following dummy example:

 FROM ubuntu #This has its own number of layers say "X" MAINTAINER FOO #This is one layer RUN mkdir /tmp/foo #This is one layer RUN apt-get install vim #This is one layer 

This will create the final image in which the total number of layers will be X + 3

+66
Jul 04 '15 at 17:13
source share

Since Docker v1.10, with the introduction of address-based content storage, the concept of โ€œlayerโ€ has become completely different. Layers have no idea about an image or belong to an image; they simply become collections of files and directories that can be shared between images. Layers and images are separated.

For example, on a locally built image from a base image, say, ubuntu:14.04 , the docker history gives a chain of images, but some image identifiers will appear as โ€œmissingโ€ because the build history is no longer loading. And the layers that make up these images can be found through

 docker inspect <image_id> | jq -r '.[].RootFS' 

The contents of the layer are saved in /var/lib/docker/aufs/diff , if the aufs repository driver is selected. But the layers are called with a randomly generated cache identifier, it seems that the connection between the layer and its cache identifier is known only to the Docker Engine for security reasons. I'm still looking for a way to find out

  • The corresponding relationship between the image and its layer (s) layout
  • Actual location and layer size on disk

This blog has given a lot of insight.

+17
Mar 28 '17 at 12:11
source share

Per Docker Image Description

Images are made up of layers. Each level is a collection of file system changes. Layers do not have configuration metadata, such as environment variables or default arguments โ€” these are image properties as a whole, not any specific layer.

So, in essence, a level is just a set of changes made to the file system.

+10
Nov 14 '16 at 15:05
source share

I think the white paper gives a fairly detailed explanation: https://docs.docker.com/engine/userguide/storagedriver/imagesandcontainers/ .

An image consists of many layers, which are usually generated from a Docker file, each line in the Dockerfile will create a new layer, and the result will be an image that is indicated by the form repo:tag , for example ubuntu:15.04 .

For more information, check out the white papers above.

+8
Nov 09 '16 at 8:56
source share

Thanks to @David Castillo for the helpful information . I think this layer is a binary change or image instruction that can be easily or easily undone. They are performed in stages, just like a layer on a layer, so we call a "layer".

For more information, you can see the โ€œdocker storyโ€ as follows:

 docker images --tree
 Warning: '--tree' is deprecated, it will be removed soon.  See usage.
 โ””โ”€511136ea3c5a Virtual Size: 0 B Tags: scratch: latest
   โ””โ”€59e359cb35ef Virtual Size: 85.18 MB
     โ””โ”€e8d37d9e3476 Virtual Size: 85.18 MB Tags: debian: wheezy
       โ””โ”€c58b36b8f285 Virtual Size: 85.18 MB
         โ””โ”€90ea6e05b074 Virtual Size: 118.6 MB
           โ””โ”€5dc74cffc471 Virtual Size: 118.6 MB Tags: vim: latest

+2
Mar 02 '16 at 4:25
source share



All Articles