Docker Containerization Docker Compose and Multi-Container Applications 5 — Questions and Answers
Question 1: In a Docker Compose file, what does specifying 'network_mode: host' for a service do?
- Restricts the service to only the host's loopback interface
- Removes container network isolation so the service shares the host's network stack (Correct answer)
- Assigns the host's IP to the container
- Enables host DNS resolution only
Correct answer: Removes container network isolation so the service shares the host's network stack
network_mode: host removes network isolation and the container directly uses the host's network interfaces and ports.
Question 2: Which strategy ensures a service in Docker Compose only starts after its dependency is fully healthy, not just running?
- depends_on with condition: service_started
- depends_on with condition: service_healthy (Correct answer)
- links with healthy: true
- wait_for with health_timeout
Correct answer: depends_on with condition: service_healthy
depends_on with condition: service_healthy waits for the dependency's healthcheck to pass before starting the dependent service.
Question 3: What is the effect of using 'docker compose up -d' followed by 'docker compose stop'?
- Stops and removes all containers and networks
- Stops running containers but keeps them and their state preserved (Correct answer)
- Pauses container processes without stopping them
- Removes containers but keeps volumes and networks
Correct answer: Stops running containers but keeps them and their state preserved
docker compose stop halts the running containers without removing them, preserving their state so they can be restarted with docker compose start.
Question 4: How can you pass build-time arguments to a Dockerfile from a Docker Compose file?
- Using the 'environment' key under the service
- Using 'args' under the 'build' key (Correct answer)
- Using 'build_args' at the top level
- Build arguments cannot be passed from Compose files
Correct answer: Using 'args' under the 'build' key
The args sub-key under build passes ARG values to the Dockerfile during the build process.
Question 5: What is the recommended approach for sharing a database volume between a primary and a replica service in Docker Compose?
- Use the same named volume for both services (Correct answer)
- Use bind mounts pointing to the same host directory
- Use separate named volumes with rsync replication
- Database sharing between containers is not supported in Compose
Correct answer: Use the same named volume for both services
Referencing the same named volume in multiple service definitions allows both services to access the same persisted data storage.
Question 6: Which Compose feature lets you extend and reuse service definitions across multiple compose files?
- The 'import' key
- The 'extends' key referencing another service or file (Correct answer)
- YAML anchors and aliases
- The 'template' key
Correct answer: The 'extends' key referencing another service or file
The extends key allows a service to inherit configuration from another service defined in the same or a different Compose file.
Question 7: When running 'docker compose down', what resources are NOT removed by default?
- Containers and default networks
- Named volumes and images
- Anonymous volumes and external networks
- Both B and C (Correct answer)
Correct answer: Both B and C
By default, docker compose down removes containers and default networks, but named volumes, external networks, and images are preserved unless --volumes, --remove-orphans, or --rmi flags are used.
In a Docker Compose file, what does specifying 'network_mode: host' for a service do?