Skip to content

Instantly share code, notes, and snippets.

@webartoli
Created September 6, 2021 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webartoli/3fe6a4bb7eccb981720fc5dc6921478d to your computer and use it in GitHub Desktop.
Save webartoli/3fe6a4bb7eccb981720fc5dc6921478d to your computer and use it in GitHub Desktop.
Generate deps graph from multistage Dockerfile
#!/usr/bin/env bash
function extract_key_value() {
egrep -o "$1[:=]['\"[:alnum:]\-]+" | egrep -o "['\"[:alnum:]\-]+$" | egrep -o "[[:alnum:]\-]+"
}
function docker_multistage_deps() {
cat Dockerfile \
| grep -i 'FROM ' \
| grep -i ' AS ' \
| awk '{print $2 " --> " $4}'
}
function docker_copy_between_images_deps() {
cat Dockerfile \
| grep -i 'from' \
| while read line
do
if [[ $line == COPY* ]]
then
source=`echo $line | extract_key_value from`
target=`echo $context | awk '{print $4}'`
echo "$source -.- $target "
fi
if [[ $line == FROM* ]]
then
context=$line
fi
done
}
echo '``` mermaid'
echo "graph LR"
echo ""
docker_multistage_deps
echo ""
docker_copy_between_images_deps
echo '```'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment