Last active
February 28, 2020 20:34
-
-
Save Mahoney/85e8549892e0ae5bb86ce85339db1a71 to your computer and use it in GitHub Desktop.
An example maintaining a cache in an image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -euo pipefail | |
IFS=$'\n\t' | |
function clear_build_cache { | |
docker builder prune -f > /dev/null | |
} | |
function clean_start { | |
rm -f appended.txt | |
rm -f Dockerfile | |
docker image rm -f cache_image app > /dev/null | |
clear_build_cache | |
} | |
function bootstrap_cache { | |
docker pull bash | |
docker tag bash cache_image | |
} | |
function create_docker_file { | |
# shellcheck disable=SC2016 | |
# shellcheck disable=SC1004 | |
echo \ | |
'FROM cache_image as builder | |
ARG TO_APPEND=1 | |
RUN mkdir -p /cached_dir && \ | |
touch /cached_dir/appended.txt && \ | |
echo $TO_APPEND >> /cached_dir/appended.txt && \ | |
mkdir -p /artifact_dir && \ | |
cp /cached_dir/appended.txt /artifact_dir/appended.txt | |
FROM cache_image as cache | |
COPY --from=builder /cached_dir /cached_dir | |
FROM bash as app | |
COPY --from=builder /artifact_dir/appended.txt /artifact_dir/appended.txt | |
' > ./Dockerfile | |
} | |
function docker_build { | |
local to_append=$1 | |
# export DOCKER_BUILDKIT=1 | |
# export BUILDKIT_PROGRESS=plain | |
docker build \ | |
. \ | |
-t app \ | |
--build-arg TO_APPEND="$to_append" | |
docker build \ | |
. \ | |
--target cache \ | |
-t cache_image \ | |
--build-arg TO_APPEND="$to_append" | |
} | |
function assert_results { | |
local expected=$1 | |
local id | |
id=$(docker create app) | |
docker cp "$id":/artifact_dir/appended.txt appended.txt | |
docker rm -v "$id" > /dev/null | |
results=$(cat appended.txt) | |
echo "$results" | |
total_lines="$(wc -l < appended.txt | tr -d ' ')" | |
if [[ "$total_lines" != "$expected" ]]; then | |
>&2 echo "Expected $expected lines, got $total_lines!" | |
exit 1; | |
fi | |
} | |
function main { | |
clean_start | |
bootstrap_cache | |
create_docker_file '' | |
echo | |
echo "Build 1" | |
docker_build "$(date "+%s")" | |
echo | |
echo | |
echo "Build 2" | |
docker_build "$(date "+%s")" | |
echo | |
echo "After 2 runs, no buildkit cache clear, there should be 2 lines in the file" | |
assert_results 2 | |
clear_build_cache | |
echo | |
echo "build 3" | |
docker_build "$(date "+%s")" | |
echo | |
echo "After a buildkit cache clear & a further run there should be 3 lines in the file because the previous state should have come from the image cache" | |
assert_results 3 | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment