Why a multilayered approach in Docker?

Anushkaa BansalAnushkaa Bansal
2 min read

#Docker #LearnInPublic
In Docker, a multilayered approach is used to build and manage images. This approach involves creating multiple layers, each with its own specific purpose, to form a Docker image. At a time, three layers are pulled.

๐—•๐—ฒ๐—ป๐—ฒ๐—ณ๐—ถ๐˜๐˜€ ๐—ผ๐—ณ ๐—ฎ ๐— ๐˜‚๐—น๐˜๐—ถ๐—น๐—ฎ๐˜†๐—ฒ๐—ฟ๐—ฒ๐—ฑ ๐—”๐—ฝ๐—ฝ๐—ฟ๐—ผ๐—ฎ๐—ฐ๐—ต

๐—˜๐—ณ๐—ณ๐—ถ๐—ฐ๐—ถ๐—ฒ๐—ป๐˜ ๐—จ๐˜€๐—ฒ ๐—ผ๐—ณ ๐—ฆ๐˜๐—ผ๐—ฟ๐—ฎ๐—ด๐—ฒ: By separating the image into multiple layers, Docker can efficiently store and manage the image. Each layer is stored only once, even if multiple images use the same layer.

๐—™๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—•๐˜‚๐—ถ๐—น๐—ฑ ๐—ง๐—ถ๐—บ๐—ฒ๐˜€: When building a new image, Docker can reuse existing layers, reducing the build time and effort required.

๐—˜๐—ฎ๐˜€๐—ถ๐—ฒ๐—ฟ ๐— ๐—ฎ๐—ถ๐—ป๐˜๐—ฒ๐—ป๐—ฎ๐—ป๐—ฐ๐—ฒ: With a multilayered approach, it's easier to maintain and update individual layers without affecting the entire image.

๐—œ๐—บ๐—ฝ๐—ฟ๐—ผ๐˜ƒ๐—ฒ๐—ฑ ๐—ฆ๐—ฒ๐—ฐ๐˜‚๐—ฟ๐—ถ๐˜๐˜†: By separating the image into multiple layers, Docker can provide better security by allowing for more granular control over access and permissions.

๐—›๐—ผ๐˜„ ๐˜๐—ต๐—ฒ ๐— ๐˜‚๐—น๐˜๐—ถ๐—น๐—ฎ๐˜†๐—ฒ๐—ฟ๐—ฒ๐—ฑ ๐—”๐—ฝ๐—ฝ๐—ฟ๐—ผ๐—ฎ๐—ฐ๐—ต ๐—ช๐—ผ๐—ฟ๐—ธ๐˜€

๐—•๐—ฎ๐˜€๐—ฒ ๐—œ๐—บ๐—ฎ๐—ด๐—ฒ ๐—Ÿ๐—ฎ๐˜†๐—ฒ๐—ฟ: The base image layer provides the foundation for the image, typically including the operating system and basic dependencies.

๐—œ๐—ป๐˜๐—ฒ๐—ฟ๐—บ๐—ฒ๐—ฑ๐—ถ๐—ฎ๐˜๐—ฒ ๐—Ÿ๐—ฎ๐˜†๐—ฒ๐—ฟ๐˜€: Intermediate layers contain the application code, dependencies, and other components required to run the application.

๐—™๐—ถ๐—ป๐—ฎ๐—น ๐—Ÿ๐—ฎ๐˜†๐—ฒ๐—ฟ: The final layer contains the application's configuration, environment variables, and other runtime settings.

๐—”๐—ฑ๐˜ƒ๐—ฎ๐—ป๐˜๐—ฎ๐—ด๐—ฒ๐˜€ ๐—ผ๐—ณ ๐—Ÿ๐—ฎ๐˜†๐—ฒ๐—ฟ๐—ถ๐—ป๐—ด

๐—Ÿ๐—ฎ๐˜†๐—ฒ๐—ฟ ๐—–๐—ฎ๐—ฐ๐—ต๐—ถ๐—ป๐—ด: Docker caches each layer, allowing for faster build times and more efficient storage.

๐—Ÿ๐—ฎ๐˜†๐—ฒ๐—ฟ ๐—ฅ๐—ฒ๐˜‚๐˜€๐—ฒ: Docker can reuse layers across multiple images, reducing storage requirements and improving build efficiency.

๐—Ÿ๐—ฎ๐˜†๐—ฒ๐—ฟ ๐—œ๐˜€๐—ผ๐—น๐—ฎ๐˜๐—ถ๐—ผ๐—ป: Each layer is isolated from the others, providing a high degree of flexibility and customization.

๐—•๐—ฒ๐˜€๐˜ ๐—ฃ๐—ฟ๐—ฎ๐—ฐ๐˜๐—ถ๐—ฐ๐—ฒ๐˜€ ๐—ณ๐—ผ๐—ฟ ๐—ช๐—ผ๐—ฟ๐—ธ๐—ถ๐—ป๐—ด ๐˜„๐—ถ๐˜๐—ต ๐—Ÿ๐—ฎ๐˜†๐—ฒ๐—ฟ๐˜€

๐—ž๐—ฒ๐—ฒ๐—ฝ ๐—Ÿ๐—ฎ๐˜†๐—ฒ๐—ฟ๐˜€ ๐—ฆ๐—บ๐—ฎ๐—น๐—น ๐—ฎ๐—ป๐—ฑ ๐—™๐—ผ๐—ฐ๐˜‚๐˜€๐—ฒ๐—ฑ: Each layer should have a specific purpose and contain only the necessary components.

๐—จ๐˜€๐—ฒ ๐—ข๐—ณ๐—ณ๐—ถ๐—ฐ๐—ถ๐—ฎ๐—น ๐—•๐—ฎ๐˜€๐—ฒ ๐—œ๐—บ๐—ฎ๐—ด๐—ฒ๐˜€: Use official base images from Docker Hub to ensure compatibility and reduce the risk of security vulnerabilities.

๐—ข๐—ฝ๐˜๐—ถ๐—บ๐—ถ๐˜‡๐—ฒ ๐—Ÿ๐—ฎ๐˜†๐—ฒ๐—ฟ ๐—ข๐—ฟ๐—ฑ๐—ฒ๐—ฟ: Optimize the layer order to minimize the number of layers and reduce build times.

By using a multilayered approach, Docker provides a flexible and efficient way to build, manage, and deploy images. This approach allows developers to create complex applications with ease, while also ensuring efficient use of resources and improved security.

0
Subscribe to my newsletter

Read articles from Anushkaa Bansal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Anushkaa Bansal
Anushkaa Bansal

Student pursuing Btech in computer engineering. Learning Devops currently. I will document and share all my learnings through blogs.