Docker Compose: Managing Multiple Containers Effectively Quiz

Explore essential concepts related to orchestrating and managing multiple containers using Docker Compose, including service dependencies, configuration strategies, and common command-line operations for multi-container applications. This quiz aims to deepen your understanding of practical tools and best practices within the docker-compose ecosystem.

  1. Defining Multiple Services

    When setting up a docker-compose.yml file to run a web application and a database together, which section is essential for specifying both containers as separate services?

    1. services
    2. images
    3. containers
    4. volumes

    Explanation: The 'services' section in a docker-compose.yml file is used to define and configure multiple containers, such as a web app and a database, as separate but orchestrated components. 'Images' refers to container images, not service definitions. 'Containers' is not a valid section in docker-compose.yml. 'Volumes' is used for data storage, not for defining service relationships.

  2. Service Dependencies

    If a backend service must not start before the database service is ready, which docker-compose directive would set this dependency in the configuration?

    1. depends_on
    2. links
    3. requires
    4. triggers

    Explanation: 'depends_on' is the correct directive to indicate service startup order, ensuring, for example, that the backend waits for the database. 'Links' is deprecated and provides legacy network aliasing. 'Requires' and 'triggers' are not valid docker-compose directives and won't set up such dependencies.

  3. Scaling a Service

    To run five identical instances of a worker service defined in a compose file, which command would you use in the terminal?

    1. docker-compose up --scale worker=5
    2. docker-compose start worker-5
    3. docker-compose build --scale 5
    4. docker-compose exec worker *5

    Explanation: The correct command to run multiple replicas of a service is 'docker-compose up --scale worker=5'. The other options do not apply: 'docker-compose start worker-5' is not a valid syntax, 'build' is for image creation, and 'exec' is used to run commands in a running container, not scaling services.

  4. Sharing Data Between Containers

    Which docker-compose feature allows two services to access the same files on the host system, such as sharing log output or database files?

    1. volumes
    2. networks
    3. ports
    4. environments

    Explanation: 'Volumes' enable sharing of file data between services and the host, making it easy for containers to access common files. 'Networks' facilitate communication, not file sharing. 'Ports' expose services outside containers. 'Environments' set configuration through environment variables, not file access.

  5. Environment Variable Configuration

    When passing sensitive information like passwords to services within a Compose file, which section is most appropriate for defining such values per service?

    1. environment
    2. labels
    3. command
    4. dns

    Explanation: The 'environment' section is used to pass variables such as passwords to containers, ensuring each service can access necessary configuration securely. 'Labels' add metadata but do not configure sensitive values. 'Command' specifies the container startup command. 'Dns' deals with networking, not service configuration data.