91

I have the following docker images.

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              48b5124b2768        2 months ago        1.84 kB
docker/whalesay     latest              6b362a9f73eb        22 months ago       247 MB

Is there a way I can see the Dockerfile of each docker image on my local system?

The answer at Where to see the Dockerfile for a docker image? does not help me because it does not exactly show the Dockerfile but the commands run to create the image. I want the Dockerfile itself.

Community
  • 1
  • 1
Lone Learner
  • 18,088
  • 20
  • 102
  • 200
  • duplicate of [Docker: Reverse Engineering of an Image](https://stackoverflow.com/questions/48228275/docker-reverse-engineering-of-an-image) – milahu Jan 19 '22 at 10:22

4 Answers4

72

As far as I know, no, you can't. Because a Dockerfile is used for building the image, it is not packed with the image itself. That means you should reverse engineer it. You can use docker inspect on an image or container, thus getting some insight and a feel of how it is configured. The layers an image are also visible, since you pull them when you pull a specific image, so that is also no secret.

However, you can usually see the Dockerfile in the repository of the image itself on Dockerhub. I can't say most repositories have Dockerfiles attached, but the most of the repositories I seen do have it.

Different repository maintainers may opt for different ways to document the Dockerfiles. You can see the Dockerfile tab on the repository page if automatic builds are set up. But when multiple parallel versions are available (like for Ubuntu), maintainers usually opt to put links the Dockerfiles for different versions in the description. If you take a look here: https://hub.docker.com/_/ubuntu/, under the "Supported tags" (again, for Ubuntu), you can see there are links to multiple Dockerfiles, for each respective Ubuntu version.

Aleksandar Stojadinovic
  • 4,851
  • 1
  • 34
  • 56
  • 11
    See the answer by @themozel down below. Nowadays you can use the `docker history` command to see how an image was built. – a544jh Sep 02 '21 at 11:37
  • @a544jh That command just shows the commands inside the docker image, which are NOT the same commands from the Dockerfile. They are similar and sometimes they map 1:1, but most of the time they don't. – Bitcoin Cash - ADA enthusiast Jul 18 '22 at 13:21
17

As the images are downloaded directly from the Dockerhub, only the image is pulled from the docker hub into your machine. 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. Otherwise not. Most official images will not provide you with Dockerfile.

Hope it helps!

Jayabalan Bala
  • 997
  • 1
  • 9
  • 16
  • 2
    If I go to https://hub.docker.com/search/?q=ubuntu%3A14.04 I can't see an official ubuntu:14.04 image. Instead I see ubuntu 14.04 images created by other users. How do I search for the official ubuntu:14.04 image? – Lone Learner Apr 10 '17 at 01:36
  • 1
    It is a bit different for different repos. You can see the Dockerfile tab in the repository if builds are set up. But when multiple parallel versions are available, maintainers usually opt to list the Dockerfiles for different versions in the readme. If you take a look here: https://hub.docker.com/_/ubuntu/, under the "Supported tags", you can see there are links to multiple Dockerifiles, for each respective Ubuntu version. Edit: I'll add this to my answer. – Aleksandar Stojadinovic Apr 10 '17 at 08:58
  • Hi @LoneLearner ... Here is the official ubuntu repo contains all the versions released by Ubuntu. https://hub.docker.com/_/ubuntu/ The Dockerfile can be found on: https://github.com/tianon/docker-brew-ubuntu-core/blob/1a5cb40f41ac4829d8c301ccd2cf3b7a13687a8b/trusty/Dockerfile As you can see in the official ubuntu page, Docker file likns to github are given – Jayabalan Bala Apr 11 '17 at 03:12
  • *"Most official images will not provide you with Dockerfile."* , isn't this wrong? As @AleksandarStojadinovic said, I can see Dockerfiles of many offical images via the link under the "Supported tags". – Rick May 14 '22 at 15:46
15

You can also regenerate the dockerfile from an image or use the docker history <image name> command to see what is inside. check this: Link to answer

TL;DR So if you have a docker image that was built by a dockerfile, you can recover this information (All except from the original FROM command, which is important, I’ll grant that. But you can often guess it, especially by entering the container and asking “What os are you?”). However, the maker of the image could have manual steps that you’d never know about anyways, plus they COULD just export an image, and re-import it and there would be no intermediate images at that point.

Tyler Collier
  • 11,489
  • 9
  • 73
  • 80
themozel
  • 312
  • 3
  • 11
  • 4
    The link might answer the question but also could change sometime in the future. So please add the relevant part of the linked document here to your answer to make it persistent and findable via search. Thank you. :) – Markus Aug 24 '20 at 10:12
6

One approach could be to save the image in a image.tar file. Next extract the file and try to explore if you can find Dockerfile in any of the layer directories.

docker image save -o hello.tar hello-world

This will output a hello.tar file.

hello.tar is the compressed output image file and hello-world is the name of the image you are saving.

After that, extract the compressed file and explore the image layer directories. You may find Dockerfile in one of the directories.

However, there is one thing to note, if the image was built while ignoring the Dockerfile in the .dockerignore. Then you will not find the Dockerfile by this approach.

Faizi
  • 410
  • 1
  • 8
  • 13