Inject environment values
docker run --rm -e APP_ENV=training -e LOG_LEVEL=info alpine:3.20 env | grep -E 'APP_ENV|LOG_LEVEL'- The image remains unchanged.
- Different containers can receive different runtime values.
Separate image contents from environment-specific configuration using environment variables, files, mounts and safe secret-handling principles.
A reusable image should not need to be rebuilt just because an environment name, feature flag or service endpoint changes. docker run -e, --env-file and Compose environment settings inject runtime configuration.
Environment variables are convenient but can be visible through process/container inspection and operational tooling. Treat credentials and private keys with stronger secret-management controls appropriate to your platform. The key principle is to keep secrets out of image layers and source repositories.
Read-only bind mounts or volumes can provide configuration files without baking them into the image. This is useful when the application expects file-based configuration, certificates or policy files.
docker run --rm -e APP_ENV=training -e LOG_LEVEL=info alpine:3.20 env | grep -E 'APP_ENV|LOG_LEVEL'printf 'APP_ENV=lab\nLOG_LEVEL=debug\n' > app.env
docker run --rm --env-file app.env alpine:3.20 envGoal: Prove that one immutable image can behave differently based on runtime environment without rebuilding.
mkdir -p ~/docker-fundamentals/config && cd ~/docker-fundamentals/config
printf 'APP_ENV=development\nMESSAGE=hello-dev\n' > dev.env
printf 'APP_ENV=production\nMESSAGE=hello-prod\n' > prod.envdocker run --rm --env-file dev.env alpine:3.20 sh -c 'echo "$APP_ENV $MESSAGE"'docker run --rm --env-file prod.env alpine:3.20 sh -c 'echo "$APP_ENV $MESSAGE"'docker image inspect alpine:3.20 --format '{{.Id}}'Organizations promote the same application image from test to production while injecting environment-specific endpoints and secrets at deployment time.
Open each answer only after you have tried to answer the question yourself.
So the same tested image can be promoted between environments without rebuilding it.
Image layers can be distributed, cached and inspected, making embedded secrets difficult to contain or remove reliably.
It loads environment variables from a file into the container at runtime.