Reading App Config & .env Files
5 exercises on reading application configuration: docker-compose.yml services, volumes and networks, .env file security, pass-through environment variables, and YAML anchors.
Config file quick reference
env_file: .env— load all KEY=VALUE pairs from a file into the containerenvironment: - KEY(no value) — pass through from host shell environment- Never commit real secrets in .env — use .env.example with placeholders instead
- No
ports:on a service = only reachable within the Docker network by service name - YAML
&anchor+<<: *anchor= define once, reuse with merge
0 / 5 completed
1 / 5
Read this docker-compose.yml services section and answer the question:
services:
app:
image: node:20-alpine
working_dir: /app
volumes:
- .:/app
- node_modules:/app/node_modules
ports:
- "3000:3000"
env_file:
- .env
command: npm start
volumes:
node_modules:
What does the env_file: - .env directive do?env_file reads a file of KEY=VALUE pairs and injects them as environment variables at runtime — not at build time.When Docker Compose starts the container, it reads the specified file (here
.env) line by line and sets each KEY=VALUE pair as an environment variable inside the running container. The file itself is NOT copied into the image.Key distinctions:
env_file:→ injects ALL variables from a file into the container environmentenvironment:→ injects individual named variables (can inline values or reference host env)volumes: - .:/app→ mounts the current directory into /app (this mounts the .env file on disk, but that is separate from loading it as environment variables)
DATABASE_URL=postgres://user:pass@db:5432/myapp NODE_ENV=production SECRET_KEY=abc123Security note: Never commit your
.env file to version control. Add it to .gitignore. Use a .env.example file with placeholder values as documentation for other developers.Vocabulary:
- env_file → Docker Compose directive to load environment variables from a file
- runtime → when the container is actually running (vs. build time when the image is created)
- inject → pass a value into a process without hardcoding it in the code
Next up: More Reading Exercises →