Environment Variables and Config

View saved

Pass a variable

Many images read settings from environment variables. Use -e for one-off values.

docker run --rm -e GREETING=Hello alpine printenv GREETING

Use an env file

Keep local secrets and settings out of shell history by loading a file. Do not commit real secrets to git.

# .env.example
APP_PORT=8000
APP_MESSAGE=Ship it

docker run --rm --env-file .env.example alpine printenv APP_MESSAGE

Configure in a Dockerfile

ENV sets defaults baked into the image. Runtime -e values override them.

ENV APP_MESSAGE=Hello
CMD ["sh", "-c", "echo $APP_MESSAGE"]

Prefer config over rebuilds

Change ports, feature flags, and credentials at run time when possible. Rebuild when the application code or system packages change.

Comments

One comment per signed-in account. Comments are saved with this page’s URL.