Junior — Middle
How can parameters or variables be passed when creating a Docker image during its build?
sobes.tech AI
Answer from AI
When building a Docker image, parameters or variables can be passed using the ARG instruction in the Dockerfile and the --build-arg key when running the docker build command.
Example:
# Dockerfile
ARG APP_VERSION=latest
FROM myapp:${APP_VERSION}
Building with a parameter:
docker build --build-arg APP_VERSION=1.2.3 -t myapp:1.2.3 .
Thus, ARG declares a variable that can be set during build time, and --build-arg passes a specific value. ARG variables are only available during build and are not saved in the final image.
To pass variables that will be available in the container during runtime, use ENV.