Skip to main content

Reverse-engineer a Dockerfile from a Docker Image

Project description

🛠️ 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dfimage-1.1.0.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dfimage-1.1.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file dfimage-1.1.0.tar.gz.

File metadata

  • Download URL: dfimage-1.1.0.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for dfimage-1.1.0.tar.gz
Algorithm Hash digest
SHA256 04c1b2035a5555b199610e2cb29b9283e0e70ce4bfa4b34175c0c83effdda448
MD5 a9f7eae271a3adbd5e407f7407b1e1e0
BLAKE2b-256 96588ee28e78322e8d0883f384b74479e9b0c3369348dfcedf76d3538817e3eb

See more details on using hashes here.

Provenance

The following attestation bundles were made for dfimage-1.1.0.tar.gz:

Publisher: pypi-publish.yml on LanikSJ/dfimage

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dfimage-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: dfimage-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for dfimage-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 910dc3cf986abea270ee8e09809f0135baa909027204b47e5eb8ab2531504f61
MD5 46231bae7d2c536152a960999303b54f
BLAKE2b-256 7ea8fe0266ff7ba21519a5f44e1266bb02d90ec8ae5669f822d4589bcd4a8f82

See more details on using hashes here.

Provenance

The following attestation bundles were made for dfimage-1.1.0-py3-none-any.whl:

Publisher: pypi-publish.yml on LanikSJ/dfimage

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page