3

I am working on a Linux machine. I built a Docker image around 3-4 weeks ago but I don't remember where the Dockerfile is located.

What's the best way to locate the Dockerfile? Is it possible to somehow get its location given the image?

I tried using:

$ docker image inspect <image>

but it does not show this information.

Paolo
  • 21,270
  • 6
  • 38
  • 69

2 Answers2

4

Docker image doesn't store any information on Dockerfile, but you can try to reverse engineer it. There's few approaches to that:

  1. Use docker history <image-name> --no-trunc, it will display information on each layer:
> docker history golang:1.14.2-alpine3.11  --no-trunc --format="{{.CreatedBy}}"
/bin/sh -c #(nop) WORKDIR /go
/bin/sh -c mkdir -p "$GOPATH/src" "$GOPATH/bin" && chmod -R 777 "$GOPATH"
/bin/sh -c #(nop)  ENV PATH=/go/bin:/usr/local/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
/bin/sh -c #(nop)  ENV GOPATH=/go
...

  1. https://github.com/CenturyLinkLabs/dockerfile-from-image -- project for reverse engineering Dockerfile from docker image. You can run it like that:
docker run -v /var/run/docker.sock:/var/run/docker.sock \
  centurylink/dockerfile-from-image <IMAGE_TAG_OR_ID>
  1. dive is actually tool for debugging/optimizing docker image, but can be helpful in this situation
Grigoriy Mikhalkin
  • 5,035
  • 1
  • 18
  • 36
1

There is not going to be a direct link between the image and the physical file used to build that.

you can try searching for the dockerfile as if it were any other file.

find / -name "Dockerfile" -print

for example.

if you know more or less when the Dockerfile was last edited:

find / -newermt "2020-01-01" ! -newermt "2020-04-31"

etc.

if you don't know what the dockerfile is called, but you can remember a command in it. find as grep:

find . -type f -exec grep "FROM:mongo:latest" '{}' \; -print

This is all standard find stuff, and has nothing to do with docker in particular.

Now let's say this is still not working for you, or you have 1000s if dockerfiles returned and matching the correct one to your image is still problematic.

You can try and rebuild the dockerfile from the image. this answer: https://stackoverflow.com/a/30793515/7281447
or
this answer: https://stackoverflow.com/a/33918866/7281447
or this answer: https://stackoverflow.com/a/30793515/7281447

describe methods of doing this. But you are 'reverse engineering' the dockerfile from the image. i.e. some information may be overly verbose or lost completely. It's not going to generate the clean, (well commented!) dockerfile you used to create your image in the first place.

However now you have a list of historical commands that were used to generate the image. You can try cherry picking unique looking commands from this list, and plugging them back into your find w/ grep command. I would start with latest commands first, as you're less likely to be looking at those commands which may have been generated within a FROM image (if your tool cannot distinguish between them).

Edit: when question states they don't know where file is located, answer assumes it's still in the file system somewhere, or in a group of known file systems. I suppose that's not necessarily the case.

baku
  • 765
  • 8
  • 22