Adding parameters to Docker build in AWS CDK

August 2023

We’re deploying CDK from GitHub Actions. Multiple Docker images get built on the Actions runner in the process. We need to change the docker build command that CDK runs to enable caching.

The CDK_DOCKER environment variable controls the command CDK runs.

A wrapper script intercepts any docker build calls and injects the additional parameters. Other calls pass through unscathed.

#!/bin/bash

# If the first argument is 'build', we manipulate the parameters before passing them on to docker.
if [ "$1" = "build" ]; then
    docker_command="docker buildx $@ --cache-to type=gha,mode=max --cache-from type=gha --load"
# Otherwise, we pass on all arguments to docker.
else
    docker_command="docker $@"
fi

echo "Wrapped: $docker_command"
eval "$docker_command"

CDK_DOCKER=docker_wrapper.sh cdk deploy glues it all together.