What is the purpose of the ARG instruction in a Dockerfile?
ARG defines a variable that users can pass at build time to customize the image build process. It is only available during the build and not in the final container.
Click to reveal answer
beginner
How does the ENV instruction differ from ARG in Dockerfiles?
ENV sets environment variables that are available both during build and in the running container. These variables persist in the final image.
Click to reveal answer
intermediate
Can you use an ARG variable to set an ENV variable in a Dockerfile? How?
Yes. You can use ARG to get a build-time value and then assign it to an ENV variable like this:
ARG VERSION=1.0
ENV APP_VERSION=$VERSION
Click to reveal answer
beginner
What happens if you try to access an ARG variable in the running container?
ARG variables are not available in the running container. They only exist during the image build process and are discarded afterward.
Click to reveal answer
intermediate
Why is it useful to use ARG for sensitive data during build?
ARG variables are not saved in the final image, so they help keep sensitive data like passwords or tokens from being exposed in the running container.
Click to reveal answer
Which Dockerfile instruction sets a variable only available during build time?
AARG
BENV
CRUN
DCMD
✗ Incorrect
ARG defines build-time variables, while ENV sets variables available at runtime.
If you want an environment variable to be available inside the running container, which instruction should you use?
ALABEL
BARG
CFROM
DENV
✗ Incorrect
ENV sets environment variables that persist in the container at runtime.
How can you pass a value to an ARG variable when building a Docker image?
Adocker build --env VAR=value
Bdocker build --build-arg VAR=value
Cdocker run -e VAR=value
Ddocker run --build-arg VAR=value
✗ Incorrect
Use --build-arg to pass values to ARG variables during docker build.
What is the default value of an ARG variable if not passed during build?
AThe value set in the Dockerfile after ARG
BUndefined and causes error
CEmpty string
DThe value of ENV variable with the same name
✗ Incorrect
ARG can have a default value in the Dockerfile, used if no build-arg is passed.
Which statement is true about ENV variables in Docker?
AThey are only available during build time.
BThey are discarded after the image is built.
CThey persist in the image and are available at runtime.
DThey cannot be overridden at container start.
✗ Incorrect
ENV variables persist in the image and are accessible when the container runs.
Explain the difference between ARG and ENV instructions in Dockerfiles.
Think about when and where each variable is available.
You got /4 concepts.
Describe how you can use ARG and ENV together to pass a build-time value into the running container.
Use ARG to get the value, then ENV to keep it.
You got /3 concepts.
Practice
(1/5)
1. What is the main difference between ARG and ENV instructions in a Dockerfile?
easy
A. ARG defines build-time variables, ENV defines runtime environment variables.
B. ARG defines runtime environment variables, ENV defines build-time variables.
C. ARG and ENV are exactly the same and interchangeable.
D. ARG is used to set default ports, ENV sets user permissions.
Solution
Step 1: Understand ARG usage
ARG defines variables that are only available during the image build process. They cannot be accessed after the image is built.
Step 2: Understand ENV usage
ENV sets environment variables that persist in the built image and are available at runtime inside containers.
Hint: Remember: ARG is build-time, ENV is runtime [OK]
Common Mistakes:
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
2. Which of the following is the correct syntax to declare a build argument named VERSION with a default value 1.0 in a Dockerfile?
easy
A. ARG VERSION : 1.0
B. ENV VERSION=1.0
C. ARG VERSION=1.0
D. ENV VERSION : 1.0
Solution
Step 1: Recall ARG syntax
The correct syntax to declare a build argument with a default value is ARG name=value. So ARG VERSION=1.0 is valid.
Step 2: Check other options
ENV is for environment variables, not build arguments. The colon syntax is invalid in Dockerfile.
Final Answer:
ARG VERSION=1.0 -> Option C
Quick Check:
ARG syntax uses equals sign (=) [OK]
Hint: ARG uses '=' to assign default values [OK]
Common Mistakes:
Using ENV instead of ARG for build arguments
Using colon (:) instead of equals (=)
Omitting the default value syntax
Confusing ARG and ENV syntax
3. Given the Dockerfile snippet:
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?
medium
A. Error: variable not found
B. $APP_VERSION
C. empty line
D. 2.0
Solution
Step 1: Understand ARG default value
ARG APP_VERSION=2.0 sets a default build argument value of "2.0" if none is passed.
Step 2: ENV uses ARG value
ENV APP_VERSION=$APP_VERSION sets an environment variable using the ARG value, so it becomes "2.0".
Step 3: RUN command output
RUN echo $APP_VERSION prints the environment variable value "2.0" during build.
Final Answer:
2.0 -> Option D
Quick Check:
ARG default used in ENV, echoed as 2.0 [OK]
Hint: ARG default flows into ENV if no override [OK]
Common Mistakes:
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
4. Identify the error in this Dockerfile snippet:
ARG PORT
ENV PORT=8080
EXPOSE $PORT
medium
A. You cannot use variables in the EXPOSE instruction.
B. The ENV variable PORT overrides ARG but EXPOSE uses $PORT which is not expanded.
C. The ARG variable PORT is not given a default value.
D. The ENV instruction must come before ARG.
Solution
Step 1: Check variable usage in EXPOSE
The EXPOSE instruction does not support variable expansion. Using $PORT will not work.
Step 2: Validate other statements
Not giving a default to ARG PORT is allowed. ENV can override ARG. The order of ARG and ENV is not restricted.
Final Answer:
You cannot use variables in the EXPOSE instruction. -> Option A
Quick Check:
EXPOSE does not expand variables [OK]
Hint: EXPOSE needs literal port numbers, no variables allowed [OK]
Common Mistakes:
Assuming EXPOSE supports variable expansion
Thinking ARG must have default value
Believing ENV must come after ARG
Confusing build-time and runtime variable scopes
5. You want to build a Docker image that accepts a build argument APP_ENV with default production and sets an environment variable APP_ENV inside the container accordingly. Which Dockerfile snippet correctly achieves this?
hard
A. ENV APP_ENV=production
ARG APP_ENV
ENV APP_ENV=$APP_ENV
B. ARG APP_ENV=production
ENV APP_ENV=$APP_ENV
C. ARG APP_ENV
ENV APP_ENV=production
D. ENV APP_ENV=$APP_ENV
ARG APP_ENV=production
Solution
Step 1: Set ARG with default value
ARG APP_ENV=production defines the build argument with default "production" if none is passed.
Step 2: Set ENV using ARG value
ENV APP_ENV=$APP_ENV sets 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 B
Quick Check:
ARG default then ENV from ARG [OK]
Hint: Declare ARG with default, then ENV using ARG variable [OK]
Common Mistakes:
Setting ENV before ARG
Not providing default value for ARG
Overwriting ENV without ARG reference
Confusing build-time and runtime variable order
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.