Course contentsShow
← Cloud and Infrastructure
Lesson 1006 of 2,98920. Docker in DepthFree lesson

ARG: Build-Time Variables

Learn ARG instruction for parameterizing builds, scope, default values, and ARG vs ENV differences.

ARG: Build-Time Variables

What you'll learn: How to pass configurable parameters into your Docker builds without hardcoding values directly in the Dockerfile.


What Are ARG Instructions?

The ARG instruction defines build-time variables — parameters you can pass into Docker during the image build process. Unlike ENV variables that persist in running containers, ARG values only exist while the image is being built. Think of them as function arguments for your Dockerfile.

How ARG Works

ARG VERSION=1.0
ARG BUILD_ENV

FROM ubuntu:22.04
RUN echo "Building version ${VERSION} for ${BUILD_ENV}"

You can set ARG values when building:

docker build --build-arg BUILD_ENV=production --build-arg VERSION=2.1 .

If you don't provide a value, Docker uses the default (like VERSION=1.0 above). If there's no default, the variable is empty.

Scope Matters

ARG instructions placed before FROM are outside the build context and only available to the FROM line itself. To use them inside the build, redeclare them after FROM:

ARG BASE_VERSION=22.04
FROM ubuntu:${BASE_VERSION}
ARG BASE_VERSION
RUN echo "Using base ${BASE_VERSION}"

ARG vs ENV

  • ARG: Build-time only; not visible in running containers; used for conditional build logic
  • ENV: Runtime variables; persist in containers; used for application configuration

Key Takeaway: Use ARG to parameterize Dockerfile builds with values set at build-time via --build-arg, keeping your images flexible without hardcoding environment-specific details. Remember ARGs disappear after the build completes—they're construction tools, not runtime configuration.