Rebuilding OS packages and reinstalling hundreds of megabytes of node_modules on every commit slows CI pipelines down fast. Splitting the build into three layered Dockerfiles (base, builder, and app) and tagging the base and builder images by content checksums lets most CI runs skip straight past unchanged layers.
Layering the checksums
Compute content hashes for each layer plus a monthly timestamp (MM-YYYY) so base images still pick up OS security patches at least once a month even if Dockerfile.base did not change:
export TIMESTAMP=$(date +%m)-$(date +%Y)
export C_BASE=$(md5sum Dockerfile.base | cut -d ' ' -f 1)
export C_PKG=$(cat package.json yarn.lock | md5sum | cut -d ' ' -f 1)
export C_BUILDER=$(md5sum Dockerfile.builder | cut -d ' ' -f 1)
export CHECKSUM_BASE=$(echo -n "$C_BASE-$TIMESTAMP" | md5sum | cut -d ' ' -f 1)
export CHECKSUM_BUILDER=$(echo -n "$C_PKG-$CHECKSUM_BASE-$C_BUILDER" | md5sum | cut -d ' ' -f 1)
- If
Dockerfile.basechanges (or a new month rolls over),CHECKSUM_BASEchanges, which rebuilds base and cascades intoCHECKSUM_BUILDER. - If
package.jsonoryarn.lockchanges,CHECKSUM_BUILDERchanges, skipping the base rebuild and rebuilding only the dependency image. - If only application code changed, CI checks
docker manifest inspectagainst the registry, seesbase-$CHECKSUM_BASEandbuilder-$CHECKSUM_BUILDERalready exist, and skips right to building the app image.
Adding a merge-request label escape hatch (like no_cache swapping the monthly stamp for date) also gives developers a quick way to force a clean rebuild whenever needed.