The Dockerfile ARG Instruction

Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Updated: July 11, 2024Published: July 11, 2024

The short answer

In Docker, the ARG property allows you to define arbitrary variables that will be passed to the Dockerfile at build-time:

Bash
ARG variable[=default]

Where:

  • variable is the name of the user-defined variable.
  • default is the default value of the variable if not specified.

Once defined, these variables can be used within the Dockerfile using the following syntax:

Bash
${variable}

And their value can be set using the --build-arg flag of the docker build command as follows:

Bash
$ docker build --build-arg variable=value .

For example, let's consider this minimal Dockerfile for building a Node.js application:

Bash
ARG NODE_VERSION=14
ARG APP_PORT=3000

FROM node:${NODE_VERSION}

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE ${APP_PORT}

CMD ["node", "app.js"]

The following command will set the NODE\_VERSION variable to 20 and the APP\_PORT variable to 3000, and build the Dockerfile while tagging it node-server:

Bash
$ docker build --build-arg NODE_VERSION=20 --build-arg APP_PORT=3000 -t node-server .

Easily retrieve this syntax using Warp's Agent Mode

If you’re using Warp as your terminal, you can easily retrieve this syntax using the Warp Agent Mode feature:

Entering docker build args in the Agent Mode question input will prompt a human-readable step by step guide including code snippets.

Written by
Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Filed under

Related articles


Learning Docker (The Easy Way) Using LazyDocker & Warp

A concise guide to learning Docker using Lazydocker. Highlights Docker’s benefits and takes advantage of Warp's AI features for a quick setup.

Run SSH In Docker

Learn how to launch and connect to a containerized SSH server in Docker using password-based authentication and SSH keys.

Remove a Docker Image

Learn how to remove a Docker image locally, on a Docker registry, and on Artifactory.

Override the Container Entrypoint With docker run

Learn how to override and customize the entrypoint of a Docker container using the docker run command.

Start a Docker Container

Learn how to start a new Docker container from an image in both the foreground and the background using the docker-run command.

Stop All Docker Containers

How to gracefully shutdown running containers and forcefully kill unresponsive containers with signals in Docker using the docker-stop and docker-kill commands.

Set Docker Container Hostname

Learn how to set, change and match a docker container hostname.

How To Use An .env File In Docker Compose

Learn how define and pass environment variables to Docker containers using an .env file in Docker Compose.

Use An .env File In Docker

Learn how to write and use .env files in Docker to populate the environment of containers on startup.

Restart Docker Containers

Learn how to restart Docker containers automatically with restart policies and manually using the docker restart, docker start, docker stop and docker kill commands.

Run Bash Shell In Docker

Start an interactive shell in Docker container

Launch MySQL Using Docker Compose

Learn how to launch a MySQL container in Docker Compose.