How do you keep the `.git` directory out of the context but still stamp the commit?
IntermediateProductionBuild Performance & CI
Answer
Ignore .git in .dockerignore and pass metadata as late ARGs.
ARG GIT_SHA=unknown
LABEL org.opencontainers.image.revision=$GIT_SHA
docker build --build-arg GIT_SHA=$(git rev-parse --short HEAD) .
Place it after expensive layers so cache is preserved.
More on Build Performance & CI
How do you make Docker builds fast in CI where runners are ephemeral?BeginnerWhat are the cache backends in buildx?BeginnerWhy does `inline` cache often disappoint?AdvancedHow do you structure a Python Dockerfile for the best caching and size?IntermediateHow do you structure a Node.js production image?IntermediateHow do you build a small Java image?Beginner