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.
More on Write-a-Dockerfile tasks
Write a production Dockerfile for a Node.js API using a multi-stage build.IntermediateWrite a Dockerfile for a Python Flask app that runs with gunicorn as a non-root user.BeginnerWrite a Dockerfile for a Go service that produces a tiny image (under 20 MB).IntermediateWrite a multi-stage Dockerfile for a Java Spring Boot app built with Maven.IntermediateWrite a Dockerfile for a static React site served by Nginx.BeginnerThis Dockerfile rebuilds dependencies on every code change. Fix it.Beginner