Junior — Middle
What is the difference between ARG and ENV variables in a Dockerfile and how are they used?
sobes.tech AI
Answer from AI
In a Dockerfile, the variables ARG and ENV serve different purposes:
-
ARG— these are variables available only during the image build process. They can be set during build withdocker build --build-arg. After the build, they are not stored in the image. -
ENV— these are environment variables that are stored in the image and available during container runtime.
Example usage:
ARG VERSION=1.0
FROM python:${VERSION}
ENV APP_ENV=production
Here, VERSION sets the Python version at build time, and APP_ENV will be available inside the container when it runs.
Summary:
ARG— for build parameters, not stored in the final image.ENV— for environment variables, available in the container after startup.