env.dev

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:

Loading…

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.

Was this helpful?

Stay up to date

Get notified about new guides, tools, and cheatsheets.