How Can I Inspect the File System of a Failed Docker Build?
TLDR
To inspect the file system of a failed Docker build, use multi-stage builds to save intermediate images, add debug steps (like sleep or tail), or leverage BuildKit's --target and --output features. This lets you start a shell in the failed state and debug interactively.
Why Is This Useful?
When a Docker build fails, you often want to see what files, environment variables, or installed packages are present at the failure point. This helps you debug missing files, permissions, or unexpected build behavior.
Method 1: Add a Debug Step (sleep or tail)
Temporarily add a line to your Dockerfile before the failing step:
RUN sleep infinity
or
RUN tail -f /dev/null
Build the image:
docker build -t debug-image .
In another terminal, find the running container:
docker ps
Start a shell in the container:
docker exec -it <container_id> /bin/bash
Now you can inspect the file system, check logs, and debug interactively. When done, remove the debug line from your Dockerfile.
Method 2: Use Multi-Stage Builds to Save Intermediate State
If your build uses multi-stage builds, you can tag an intermediate stage and run a container from it:
FROM ubuntu:22.04 as builder
# ... build steps ...
FROM builder as debug-stage
# Add a debug step if needed
Build up to the debug stage:
docker build --target debug-stage -t debug-image .
docker run -it debug-image /bin/bash
Method 3: Use BuildKit's --output=type=local
With BuildKit enabled, you can export the build's file system to a local directory:
docker build --output type=local,dest=./output .
This writes the final build context to ./output, so you can inspect files directly on your host.
Method 4: Commit a Stopped Container (if build got far enough)
If your build ran a container that exited, you can commit it to an image:
docker ps -a # Find the exited container
# Then:
docker commit <container_id> debug-image
docker run -it debug-image /bin/bash
Best Practices
- Remove debug steps after you're done to keep images clean.
- Use multi-stage builds to avoid leaking secrets or build tools into final images.
- For complex builds, consider using BuildKit for advanced debugging features.
Conclusion
Inspecting the file system of a failed Docker build is easy with debug steps, multi-stage builds, or BuildKit's output features. Use these techniques to troubleshoot and fix build issues faster.
Related Resources
- Exploring Docker Container File System — browse container files
- Advanced Docker Features — BuildKit debugging
- Force Docker Clean Build — rebuild from scratch
- Introduction to Docker: Building Images — Dockerfile guide
We earn commissions when you shop through the links below.
DigitalOcean
Cloud infrastructure for developers
Simple, reliable cloud computing designed for developers
DevDojo
Developer community & tools
Join a community of developers sharing knowledge and tools
SMTPfast
Developer-first email API
Send transactional and marketing email through a clean REST API. Detailed logs, webhooks, and embeddable signup forms in one dashboard.
QuizAPI
Developer-first quiz platform
Build, generate, and embed quizzes with a powerful REST API. AI-powered question generation and live multiplayer.
Want to support DevOps Daily and reach thousands of developers?
Become a SponsorFound an issue?
Related Posts
Also worth your time on this topic
docker: 'build' requires 1 argument. See 'docker build --help'
Getting the error 'docker: build requires 1 argument'? Learn what it means, why it happens, and how to fix it with practical examples for your Docker workflow.
Docker Security Hardening Checklist
Comprehensive security checklist for hardening Docker containers, images, and runtime environments.
60-90 minutes
Running Docker Containers on Your Linux Server
Install Docker and Docker Compose on Ubuntu, run your first container, deploy a WordPress stack with docker-compose, and set up Nginx as a reverse proxy in front of your containers.
60 minutes