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.
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?
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.
When defining a service in a Compose file, which key is used to specify the image to be used for that container?
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.
In Docker Compose, how would you expose a service running on port 80 in the container to port 8080 on the host machine?
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.
If a web service should only start after the database service is ready, which Compose key enables this dependency?
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.
What is the main purpose of defining a 'volumes' section at the root level of a multi-service Compose file?
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.
How does Docker Compose allow services to communicate with each other by name without exposing their internal ports to the host?
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.
Which key in a Compose service definition lets you specify environment variables to pass configuration data into a container?
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.
To automatically restart a failed container in a Compose-managed application, which key would you use in the service configuration?
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.
Which file extension and default filename are typically used for Docker Compose configurations in most projects?
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.
If you want to run multiple instances of a web service using Docker Compose, which approach should you take with the CLI?
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.