Docker Compose

The compose.yaml file and its keys (services, image, build, ports, environment, volumes, depends_on, networks, restart, healthcheck), the default network and name resolution by service name, the up, down, ps, logs and exec commands.

  • 25 cards
  • 5 topics
  • Free

The outline, topic by topic

Each topic is weighted the way the exam weighs it.

  1. Concepts
  2. Questions
  3. Sort and link
  4. Scenarios
  5. Understand

A few cards from the deck

  • Docker Compose: what is it?

    Show the answer

    A tool that describes in a single file several containers forming an application, with their networks and volumes, and drives them with one command.

    It replaces a pile of long docker run commands with a versioned file: docker compose up -d is enough to start everything.

  • Which command starts all the file's services in the background?

    • docker compose up -d
    • docker compose start -d
    • docker compose run -d
    • docker compose build -d
    Show the answer

    docker compose up -d

    up creates (network, volumes, containers) then starts; -d returns control. start only restarts containers that already exist, and build only builds images.

  • docker compose down deletes the containers and the network; adding the … option also deletes the … declared in the file.

    Show the answer

    docker compose down deletes the containers and the network; adding the -v option also deletes the volumes declared in the file.

    Without -v, the named volumes' data survives a down: that is what lets the application be re-created without losing the database.

  • compose.yaml: what is it?

    Show the answer

    A YAML file whose root key services lists the application's containers, each with its image, ports, variables and volumes.

    In YAML, structure comes from indentation, with spaces and never tabs: a two-space shift changes the meaning or makes the file invalid.

  • The API returns connect ECONNREFUSED 127.0.0.1:5432 although the db container is running. What is the probable cause, and the fix?

    • Port 5432 is not published; add ports: ["5432:5432"] to the db service
    • The API targets localhost, so itself; replace the host with the service name, db
    • The database is not on the same network as the API; declare a backend network for each
    • The API starts too fast; add restart: always so that it retries
    Show the answer

    The API targets localhost, so itself; replace the host with the service name, db

    127.0.0.1 is the API container's address. Between Compose services there is no need to publish a port: the default network connects them, and only the service name lets them reach each other.