Docker Containerization Docker Compose and Multi-Container Applications 3 — Questions and Answers
Question 1: Which Docker Compose version 3 feature allows you to store sensitive data like passwords separately from the compose file?
- volumes
- configs
- secrets (Correct answer)
- environment
Correct answer: secrets
The secrets top-level key in Compose v3 manages sensitive data, which is mounted as files in the container at /run/secrets/.
Question 2: What does the 'build' key in a Docker Compose service definition specify?
- A pre-built image to pull from a registry
- The build context and optional Dockerfile for building the service image (Correct answer)
- The command to run after the image is built
- Resource constraints for building the image
Correct answer: The build context and optional Dockerfile for building the service image
The build key specifies the build context path and optionally a Dockerfile location used to build the service's image.
Question 3: Which command recreates only the services that have changed configuration since the last 'docker compose up'?
- docker compose restart
- docker compose up --force-recreate
- docker compose up (Correct answer)
- docker compose update
Correct answer: docker compose up
Running docker compose up without flags automatically recreates only services whose configuration has changed.
Question 4: How do you override docker-compose.yml values for a specific environment without modifying the base file?
- Edit docker-compose.yml directly
- Use docker-compose.override.yml or specify -f with an additional compose file (Correct answer)
- Set COMPOSE_OVERRIDE environment variable
- Use docker compose env command
Correct answer: Use docker-compose.override.yml or specify -f with an additional compose file
docker-compose.override.yml is automatically merged with docker-compose.yml, or you can use -f to specify multiple compose files.
Question 5: In a multi-container Compose setup, how does the 'web' service resolve the hostname 'db' to connect to the database service?
- Via /etc/hosts entries injected by Docker
- Via Docker's embedded DNS server resolving service names (Correct answer)
- Via environment variables set by links
- Via a Consul service registry
Correct answer: Via Docker's embedded DNS server resolving service names
Docker's embedded DNS server automatically resolves service names to container IP addresses within the shared Compose network.
Question 6: What is the effect of setting 'restart: always' on a Docker Compose service?
- Restarts the service on every docker compose up
- Always restarts the container if it stops for any reason (Correct answer)
- Rebuilds the image on every restart
- Restarts the service every 24 hours
Correct answer: Always restarts the container if it stops for any reason
restart: always causes Docker to automatically restart the container whenever it exits, regardless of the exit code.
Question 7: Which Compose directive allows you to define custom networks so services can be isolated from each other?
- The links key under each service
- Top-level 'networks' key with services assigned to specific networks (Correct answer)
- The expose key under each service
- The ports key with internal-only port mapping
Correct answer: Top-level 'networks' key with services assigned to specific networks
Defining networks at the top level and assigning services to specific networks enables fine-grained isolation between service groups.
Which Docker Compose version 3 feature allows you to store sensitive data like passwords separately from the compose file?