The Dockerfile ARG Instruction
Razvan Ludosanu
Founder, learnbackend.dev
Updated: 7/11/2024
Published: 7/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:
ARG variable[=default]
Run in Warp
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:
${variable}
Run in Warp
And their value can be set using the --build-arg flag of the docker build command as follows:
$ docker build --build-arg variable=value .
Run in Warp
For example, let's consider this minimal Dockerfile for building a Node.js application:
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"]
Run in Warp
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:
$ docker build --build-arg NODE_VERSION=20 --build-arg APP_PORT=3000 -t node-server .
Run in Warp
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
Founder, learnbackend.dev
Filed Under
Related Articles
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.
Use An .env File In Docker
Learn how to write and use .env files in Docker to populate the environment of containers on startup.
Run SSH In Docker
Learn how to launch and connect to a containerized SSH server in Docker using password-based authentication and SSH keys.
Launch MySQL Using Docker Compose
Learn how to launch a MySQL container in Docker Compose.
Execute in a Docker Container
Learn how to execute one or multiple commands in a Docker container using the docker exec command.
Expose Docker Container Ports
Learn how to publish and expose Docker container ports using the docker run command and Dockerfiles.
Restart Containers In Docker Compose
Learn how to restart and rebuild one or more containers in Docker Compose.
Output Logs in Docker Compose
Learn how to output, monitor, customize and filter the logs of the containers related to one or more services in Docker Compose
Rename A Docker Image
Learn how to rename Docker images locally and remotely using the docker tag command.
Understand healthcheck in Docker Compose
Learn how to check if a service is healthy in Docker Compose using the healthcheck property.