A teammate edits one line of JavaScript and Docker installs every dependency again. Later, recreating the container makes an uploaded file disappear. Those failures occur at different points: one concerns building an image, the other writing data at runtime. Trace them separately before proposing a fix.
Explain what the build can reuse
1. Why did editing an application file invalidate the install?
Docker can reuse a cached build step when its instruction and relevant inputs match. A broad COPY . . before dependency installation makes application edits relevant to the copy and forces downstream steps to rebuild. Copy manifests first, install from the lockfile, then copy implementation files. Exclude local dependencies and build outputs from the context using .dockerignore. If installation also depends on workspace manifests, those must be copied before the install too.
FROM node:24-slim
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci --omit=dev
COPY src ./src
USER node
CMD ["node", "src/index.js"]
This example assumes a single package, checked-in lockfile, and a server that can run directly from src. It does not show a transpilation step. The version tag is readable but mutable; if the build requires reproducible base inputs, pin a digest and maintain a process for updating it.
2. Does deleting a tool in a later layer remove it from the image?
The final filesystem may hide the file, but the bytes remain in the earlier image layer. A build stage can compile assets with the needed tools, then copy only the outputs into a separate runtime stage. Test the final stage, not just the builder: missing shared libraries or runtime assets can first show up there.
3. Where should private registry credentials go during a build?
Use build secret mounts rather than copying credential files into the context or passing credentials as ordinary build arguments. Give the relevant build step temporary access to the secret. It still must not print or copy the secret into output. Check the build logs as well as the final artifacts when evaluating that boundary.
Explain what survives the container
4. Why did a file disappear after the container was recreated?
A container gets a writable layer over the image. That layer is tied to the container, not the image, and is destroyed when the container is removed. Use a named volume when data must be retained separately, or keep durable data in an external service. Restarting the same container is not equivalent to replacing it; reproduce the latter when testing persistence.
5. Why does the container not shut down cleanly?
Check which process is PID 1 and which process receives the stop signal. Exec-form CMD starts the application directly when there is no intervening entrypoint wrapper; a shell wrapper may require explicit signal forwarding or exec. Signal delivery is only the first step: the application must stop accepting new work and close resources within its shutdown window.
Try three changes independently
- Edit a source file, then the lockfile, and compare the cached steps.
- Write a test file with and without a mounted volume, then recreate the container.
- Stop the container during an in-flight request and check what the application logs.
For the next layer up, read the Kubernetes rollout walkthrough. Then use Mentaro's free practice demo to rehearse explaining one observed result and its cause.