Skip to content
BIASED
Back to topic

Optional

Image layers and cache hash

For a 5-step Dockerfile mark which layer a COPY vs RUN apt edit invalidates.

Version
v1
Minutes
~20
Evidence
Self-check in the notebook

Until platform auto-check exists, compare cell output with the expected numbers in the notebook. This is not a debt and does not affect mastery.

Download .ipynb

Download the notebook and run it locally. Its environment requirements are listed inside.

Notebook contents

Readable on your phone. The code below has not been executed in the browser; download the notebook to run it.

DevOps · Слои Dockerfile и кэш / Dockerfile layer cache

Activity version: 1 · Seed: n/a · Данные: 5 инструкций; правка COPY app vs RUN apt.

Правило префиксного кэша: первая изменённая инструкция инвалидирует все слои после неё.

Ожидаемые числа
Quantity Value Tolerance
steps 5 exact
layers rebuilt after COPY app edit (index 3) 2 exact (steps 3–4)
layers rebuilt after RUN apt edit (index 1) 4 exact (steps 1–4)
Самопроверка в ноутбуке

Не влияет на mastery.

import hashlib
 
steps = [
    ("FROM", "python:3.12-slim"),
    ("RUN", "apt-get update && apt-get install -y curl"),
    ("COPY", "requirements.txt /app/"),
    ("RUN", "pip install -r /app/requirements.txt"),
    ("COPY", "app/ /app/src/"),
]
 
def layer_ids(instructions):
    ids = []
    prev = b""
    for op, arg in instructions:
        payload = prev + f"{op} {arg}".encode()
        digest = hashlib.sha256(payload).hexdigest()[:12]
        ids.append(digest)
        prev = digest.encode()
    return ids
 
base = layer_ids(steps)
 
# Edit application COPY (index 4) — only that layer and nothing after (it is last).
# Prefer the course drill: COPY app is step index 4; if we edit requirements COPY (2) vs RUN apt (1).
# Scenario A: edit COPY app (last step) → 1 rebuild.
edited_app = [list(s) for s in steps]
edited_app[4][1] = "app_v2/ /app/src/"
ids_app = layer_ids([tuple(s) for s in edited_app])
rebuilt_app = sum(1 for a, b in zip(base, ids_app) if a != b)
 
# Scenario B: edit RUN apt (index 1) → rebuild steps 1..4 (4 layers).
edited_apt = [list(s) for s in steps]
edited_apt[1][1] = "apt-get update && apt-get install -y curl git"
ids_apt = layer_ids([tuple(s) for s in edited_apt])
rebuilt_apt = sum(1 for a, b in zip(base, ids_apt) if a != b)
 
# Scenario C: edit COPY requirements (index 2) → rebuild 2..4 (3 layers); warm FROM+apt.
edited_req = [list(s) for s in steps]
edited_req[2][1] = "requirements-lock.txt /app/"
ids_req = layer_ids([tuple(s) for s in edited_req])
rebuilt_req = sum(1 for a, b in zip(base, ids_req) if a != b)
 
print("rebuilt_app", rebuilt_app, "rebuilt_apt", rebuilt_apt, "rebuilt_req", rebuilt_req)
assert len(steps) == 5
assert rebuilt_app == 1
assert rebuilt_apt == 4
assert rebuilt_req == 3
assert base[0] == ids_apt[0]  # FROM still cached after apt edit
print("OK: Dockerfile layer cache")
 
Самопроверка в ноутбуке
  • правка COPY app инвалидирует 1 слой
  • правка RUN apt инвалидирует 4 слоя после FROM