what is doceker layer ?

A Docker layer is a read-only part of a Docker image created during the image-building process.Each instruction in a Dockerfile such as FROM, RUN, COPY, or ADD creates a new layer.

Docker uses these layers to make images efficient, reusable, and faster to build.

suppose you have a docker file like this

FROM python:3.11

WORKDIR /app

COPY requirements.txt .

RUN pip install -r requirements.txt

COPY . .

CMD ["python","manage.py","runserver"]

Docker executes one instruction at a time.Each instruction creates a read-only layer.

Layer 1:
FROM python:3.11
-------------------
Base OS + Python

Layer 2:
WORKDIR /app
-------------------
Creates working directory

Layer 3:
COPY requirements.txt .
-------------------
Adds requirements file

Layer 4:
RUN pip install
-------------------
Installs packages

Layer 5:
COPY . .
-------------------
Copies project code

Layer 6:
CMD
-------------------
Startup command

Algorithm for Docker Layer Creation Process

  1. Start Docker build process

  2. Read Dockerfile from top to bottom

  3. Create base layer using FROM python:3.11

  4. Create working directory using WORKDIR /app

  5. Copy requirements.txt into container using COPY requirements.txt .

  6. Install required packages using RUN pip install -r requirements.txt

  7. Copy project files into container using COPY . .

  8. Store startup command using CMD

  9. Stack all layers together to form the final Docker image

  10. Add a writable layer when a container runs from the image

  11. Why Docker uses layers?

A.Faster build (Caching):