Explore the foundational elements of Docker Compose with this quiz designed to reinforce understanding of multi-container configuration, service management, and basic syntax. Ideal for beginners aiming to grasp essential Docker Compose commands and structure effectively.
Which filename does Docker Compose look for by default when running 'docker-compose up' in a directory?
Explanation: Docker Compose will automatically use 'docker-compose.yml' as the default configuration file in the current directory if no file is specified. The option 'compose.yaml' is not the standard default, though it is sometimes accepted in newer versions; 'docker_file.yml' and 'compose.yml' are incorrect as they are not recognized without explicit specification. Defaulting to 'docker-compose.yml' avoids ambiguity and ensures consistent service orchestration.
In a typical 'docker-compose.yml' file, under which top-level key should individual service definitions like 'web' and 'db' be placed?
Explanation: The 'services' key is the correct top-level section where each service, such as 'web' or 'db', is defined in a Docker Compose file. 'containers' and 'apps' are not recognized keys in Compose syntax and would produce errors. 'deployments' is also not a valid key in this context. Using 'services' allows Compose to understand how to manage and start each defined component.
When you execute 'docker-compose up' in a project directory, what does it do with the services defined in the file?
Explanation: Running 'docker-compose up' initiates all services defined in the file, showing their combined output in the foreground. The command does not merely build images (though it will build if necessary); that would require a 'build' option. It does not delete containers, making 'Deletes all stopped containers' incorrect. 'Pulls images but does not start services' incorrectly focuses solely on image retrieval. Therefore, running the command brings all configured services online interactively.
How can you provide or override environment variables for a service defined in a 'docker-compose.yml' file?
Explanation: Environment variables for a service are set by including an 'environment' key with the variables listed under the specific service. Placing variables in 'volumes' or 'secrets' is not valid and will result in configuration errors. Typing variables directly after the 'image' key is not correct syntax in Docker Compose files. The 'environment' section is specifically designed for passing key-value pairs into the container environment.
Which option ensures that a 'web' service waits for a 'db' service to start before launching in Docker Compose?
Explanation: The 'depends_on' key under the 'web' service lists which other services need to be started before 'web' is launched, making startup order explicit. 'wait_for' is not a recognized Compose option and will be ignored. The order of service definitions or naming conventions like 'db_first' do not influence startup dependencies directly. Proper dependency management improves service reliability during composition.