
How to view or recreate a Dockerfile from an existing Docker image
Table of contents
Quick Access

When working with Docker images, it's common to need access to the original Dockerfile to understand how a container was built, troubleshoot issues, replicate an environment, or improve an existing configuration. However, Docker images do not typically include the original Dockerfile, making this task less straightforward than many developers expect.
In this guide, you'll learn how to inspect a Docker image and recreate its Dockerfile using tools such as docker history, image layer analysis, and source repository lookups. We'll also cover the limitations of reverse-engineering Docker images and the best practices for recovering build information as accurately as possible.

How to Find the Dockerfile of an Image?
It is not possible to directly view the original Dockerfile of an image unless the author has made it public (for example, on Docker Hub or in a GitHub repository). However, you can partially reconstruct the steps used to create the image using command-line tools.
1. Use docker history to inspect the image
This command shows the image's layer history and the commands used to build it:
docker history --no-trunc image-name
You will see instructions like RUN, COPY, or CMD, although in a simplified form. This does not generate a complete Dockerfile, but it gives you a good idea of how the image was built.
2. Explore the image with dive
Dive is an open-source tool that allows you to explore each layer of the image, view the added files, and analyze the size of each change.
Install and run it like this:
dive image-name
It’s ideal for understanding how an image is structured or auditing third-party images.
3. Search for the source repository
Many images on Docker Hub include links to their GitHub repository, where they often publish the original Dockerfile.
For example, the official NGINX image on Docker Hub: https://hub.docker.com/_/nginx points to this repository: https://github.com/nginxinc/docker-nginx
There you can find the Dockerfile in the root directory or in folders like /docker.

Important
Although you can reconstruct most of the build steps of an image, there are things you won’t be able to recover, such as:
- Dockerfile comments
- Values of
ARGused during build time - Files ignored by
.dockerignore - Sensitive data or secrets
Summary
To find or reconstruct the Dockerfile of an image:
- Use
docker historyto view commands by layer - Explore with
diveto understand the contents of each layer - Check Docker Hub or GitHub for the original Dockerfile if available
Do you need help auditing existing images, migrating containers, or designing a more efficient and secure Dockerfile? At Rootstack, we help development teams modernize their Docker infrastructure and optimize their DevOps workflows. Let’s talk today.
We recommend this video
Related blogs


AI Mobile App Development Services: Use Cases Across Industries
