0
0
Dockerdevops~3 mins

Why ARG and ENV instructions in Docker? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

Discover how simple variables can save you hours of frustrating container rebuilds!

The Scenario

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.

The Problem

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.

The Solution

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.

Before vs After
Before
RUN echo "version=1.0" > config.txt
RUN echo "password=1234" > secrets.txt
After
ARG VERSION=1.0
ENV PASSWORD=1234
RUN echo "version=$VERSION" > config.txt
RUN echo "password=$PASSWORD" > secrets.txt
What It Enables

You can build and run containers with different settings quickly and safely without changing the Dockerfile every time.

Real Life Example

A developer can build the same app container for testing, staging, and production by just changing ARG or ENV values, not the whole Dockerfile.

Key Takeaways

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.