Discover how simple variables can save you hours of frustrating container rebuilds!
Why ARG and ENV instructions in Docker? - Purpose & Use Cases
Imagine you build a software container by typing in all the settings and values every single time you create it.
You have to remember and type the same details like server names, passwords, or versions again and again.
This manual way is slow and easy to mess up.
One wrong value can break your container or cause security risks.
It's hard to share or update these settings without mistakes.
Using ARG and ENV instructions in Dockerfiles lets you set variables once and reuse them easily.
ARG is for build-time values, ENV is for runtime settings.
This makes your containers flexible, safer, and easier to manage.
RUN echo "version=1.0" > config.txt RUN echo "password=1234" > secrets.txt
ARG VERSION=1.0 ENV PASSWORD=1234 RUN echo "version=$VERSION" > config.txt RUN echo "password=$PASSWORD" > secrets.txt
You can build and run containers with different settings quickly and safely without changing the Dockerfile every time.
A developer can build the same app container for testing, staging, and production by just changing ARG or ENV values, not the whole Dockerfile.
Manual setting of values is slow and error-prone.
ARG and ENV let you define variables for build and runtime.
This makes container builds flexible, reusable, and safer.
Practice
ARG and ENV instructions in a Dockerfile?Solution
Step 1: Understand
ARGusageARGdefines variables that are only available during the image build process. They cannot be accessed after the image is built.Step 2: Understand
ENVusageENVsets environment variables that persist in the built image and are available at runtime inside containers.Final Answer:
ARGdefines build-time variables,ENVdefines runtime environment variables. -> Option AQuick Check:
ARG= build-time,ENV= runtime [OK]
- Confusing ARG as runtime variable
- Thinking ENV variables are only for build-time
- Assuming ARG persists in the final image
- Mixing up the purpose of ARG and ENV
VERSION with a default value 1.0 in a Dockerfile?Solution
Step 1: Recall ARG syntax
The correct syntax to declare a build argument with a default value isARG name=value. SoARG VERSION=1.0is valid.Step 2: Check other options
ENVis for environment variables, not build arguments. The colon syntax is invalid in Dockerfile.Final Answer:
ARG VERSION=1.0 -> Option CQuick Check:
ARG syntax uses equals sign (=) [OK]
- Using ENV instead of ARG for build arguments
- Using colon (:) instead of equals (=)
- Omitting the default value syntax
- Confusing ARG and ENV syntax
ARG APP_VERSION=2.0 ENV APP_VERSION=$APP_VERSION RUN echo $APP_VERSION
What will be the output of the
RUN echo $APP_VERSION command during build if no build argument is passed?Solution
Step 1: Understand ARG default value
ARG APP_VERSION=2.0sets a default build argument value of "2.0" if none is passed.Step 2: ENV uses ARG value
ENV APP_VERSION=$APP_VERSIONsets an environment variable using the ARG value, so it becomes "2.0".Step 3: RUN command output
RUN echo $APP_VERSIONprints the environment variable value "2.0" during build.Final Answer:
2.0 -> Option DQuick Check:
ARG default used in ENV, echoed as 2.0 [OK]
- Expecting empty output if no build arg passed
- Thinking $APP_VERSION is literal string
- Assuming error if variable not passed
- Confusing build-time and runtime variable scopes
ARG PORT ENV PORT=8080 EXPOSE $PORT
Solution
Step 1: Check variable usage in EXPOSE
TheEXPOSEinstruction does not support variable expansion. Using$PORTwill not work.Step 2: Validate other statements
Not giving a default toARG PORTis allowed.ENVcan overrideARG. The order ofARGandENVis not restricted.Final Answer:
You cannot use variables in theEXPOSEinstruction. -> Option AQuick Check:
EXPOSE does not expand variables [OK]
- Assuming EXPOSE supports variable expansion
- Thinking ARG must have default value
- Believing ENV must come after ARG
- Confusing build-time and runtime variable scopes
APP_ENV with default production and sets an environment variable APP_ENV inside the container accordingly. Which Dockerfile snippet correctly achieves this?Solution
Step 1: Set ARG with default value
ARG APP_ENV=productiondefines the build argument with default "production" if none is passed.Step 2: Set ENV using ARG value
ENV APP_ENV=$APP_ENVsets the runtime environment variable to the ARG value, so it reflects the build argument or default.Step 3: Validate other options
ENV APP_ENV=production ARG APP_ENV ENV APP_ENV=$APP_ENV sets ENV before ARG which is invalid. ARG APP_ENV ENV APP_ENV=production does not set default for ARG. ENV APP_ENV=$APP_ENV ARG APP_ENV=production uses ENV before ARG which is incorrect.Final Answer:
ARG APP_ENV=production ENV APP_ENV=$APP_ENV -> Option BQuick Check:
ARG default then ENV from ARG [OK]
- Setting ENV before ARG
- Not providing default value for ARG
- Overwriting ENV without ARG reference
- Confusing build-time and runtime variable order
Ready to prove it?
Pass the test and this pattern moves unknown
