Troubleshoot Docker build failures
Topic: Containers core
Summary
When docker build fails check the failing step, cache, and context. Use --no-cache to rule out cache. Check Dockerfile syntax and paths. Use when a build fails or is slow.
Intent: Troubleshooting
Quick answer
- Read the error line. Often COPY or RUN path is wrong. Fix path or add file to context. Use .dockerignore to exclude unneeded files.
- Run with --no-cache to avoid stale cache. Check base image exists and network for apt or pip.
- Build in stages; run the failing RUN in a temporary container to debug. Check multi-stage COPY --from= paths.
Prerequisites
Steps
-
Locate failure
Note which step fails. Check that files exist in build context and paths in COPY and RUN are correct.
-
Cache and network
Try docker build --no-cache. For RUN apt or pip ensure network and base image are valid.
-
Debug step
Comment out later steps; build to failing step. Run a shell in that layer to reproduce (docker run -it image_id /bin/sh).
Summary
Find failing step; fix paths and context; use —no-cache if needed; debug by running shell at that layer.
Prerequisites
Steps
Step 1: Locate failure
Identify step; verify paths and context.
Step 2: Cache and network
Try —no-cache; check base image and network.
Step 3: Debug step
Build to failure; run shell at that layer to debug.
Verification
- Build completes; or you know exact fix needed.
Troubleshooting
COPY failed — File not in context or path wrong. RUN failed — Reproduce in interactive container.