Sobes.tech
Junior — Middle

What method can be used to pass variables that change during the Docker image build?

sobes.tech AI

Answer from AI

Variables that change during Docker image build can be passed using build arguments (ARG). In the Dockerfile, an argument is declared with the ARG instruction, and during build, values are passed with the --build-arg flag.

Example:

FROM alpine
ARG APP_VERSION
RUN echo "Application version: $APP_VERSION"

Build with argument:

docker build --build-arg APP_VERSION=1.2.3 -t myapp:1.2.3 .

Important: ARG is only available during build and is not stored in the final image as an environment variable. If you want the variable to be available in the container, use ENV or pass it as an environment variable during container run.