SkillSprout
Chapter 1 · Lesson 20
Intermediate
1 min read

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

bash
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:

bash
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

bash
docker compose stop
docker compose down
docker compose down -v
  • stop stops containers but retains project resources.
  • down removes containers and the project network.
  • down -v also removes named volumes declared in Compose.

Environment variables

.env can supply Compose variables:

text
POSTGRES_PASSWORD=change-me
API_PORT=8080
yaml
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

bash
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. 1.What is an image?

  2. 2.Which component executes container operations?

  3. 3.What is Docker Hub?

  4. 4.Which command starts an image as a container?

  5. 5.What does `-p 8080:80` mean?

  6. 6.Which command displays stopped containers too?

  7. 7.What does `nginx:alpine` identify?

  8. 8.Where do runtime files created inside a container live?

  9. 9.Which identifier is more immutable?

  10. 10.Which instruction chooses the starting image?

  11. 11.Which instruction copies build context files?

  12. 12.What happens to `CMD` when `docker run image command` supplies a command?

  13. 13.What is the build context?

  14. 14.What does `.dockerignore` improve?

  15. 15.Why copy package manifests before source?

  16. 16.What does `8080:80` mean?

  17. 17.Which method resolves container names on a user-defined bridge?

  18. 18.Does `EXPOSE 80` publish port 80?

  19. 19.Which storage option is managed by Docker?

  20. 20.When is a bind mount most useful?

  21. 21.What happens to tmpfs data?

  22. 22.What is YAML indentation used for?

  23. 23.Where are Compose services defined?

  24. 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