How to use Docker layer architecture?

banner

Docker Layers

Layers of docker file just like, files are generated after running some command. Docker provides the different storage drivers for Linux flavored operating systems and windows.

Below are some of the drivers for Linux flavored operating systems.

  • aufs (the original and oldest)
  • overlay2 (probably the best choice for the future)
  • devicemapper
  • btrfs
  • Zfs

For windows, only one driver named windows filter supported by docker.

Docker file will create intermediate images for some of the instructions like RUN, COPY, ADD, etc. for others, it just updates the metadata of the existing image.

Each docker file will have a base image, for this base image, the docker will create a layer, and then next docker instructions will be executed based on this layer.

We can find the all docker layers in /var/lib/docker/<storage-driver> folder.

We can find the default storage driver in two ways.

$docker system info

$docker system info is the command to display a storage driver.

Or specify the storage driver in /etc/docker/daemon.json file and restart the docker to reflect the changes.

It will contain all docker layers for a specific storage driver.

Navigate to any of the folders, we can find the contents of the layer.

For suppose, execute the docker pull hello-world command.

For Linux overlay2 is the default storage driver.

Navigate to /var/lib/docker/overlay2 and do ls -al to display all files and folders. We can find some hashcode folder.

Docker Layer Architecture

Navigate to 35bf… /diff folder to see the contents of this layer.

These layers can be re-used by multiple images to reduce the disk space, and also reducing the time to build the image.

Let's create docker build and see how these layers are generating:

Clone the https://github.com/nigelpoulton/psweb.git to generate the docker image.

Below is the docker file.

FROM alpine LABEL maintainer="nigelpoulton@hotmail.com" RUN apk add --update nodejs nodejs-npm COPY . /src WORKDIR /src RUN npm install EXPOSE 8080 ENTRYPOINT ["node", "./app.js"]

Alpine is the minimal Linux based os with mandatory commands are available and also with the small-sized docker image.

The GitHub repository is the nodejs based application, so we need to nodejs and npm tools, we are doing in the second line based on the alpine image layer, in this stage, docker creates a temporary image for this.

COPY . /src

Means copy buildContext folder to src folder, changed the working directory to src folder, and then installing npm dependencies then starting nodejs application.

$ docker build . -t pswed:latest Sending build context to Docker daemon 76.29kB Step 1/8 : FROM alpine latest: Pulling from library/alpine aad63a933944: Pull complete Digest: sha256:b276d875eeed9c7d3f1cfa7edb06b22ed22b14219a7d67c52c56612330348239 Status: Downloaded newer image for alpine:latest ---> a187dde48cd2 Step 2/8 : LABEL maintainer="nigelpoulton@hotmail.com" ---> Running in 54d4036fc131 Removing intermediate container 54d4036fc131 ---> f041b9b9a1a8 Step 3/8 : RUN apk add --update nodejs nodejs-npm ---> Running in 0a3e9afc4998 Removing intermediate container 0a3e9afc4998 ---> 86b17f5c18bc Step 4/8 : COPY . /src ---> a117f6f995bc Step 5/8 : WORKDIR /src ---> Running in 20e764c8c12a Removing intermediate container 20e764c8c12a ---> b09e1fc39d37 Step 6/8 : RUN npm install ---> Running in da8f53c85998 found 6 vulnerabilities (3 low, 2 moderate, 1 critical) run `npm audit fix` to fix them, or `npm audit` for details Removing intermediate container da8f53c85998 ---> 21f4f9efe773 Step 7/8 : EXPOSE 8080 ---> Running in b37fdc689791 Removing intermediate container b37fdc689791 ---> be181852e0a9 Step 8/8 : ENTRYPOINT ["node", "./app.js"] ---> Running in 609848ef9d90 Removing intermediate container 609848ef9d90 ---> b01d231a9581 Successfully built b01d231a9581 Successfully tagged pswed:latest

As we saw from the above command for intermediate docker image, deleting and creating of the new images.

$ docker image inspect pswed

$ docker image inspect pswed is the command to see the list of images with their locations in a JSON file.

"GraphDriver": "Data ": { "LowerDir": "/var/lib/docker/overlay2/d8a5d8dlf0166a8a2002120162f201ce03628a7b366ddc4cf77f9e0631b3542b/diff:/var/lib/docker/overlay2/408dl47d06ae5fcdd9c956ac4c 7671650cd0fb5ad74e9e656b5cf8a4df2e690d/d iff:/var/lib/docker/overlay2/48612717988b934eblf2108d4172ab79077c9f0335538c8a6ce13542100de698/d iff", "MergedDir": "/var/lib/docker/overlay2/0ffa7333a5461ab749b0e16252bccf0682642196a418e28287213a624149976a/merged", "UpperDir": "/var/lib/docker/overlay2/0ffa7333a5461ab749b0e16252bccf0682642196a418e28287213a624149976a/diff", "WorkDir": "/var/lib/docker/overlay2/0ffa7333a5461ab749b0e16252bccf0682642196a418e28287213a624149976a/work" }, "Name": "overlay2" }, "RootFS":{ "Type":"layers", "Layers": [ "sha256:beee9f3 0bclf711043e78d4a2be0668955d4b761d587d6f60c2c8dc081efb203", "sha256:1b176b76f80e10be505893323887537af569f586dbe2d0b81488c69c8037a9d0", "sha256:c458c53c4elabad0d823f54b244ca9e85547b311738869a36f0ccdc8c9al039f", "sha256:0c45eb349dfc4bf83c9657bad8757356bfba015ddl7e46d8c52cf4f987e2d0dl"] }, "MetaData":{ "LastTagTime": "2020-03-24T17:56:17.615464261Z" } } And navigate to /var/lib/docker/overlay2 folder to see all docker layers.

Conclusion:

We learned about the docker layers, how it will be formed, what are its usages, and also learned about the above docker storage driver. Docker command to get all layers for a given image, how to find the contents of this layer. Don't worry if you're stuck in a project, Hire Full Stack Java Developers team for a new and ongoing project which solve your query also guide how you can go ahead.

Related article

Docker is one of best tools for containerization tool for creating images. Docker provide the layered architecture, using which it will not take much space

Asp.net web development team will explain the way to resolve issue related to not calling controller method. The experts will use AJAX JSON call from view page.

Json is a lightweight format that is used in asp.net development for data exchange.

DMCA Logo do not copy