Skip to main content

🛠️ Dockerfile From Image (dfimage)

GitHub Repo Size GitHub Code Size in Bytes GitHub Last Commit GitHub Commit Activity

📑 Table of Contents

🎯 Purpose

Reverse-engineers a Dockerfile from a Docker image.

See my Inspiration and Container Source for more information.

Similar to how the docker history command works, the Python script is able to re-create the Dockerfile (approximately) that was used to generate an image using the metadata that Docker stores alongside each image layer.

⚡ Usage

Codacy Badge codecov

The Python script is itself packaged as a Docker image so it can easily be executed with the Docker run command:

docker run -v /var/run/docker.sock:/var/run/docker.sock dfimage ruby:latest

The ruby:latest parameter is the image name & tag (either the truncated form or the complete image name & tag).

Since the script interacts with the Docker API in order to query the metadata for the various image layers it needs access to the Docker API socket. The -v flag shown above makes the Docker socket available inside the container running the script.

Note that the script only works against images that exist in your local image repository (the stuff you see when you type docker images). If you want to generate a Dockerfile for an image that doesn't exist in your local repo you'll first need to docker pull it.

You can find more usage examples in the Wiki.

🐍 Alternative: pip install dfimage

To install the dfimage package using pip(x), run the following command:

pipx install dfimage

Note that Docker must be installed on your system for the dfimage package to work correctly.

🧪 Test

dfimage --help

🐋 Docker Example

docker-publish docker-hub

Here's an example that shows the official Docker ruby image being pulled and the Dockerfile for that image being generated. Note: A docker tag is required for correct functionality.

docker pull ruby:latest
docker pull ghcr.io/laniksj/dfimage
alias dfimage="docker run -v /var/run/docker.sock:/var/run/docker.sock --rm ghcr.io/laniksj/dfimage"
dfimage ruby:latest

🔍 How Does It Work

When an image is constructed from a Dockerfile, each instruction in the Dockerfile results in a new layer. You can see all of the image layers by using the docker images command with the (now deprecated) --tree flag.

docker images --tree

Each one of these layers is the result of executing an instruction in a Dockerfile. In fact, if you do a docker inspect on any one of these layers you can see the instruction that was used to generate that layer.

⚠️ Limitations

As the Python script walks the list of layers contained in the image it stops when it reaches the first tagged layer. It is assumed that a layer which has been tagged represents a distinct image with its own Dockerfile so the script will output a FROM directive with the tag name.

In the example above, the ruby image contained a layer in the local image repository which had been tagged with buildpack-deps (though it wasn't shown in the example, this likely means that buildpack-deps:latest was also pulled at some point). If the buildpack-deps layer had not been tagged, the Python script would have continued outputting Dockerfile directives until it reached the root layer.

Also note that the output generated by the script won't match exactly the original Dockerfile if either the COPY or ADD directives (like the example above) are used. Since we no longer have access to the build context that was present when the original docker build command was executed all we can see is that some directory or file was copied to the image's filesystem (you'll see the file/ directory checksum and the destination it was copied to).

🐛 Bugs

Please report any bugs or issues you find. Thanks!

📝 License

MIT License

💰 Donate

Patreon

Metadata

Release files for dfimage 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dfimage 1.1.0
File Size Uploaded Provenance Actions
dfimage-1.1.0.tar.gz 6.5 kB With provenance Details for dfimage-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for dfimage 1.1.0
File Interpreter ABI Platform Actions
dfimage-1.1.0-py3-none-any.whl Python 3 none any Details for dfimage-1.1.0-py3-none-any.whl

Total release size: 13.3 kB

Release files / dfimage-1.1.0.tar.gz

Download URL dfimage-1.1.0.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
04c1b2035a5555b199610e2cb29b9283e0e70ce4bfa4b34175c0c83effdda448
BLAKE2b-256 checksum
How to use checksums
96588ee28e78322e8d0883f384b74479e9b0c3369348dfcedf76d3538817e3eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 5, 2026.

Transparency log

Release files / dfimage-1.1.0-py3-none-any.whl

Download URL dfimage-1.1.0-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
910dc3cf986abea270ee8e09809f0135baa909027204b47e5eb8ab2531504f61
BLAKE2b-256 checksum
How to use checksums
7ea8fe0266ff7ba21519a5f44e1266bb02d90ec8ae5669f822d4589bcd4a8f82
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 5, 2026.

Transparency log

Security for dfimage 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for dfimage-1.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 5, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page