Externalize Docker Configuration with Environment Variables and Compose Secrets
Externalize Docker app config the 12-factor way using -e, --env-file, Compose env_file, and Compose secrets.

Lab overview
Configuration is one of the few things that legitimately differs between a developer laptop, a staging cluster, and production. The twelve-factor methodology codifies this with its third factor: store config in the environment, never in the code. Docker takes that doctrine and gives developers a layered toolkit for honoring it — the -e flag for one-off overrides, --env-file for bundling related variables, the Compose environment: and env_file: directives for declarative service definitions, and the Compose secrets: block for credentials that should never appear in a process listing.
These layers exist because not all config is equal. A non-sensitive value like an app name belongs in a checked-in .env.example. A connection string belongs in an environment-specific .env file that stays out of source control. And a database password belongs nowhere near a process environment — it should be mounted as a file at /run/secrets/<name> so it never appears in docker inspect or process listings. In this lab, you will progressively externalize the config of a small Node.js HTTP service, starting from a hardcoded anti-pattern and ending with a 12-factor-clean container whose only inputs are an --env-file and a file-backed Compose secret, with .dockerignore ensuring no secrets ever leak into a built image.
Objectives
Upon completion of this intermediate-level lab, you will be able to:
- Run a containerized app with config injected via
-e VAR=valueflags - Bundle related variables into a
.envfile and load them with--env-file - Declare configuration in
docker-compose.ymlusingenvironment:andenv_file:directives with variable substitution - Mount a sensitive credential as a file-backed Compose secret at
/run/secrets/<name> - Prevent secret leakage into images using
.dockerignore
Who is this lab for?
This lab is designed for:
- Developers adopting the 12-factor configuration pattern in containerized apps
- Platform engineers separating secrets from non-sensitive config in Compose stacks
- Security-minded practitioners hardening Docker workflows against credential leaks
Verified against your live environment
An automated validation engine inspects your actual resources and configurations as you work. Completion means the task was performed, not multiple choice, real-world proficiency.
More labs like this
Deploy Multi-Container Apps with Docker Compose
Author a compose.yaml that runs nginx, a Python API, and Postgres as a single declarative multi-service stack.
Docker Compose for Microservices with Scaling and Health Checks
Build a 4-service microservices stack with Docker Compose, scale replicas, add HEALTHCHECK and restart policies, and test failover and recovery.
Running Containers with docker run and kubectl
Run nginx:alpine first with docker run then with kubectl run on the same VM, and see how Kubernetes maps onto Docker concepts.
Environment
Every lab includes
- Real environment, pre-credentialed
- Automated checks on every step
- Isolated sandbox, auto cleanup
- AI-recommended next steps
Lab curriculum
- 01
Getting Started with Docker IDE
- 02
Running the App with -e Flag Overrides
1 automated check
- 03
Externalizing Variables with --env-file Loading
1 automated check
- 04
Declaring Config in the Docker Compose File
1 automated check
- 05
Mounting Secrets with the Compose Secrets Block
1 automated check
- 06
Preventing Secret Leakage with .dockerignore
1 automated check
Not the lab you were looking for?
Browse 200+ hands-on labs across AWS, Azure, Kubernetes, Docker, and cloud security.