I'm just starting out with Docker, and it would be very helpful to be able to see the Dockerfiles used to create existing docker images.
Even if the image was built by running commands manually, and then committing to a tag, it would be nice to be able to see how the image was made, both for learning purposes and for security.
Is there a way to extract a Dockerfile or list of commands used to build a given docker image?
Edit (November 2021): Since people are still upvoting this, I can say that based on the answers and comments, I settled on:
docker history --no-trunc --format '{{.CreatedBy}}' <image> | grep -v '#(nop)' | tac
It produces output that is easy to put in a Dockerfile. Example:
$ docker history --no-trunc --format '{{.CreatedBy}}' qemu | grep -v '#(nop)' | tac ENV DEBIAN_FRONTEND=noninteractive RUN /bin/sh -c apt-get update && apt-get -y upgrade # buildkit RUN /bin/sh -c apt install -y qemu-system-arm gcc-arm-none-eabi build-essential cmake bison flex # buildkit RUN /bin/sh -c useradd --create-home qemu # buildkit WORKDIR /home/qemu USER qemu COPY baremetal-arm baremetal-arm # buildkit
But as I also wrote, I don't think there is a good way to extract the Dockerfile, so if you need it, and can't find the source code, maybe give the image a pass.
If you want to see the dockerfile, then you can go to docker hub and type the image name and version name in the tag format (e.g ubuntu:14.04) this will open the image along with Docker file details. Also keep in mind, only if the owner of the image shared their Dockerfile, you can see it.
You can use COPY or ADD command within Dockerfile to copy your file/code into the Docker container. The following Dockerfile example shows how to add the current working directory files and folders into the directory /usr/Qxf2_POM of the container image.
In order to extract image contents without dealing with many layers, a container should be created first. If docker run was already run, use that container, otherwise create a stopped container with docker create . Then use docker export or docker cp .
A Dockerfile is a text document (without a file extension) that contains the instructions to set up an environment for a Docker container. You can build a Docker image using a Dockerfile. The command docker build . builds a Docker image using the Dockerfile in the directory that this command is executed.
You have docker history <image>
that is very helpful. It can even be used to generate a dockerfile if none of the steps involved stdin.
If a step as stdin, the only way to know what happened would be to do docker logs <container id parent>
, but if you do not have the container, you can't.
There is undocker available now. We can install it by using the pip command.
pip install git+https://github.com/larsks/undocker/
and use
docker save IMAGE_NAME | undocker -i -o OUTPUT_DIR
to extract the files from docker.
https://github.com/larsks/undocker/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With