Multi-Service Docker Compose Essentials Quiz Quiz

Challenge your understanding of writing and configuring multi-service Docker Compose files, including service definitions, networking, dependencies, and best practices. This quiz is designed for those seeking to strengthen foundational skills in orchestrating multiple containers with Compose.

  1. Service Definition Basics

    Which section in a Docker Compose YAML file is used to declare multiple application services, like a web server and a database, that should be launched together?

    1. networks
    2. stages
    3. volumes
    4. services

    Explanation: The 'services' section is where you define the different containers that will run as part of your application stack. 'volumes' and 'networks' are used to configure data persistence and networking, not for declaring the services themselves. 'stages' is not a valid section in the Compose file format. Therefore, 'services' is the correct answer.

  2. Specifying Base Images

    When defining a service in a Compose file, which key is used to specify the image to be used for that container?

    1. base
    2. image
    3. container
    4. instance

    Explanation: The 'image' key tells Compose which container image to use for the service. 'container', 'instance', and 'base' are not valid keys for this purpose and will cause errors if used instead. Only 'image' correctly instructs Compose on which image to deploy.

  3. Port Mapping Syntax

    In Docker Compose, how would you expose a service running on port 80 in the container to port 8080 on the host machine?

    1. 80:8080
    2. 8080:80
    3. 8080-u003E80
    4. 80-u003E8080

    Explanation: The syntax 'host_port:container_port' is used to map ports in Docker Compose. Therefore, '8080:80' maps port 8080 on the host to port 80 within the container. '80:8080' would reverse this mapping. '8080-u003E80' and '80-u003E8080' are not valid Compose syntaxes and will not work.

  4. Service Dependencies

    If a web service should only start after the database service is ready, which Compose key enables this dependency?

    1. links
    2. require
    3. wait_for
    4. depends_on

    Explanation: The 'depends_on' key makes one service dependent on another, instructing Compose to start the dependency first. 'links' is used for network connectivity but does not control start order. 'wait_for' and 'require' are not valid Compose configuration options. 'depends_on' ensures services start in the right order for basic dependencies.

  5. Persistent Data with Volumes

    What is the main purpose of defining a 'volumes' section at the root level of a multi-service Compose file?

    1. To define environment variables for services
    2. To list unused containers
    3. To configure service dependencies
    4. To create named persistent storage shared between services

    Explanation: The root-level 'volumes' section allows you to declare named volumes that containers can share, which is essential for persisting data across service restarts. Defining environment variables is done within the service itself, not in 'volumes'. Unused containers are not listed here, and service dependencies are handled elsewhere using 'depends_on' or similar keys.

  6. Isolated Communication

    How does Docker Compose allow services to communicate with each other by name without exposing their internal ports to the host?

    1. By default, services are on a user-defined network and can reach each other via service names
    2. Service names are not used for internal communication
    3. They must use host networking for inter-service communication
    4. Each service needs the 'public: true' setting

    Explanation: Compose automatically creates a user-defined network for the project, allowing containers to address each other by service name internally without exposing ports. 'Host networking' is rarely used for this and can bypass isolation. There's no 'public: true' setting in Compose. Service names are indeed how internal communication typically occurs, making the correct answer clear.

  7. Passing Configuration

    Which key in a Compose service definition lets you specify environment variables to pass configuration data into a container?

    1. env_vars
    2. config_vars
    3. environment
    4. environ

    Explanation: The 'environment' key is used to provide environment variables to a service in Compose. 'config_vars', 'environ', and 'env_vars' are not valid keys in the Compose specification and will not have any effect. Only 'environment' is recognized by Compose for this purpose.

  8. Restart Policies

    To automatically restart a failed container in a Compose-managed application, which key would you use in the service configuration?

    1. repeat
    2. restart
    3. redo
    4. again

    Explanation: 'restart' allows you to set policies for container restarts when using Compose, such as 'always' or 'on-failure'. 'redo', 'again', and 'repeat' are not valid Compose options and have no effect on service restart behavior. Therefore, 'restart' is the correct choice for automatic restarts.

  9. File Format Identification

    Which file extension and default filename are typically used for Docker Compose configurations in most projects?

    1. dockerfile.yml
    2. docker-compose.yml
    3. DockerFile
    4. compose.yaml

    Explanation: The default filename and extension for Docker Compose files is 'docker-compose.yml', although 'compose.yaml' is sometimes supported as an alternative. 'DockerFile' is used for single image build instructions, and 'dockerfile.yml' is not a standard naming convention. 'docker-compose.yml' is the most widely recognized and supported name.

  10. Scaling Services

    If you want to run multiple instances of a web service using Docker Compose, which approach should you take with the CLI?

    1. Use the '--scale' flag with 'docker compose up'
    2. Duplicate port numbers in the 'ports' section
    3. Increase the 'replicas' value in the Compose file
    4. Add more 'services' entries with different names

    Explanation: The '--scale' flag allows you to specify how many instances of a service to launch. 'replicas' is used in other orchestration tools rather than Compose itself. Adding more services or duplicating port numbers would cause conflicts or are not the intended way to scale services. The CLI scaling option is the simplest and correct choice for Compose.