1. Env vars
environment sets environment variables inside the container as key: value pairs.
environment:Typorithm · medium
Step 1 of 8 · intro card
Compose
environment sets container env vars (or use env_file), named volumes declared at the top level persist data across restarts, and Compose puts every service on a default network so they resolve each other by service name.
environment injects environment variables into a container as key: value pairs; for many values you can point env_file at a .env file instead.
A named volume declared at the top-level volumes key persists data outside the container's lifecycle. Mounting it (dbdata:/var/lib/postgresql/data) keeps the database intact across restarts and re-creations.
Compose attaches all services to a default network, so they reach each other by service name — another container connects to this database simply as the host db. Healthchecks and profiles refine this further.
environment sets env vars; env_file loads many at once.
Named volumes persist data across restarts.
Services resolve each other by service name on the default network.
services:
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: secret
volumes:
- dbdata:/var/lib/postgresql/data
volumes:
dbdata:Connect each part of the code to the concept before you type.
environment sets environment variables inside the container as key: value pairs.
environment:Postgres reads POSTGRES_PASSWORD on first boot to set the database password.
POSTGRES_PASSWORD: secretThis mounts the named volume dbdata at the container path where Postgres stores its data.
- dbdata:/var/lib/postgresql/dataNamed volumes must be declared at the top level so Compose creates and manages them.
volumes:
dbdata:Other services reach this one by its service name, db, over the default network.
image: postgres:16Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.