0
0
Dockerdevops~10 mins

ARG and ENV instructions in Docker - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - ARG and ENV instructions
Start Dockerfile Build
ARG: Define build-time variable
Use ARG in Dockerfile
ENV: Define runtime environment variable
Use ENV in container runtime
Build completes with ARG values
Run container with ENV variables set
This flow shows how ARG sets variables during build time, and ENV sets variables available when the container runs.
Execution Sample
Docker
ARG APP_PORT=8080
ENV APP_ENV=production
RUN echo "Port is $APP_PORT"
CMD ["sh", "-c", "echo Running in $APP_ENV on port $APP_PORT"]
This Dockerfile snippet defines a build-time ARG and a runtime ENV, then uses them in commands.
Process Table
StepInstructionVariable SetValueUsageOutput/Effect
1ARG APP_PORT=8080APP_PORT8080Build-time variableAPP_PORT set to 8080 during build
2ENV APP_ENV=productionAPP_ENVproductionRuntime environment variableAPP_ENV set to production in container
3RUN echo "Port is $APP_PORT"N/AN/AUses ARG valueOutputs: Port is 8080
4CMD ["sh", "-c", "echo Running in $APP_ENV on port $APP_PORT"]N/AN/AUses ENV at runtime (ARG unavailable)When container runs, outputs: Running in production on port
5Build endsN/AN/AN/ABuild completes with ARG applied
6Container runsN/AN/AN/AENV variables available, ARG not available at runtime
💡 Build finishes after all instructions; ARG variables exist only during build, ENV variables persist in container runtime.
Status Tracker
VariableStartAfter ARGAfter ENVFinal
APP_PORTundefined808080808080 (build-time only)
APP_ENVundefinedundefinedproductionproduction (runtime)
Key Moments - 3 Insights
Why can't I use ARG variables inside the running container?
ARG variables exist only during the build steps (see execution_table step 1 and 6). They are not saved in the final image, so at runtime (step 6) they are not available.
Can ENV variables be changed during build time?
ENV variables are set during build and persist in the image (step 2). You can override them at container start, but inside the Dockerfile they remain constant after set.
What happens if I use an ARG variable in CMD without ENV?
ARG variables are not available at runtime (step 6), so using ARG directly in CMD will not work as expected. Use ENV to pass variables to runtime commands.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of APP_PORT during the RUN instruction?
Aproduction
Bundefined
C8080
Dnull
💡 Hint
Check step 3 in the execution_table where RUN uses APP_PORT.
At which step does the APP_ENV variable become available in the container runtime?
AStep 1
BStep 6
CStep 4
DStep 2
💡 Hint
Look at the exit_note and step 6 in execution_table about runtime availability.
If you remove the ENV instruction, what happens to APP_ENV in the running container?
AIt becomes undefined
BIt remains 'production'
CIt takes the ARG value
DIt defaults to 'development'
💡 Hint
ENV sets runtime variables; without it, see variable_tracker for APP_ENV.
Concept Snapshot
ARG sets variables only during build time.
ENV sets variables available when container runs.
Use ARG for build-time config, ENV for runtime config.
ARG values can't be accessed in running containers.
ENV variables persist in the image and container.
Use ENV to pass variables to commands run inside container.
Full Transcript
This visual execution shows how Dockerfile instructions ARG and ENV work. ARG defines variables used only during the image build process. ENV defines variables that exist inside the container when it runs. The example Dockerfile sets APP_PORT as ARG with default 8080 and APP_ENV as ENV with value production. During build, the RUN command can use APP_PORT because ARG is available. At runtime, the container can access APP_ENV but not APP_PORT because ARG does not persist. The execution table traces each step, showing when variables are set and used. The variable tracker confirms APP_PORT exists only during build, while APP_ENV exists at runtime. Key moments clarify common confusions about ARG and ENV scopes. The quiz tests understanding of variable availability and usage. The snapshot summarizes the key differences and usage rules for ARG and ENV in Dockerfiles.