Sometimes we don't have Dockerfile but have only docker images so we might need to inspect the commands or steps that were run when building the image. Despite the fact that Docker doesn't store the Dockerfile directly inside the image, you can extract the image's build history using the docker history command and rebuild it.
The docker history command displays the history of an image. To get the full details of each layer, you can use the --no-trunc option.
docker history <image-name> --no-trunc
Explanation of the Command
The command produces a table with the following columns:
Example output:
IMAGE CREATED CREATED BY SIZE COMMENT
<image-id> 10 minutes ago /bin/sh -c apt-get update && apt-get install... 45MB
<image-id> 12 minutes ago /bin/sh -c echo "Hello, World!" 0B
<image-id> 15 minutes ago /bin/sh -c #(nop) ADD file:abcd1234abcd1234... 120MB
Steps to Recreate the Dockerfile
Analyze the CREATED BY column to identify the commands used.
Start with the base image and layer the commands in order.
Combine the commands to reconstruct a Dockerfile.
Example reconstructed Dockerfile:
FROM ubuntu:20.04
RUN apt-get update && apt-get install -y curl
RUN echo "Hello, World!"
ADD file:abcd1234abcd1234 /app
By leveraging docker history, you can gain valuable insights into Docker images and streamline your workflow.
Ready to transform your business with our technology solutions? Contact Us today to Leverage Our DevOps Expertise.
0