Skip to content

Instantly share code, notes, and snippets.

@scentoni
Created October 27, 2021 17:28
Show Gist options
  • Save scentoni/5da5b4856551323f87462257d350c991 to your computer and use it in GitHub Desktop.
Save scentoni/5da5b4856551323f87462257d350c991 to your computer and use it in GitHub Desktop.
generate a dockerfile from an image
#!/bin/bash
# https://stackoverflow.com/questions/19104847/how-to-generate-a-dockerfile-from-an-image
case "$OSTYPE" in
linux*)
docker history --no-trunc --format "{{.CreatedBy}}" $1 | # extract information from layers
tac | # reverse the file
sed 's,^\(|3.*\)\?/bin/\(ba\)\?sh -c,RUN,' | # change /bin/(ba)?sh calls to RUN
sed 's,^RUN #(nop) *,,' | # remove RUN #(nop) calls for ENV,LABEL...
sed 's, *&& *, \\\n \&\& ,g' # pretty print multi command lines following Docker best practices
;;
darwin*)
docker history --no-trunc --format "{{.CreatedBy}}" $1 | # extract information from layers
tail -r | # reverse the file
sed -E 's,^(\|3.*)?/bin/(ba)?sh -c,RUN,' | # change /bin/(ba)?sh calls to RUN
sed 's,^RUN #(nop) *,,' | # remove RUN #(nop) calls for ENV,LABEL...
sed $'s, *&& *, \\\ \\\n \&\& ,g' # pretty print multi command lines following Docker best practices
;;
*)
echo "unknown OSTYPE: $OSTYPE"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment