Looking for an Expert Development Team? Take two weeks Trial! Try Now

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 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 is the command to see the list of images with their locations in a JSON file.

Docker Layer Architecture
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.

DMCA Logo do not copy