Docker Environment Variables: Dockerfile ENV, ARG & Runtime
How to use environment variables in Docker: ENV vs ARG in Dockerfile, docker run -e, --env-file, multi-stage builds, BuildKit secrets, and best practices.
Last updated:
Frequently Asked Questions
What is the difference between ENV and ARG in a Dockerfile?
ARG is only available during the build (docker build) and is not persisted in the final image. ENV is available both during build and at runtime in the container. Use ARG for build-time configuration (version numbers, build flags) and ENV for runtime configuration (API URLs, feature flags).
How do I pass environment variables to docker run?
Use -e KEY=VALUE for individual variables or --env-file .env to load from a file. Environment variables set with -e override ENV values from the Dockerfile.
Should I put secrets in ENV instructions?
No. ENV values are baked into the image layer and visible via docker inspect and docker history. Use BuildKit secrets (--mount=type=secret) for build-time secrets, and inject runtime secrets via -e or a secrets manager.
Stay up to date
Get notified about new guides, tools, and cheatsheets.
Related Cheatsheets
Cheatsheet
YAML Syntax Cheat Sheet — Scalars, Collections & Gotchas
Quick reference for YAML syntax: strings, numbers, multiline values, sequences, mappings, anchors, aliases, and common gotchas like the Norway problem.
Cheatsheet
Nginx Cheat Sheet — Server Blocks, Proxy & SSL
Quick reference for Nginx: service management, server blocks, reverse proxy, load balancing, SSL/TLS, location matching, logging, and performance tuning.
Cheatsheet
GitHub Actions Cheat Sheet
Quick reference for GitHub Actions CI/CD: workflow syntax, triggers, jobs, matrix strategies, secrets, caching, artifacts, reusable workflows, and essential actions.
Related Guides
Kubernetes Environment Variables: ConfigMaps, Secrets & Pod Spec
How to configure environment variables in Kubernetes: inline env, ConfigMaps, Secrets, the downward API, and best practices for managing configuration at scale.
Dev Containers: Guide to Containerized Development
Everything you need to know about Dev Containers: devcontainer.json, Features, lifecycle scripts, Docker Compose integration, the CLI, performance optimization, security hardening, and common gotchas.
Docker Compose Environment Variables: The Complete Guide
How to use environment variables in Docker Compose: .env files, env_file directive, --env-file flag, multiple environments, variable substitution, and secrets management.