How can I inspect the file system of a failed docker build?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Inspecting the file system of a failed docker build is a crucial skill for developers and DevOps engineers. It allows one to debug and diagnose issues within the Docker image creation process. In this guide, we'll explore techniques to inspect the file system of a failed Docker build, complete with technical explanations and examples.
Understanding Docker Build
The docker build process involves executing a Dockerfile line by line to create a Docker image. When a build fails, it's typically due to a problematic command or configuration in the Dockerfile. When this happens, being able to inspect the filesystem as it was at the time of failure can provide essential insights.
Techniques to Inspect a Failed Docker Build
1. Using Intermediate Containers
When Docker builds an image, it creates intermediate containers for each step defined in the Dockerfile. These containers can be useful when a build fails. Here's how you can utilize them:
- Run the Intermediate Container: When a build fails, Docker outputs a message containing the ID of the last successful intermediate container. You can start a shell in this intermediate container using:
- Inspecting the Filesystem: Once inside the container, you can navigate the filesystem to inspect files and directories just as you would on any Linux system. Use common commands like
ls,cd,cat, etc.
2. Committing an Intermediate Container
In some cases, it might be more beneficial to save the state of the intermediate container:
- Commit the Container to an Image: You can commit the state to a new Docker image for further investigation using:
- Run a Shell in the Committed Image: You can then run a shell in the new image:
This approach allows repeated inspections without worrying about the container's lifecycle.
Debugging Tips
3. Using Docker BuildKit
Docker's BuildKit allows advanced caching and diagnostics features. It can be enabled to provide a more informative build context:
- Enable BuildKit: Set the environment variable
DOCKER_BUILDKIT=1before running the build command:
- BuildKit Debugging: With BuildKit, you can leverage output features like logging the execution of build commands, which sometimes can expose where a command is failing, yielding a log that offers insight into potential filesystem issues.
Example Scenario
Let's consider a basic Dockerfile that fails during its build:
Suppose the build fails at the last step (RUN python3 /app/script.py). You can identify and inspect the intermediate container that successfully executed up to the previous step using the methods described.
Summary Table
| Method | Description | Commands |
| Intermediate Containers | Run a shell in the last successful container | docker run -it <container_id> /bin/sh |
| Committing Container | Save container state to an image | docker commit <container_id> debug-image & run |
| Docker BuildKit | Enhanced logging and diagnostics | export DOCKER_BUILDKIT=1 & docker build . |
Additional Considerations
Managing Docker Resources
Pay attention to disk space usage as intermediate containers, images, and build caches can accumulate over time. Use docker system prune to clean up, but be cautious as it can remove both stopped containers and dangling images.
Multi-Stage Builds
For complex Dockerfiles, utilizing multi-stage builds can sometimes isolate issues. By segmenting the build process, each stage remains lighter and easier to debug.
Conclusion
Inspecting the filesystem on a failed docker build requires understanding and leveraging Docker's features like intermediate containers and leveraging tools like BuildKit. By mastering these techniques, developers can effectively diagnose build issues and maintain efficient Docker workflows.

