patternModerate
Docker build cache invalidation from COPY before RUN install
Viewed 0 times
Docker cachelayer cachingCOPYnpm cipip installbuild speeddockerignore
Problem
Docker builds are slow because changing any source file invalidates the
npm install / pip install layer, causing a full dependency reinstall every build.Solution
Copy dependency files first, install, then copy source. For Node:
COPY package.json package-lock.json ./ then RUN npm ci, then COPY . .. For Python: COPY requirements.txt ./ then RUN pip install -r requirements.txt, then COPY . .. This way the install layer is cached unless dependencies actually change. Also use .dockerignore to exclude node_modules, .git, and other large directories from the COPY context.Why
Docker layers are cached sequentially. If layer N changes, all layers after N are rebuilt. By isolating dependency installation before source copying, you only rebuild the install layer when dependencies change.
Revisions (0)
No revisions yet.