Interview Prep/Real-World Interview/Write-a-Dockerfile tasks

Explain and use `ARG` vs `ENV` in a Dockerfile to stamp a version into the image.

IntermediateHands-onWrite-a-Dockerfile tasks
Answer
FROM alpine:3.20
ARG VERSION=dev
ARG GIT_SHA=unknown
LABEL org.opencontainers.image.version=$VERSION \
      org.opencontainers.image.revision=$GIT_SHA
ENV APP_VERSION=$VERSION
docker build --build-arg VERSION=1.4.2 --build-arg GIT_SHA=$(git rev-parse --short HEAD) .

Key trick → ARG exists only at build time; copying it into ENV keeps it at runtime. OCI labels let docker inspect show which commit an image came from.

All Real-World Interview questions Roadmap: Interview readiness

More on Write-a-Dockerfile tasks