Kritim Yantra
Apr 25, 2025
When working with Docker, understanding the difference between Build Arguments and Environment Variables is key to writing clean, efficient, and customizable Dockerfiles.
In this post, we’ll break them down in plain English:
Let’s get started!
ARG)?Build arguments are variables you can pass at build-time (when you build the image using docker build). They’re useful when you want to customize your image without hardcoding values.
In your Dockerfile:
ARG VERSION=16
FROM node:${VERSION}
When building the image:
docker build --build-arg VERSION=18 -t my-node-app .
If you don’t pass a --build-arg, Docker will use the default (16 in this case).
ENV)?Environment variables are values that are available at runtime (when the container is running). You can set them in the Dockerfile or while running the container.
development or productionIn your Dockerfile:
ENV PORT=3000
ENV NODE_ENV=production
These values will be accessible inside the container:
echo $PORT
# or
process.env.PORT // in Node.js
You can also override or set them using the docker run command:
docker run -e PORT=4000 -e NODE_ENV=development my-node-app
| Feature | Build ARG (ARG) |
Env VAR (ENV) |
|---|---|---|
| Available during | Image build-time | Container runtime |
| Can be overridden at run-time? | ❌ No | ✅ Yes |
| Passed via | --build-arg in docker build |
-e in docker run or ENV in Dockerfile |
| Available to application code? | ❌ No (unless re-exported) | ✅ Yes |
| Security? | Not secure for secrets | Also not secure for secrets |
# Build-time variable
ARG NODE_VERSION=18
# Use the build arg to pick a base image
FROM node:${NODE_VERSION}
# Set runtime environment variables
ENV APP_PORT=3000
ENV NODE_ENV=production
WORKDIR /app
COPY . .
RUN npm install
EXPOSE ${APP_PORT}
CMD ["node", "server.js"]
docker build --build-arg NODE_VERSION=20 -t my-app .
docker run -e APP_PORT=8080 -e NODE_ENV=development -p 8080:8080 my-app
Your app will now run with Node.js v20, on port 8080, in development mode.
You can also set environment variables in your docker-compose.yml:
services:
app:
build:
context: .
args:
NODE_VERSION: 18
environment:
- NODE_ENV=production
- APP_PORT=3000
ports:
- "3000:3000"
And optionally define them in a .env file:
NODE_ENV=production
APP_PORT=3000
Docker Compose will automatically read .env values.
⚠️ Do not use ARG or ENV to store sensitive data like passwords or API keys.
These values can be seen using docker history or by inspecting containers.
Instead, use secure secrets managers (like Docker secrets, AWS Secrets Manager, Vault, etc.) when dealing with production secrets.
ARG for values needed only at build time.ENV for values used at runtime.ARG and ENV in your Dockerfile..env or external config files when possible.| Feature | Build ARG | Environment VAR |
|---|---|---|
| Defined using | ARG |
ENV |
| Scope | Build-time only | Runtime |
| Use case | Image customization | App configuration |
| CLI usage | --build-arg |
-e or docker-compose |
| Access inside app | ❌ (unless converted) | ✅ Yes |
Both ARG and ENV are powerful tools in Docker that help you:
Mastering them will level up your Docker skills and help you build production-ready containers like a pro 🛠️
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google
Kritim Yantra
Kritim Yantra
Kritim Yantra