Compose Lifecycle and Environment Files
Overview
Compose creates a project from a set of services, networks and volumes. It gives the project a name, and commands such as up, logs and down operate on resources belonging to that project.
Detailed Notes
Start and inspect
docker compose up -d
docker compose ps
docker compose logs -f api
-d starts in detached mode. ps lists project services. logs -f api follows one service.
Recreate after a Compose file or image change:
docker compose up -d --build --force-recreate
--build rebuilds services that use build:. --force-recreate replaces containers even if Compose otherwise considers them unchanged.
Stop and remove
docker compose stop
docker compose down
docker compose down -v
stopstops containers but retains project resources.downremoves containers and the project network.down -valso removes named volumes declared in Compose.
Environment variables
.env can supply Compose variables:
POSTGRES_PASSWORD=change-me
API_PORT=8080
services:
api:
ports:
- "${API_PORT}:8080"
database:
image: postgres:16
environment:
POSTGRES_PASSWORD: "${POSTGRES_PASSWORD}"
Add .env to .gitignore. For a real team environment, use a secret manager or CI secret store. .env is convenient for local defaults, not encryption.
Debug Compose resolution
docker compose config
docker compose config --services
docker compose logs database
config reveals the final resolved YAML after environment substitution, which helps find wrong ports or missing values.
Common Interview Questions
Chapter quiz
Docker Fundamentals · 24 questions
1.What is an image?
2.Which component executes container operations?
3.What is Docker Hub?
4.Which command starts an image as a container?
5.What does `-p 8080:80` mean?
6.Which command displays stopped containers too?
7.What does `nginx:alpine` identify?
8.Where do runtime files created inside a container live?
9.Which identifier is more immutable?
10.Which instruction chooses the starting image?
11.Which instruction copies build context files?
12.What happens to `CMD` when `docker run image command` supplies a command?
13.What is the build context?
14.What does `.dockerignore` improve?
15.Why copy package manifests before source?
16.What does `8080:80` mean?
17.Which method resolves container names on a user-defined bridge?
18.Does `EXPOSE 80` publish port 80?
19.Which storage option is managed by Docker?
20.When is a bind mount most useful?
21.What happens to tmpfs data?
22.What is YAML indentation used for?
23.Where are Compose services defined?
24.What is Compose `depends_on` primarily responsible for?
0 of 24 answered
Finished this lesson?
0 of 27 lessons done · 0% of the course

