Docker Containerization Docker Compose and Multi-Container Applications 2 — Questions and Answers
Question 1: Which Docker Compose command stops and removes containers, networks, and volumes defined in the compose file?
- docker compose stop
- docker compose down --volumes (Correct answer)
- docker compose rm -f
- docker compose kill
Correct answer: docker compose down --volumes
docker compose down --volumes stops containers and removes containers, networks, and named volumes.
Question 2: In a Docker Compose file, what does the 'depends_on' directive control?
- Resource limits for dependent services
- Startup and shutdown order of services (Correct answer)
- Network access between services
- Volume sharing between services
Correct answer: Startup and shutdown order of services
depends_on controls the order in which services start and stop, but does not wait for the service to be 'ready'.
Question 3: What is the default network behavior when services are defined in the same Docker Compose file?
- Each service gets its own isolated network
- All services share the host network
- All services are placed on a shared default bridge network (Correct answer)
- Services must manually link to communicate
Correct answer: All services are placed on a shared default bridge network
Docker Compose automatically creates a default bridge network that all services in the file join, enabling name-based DNS resolution.
Question 4: Which field in docker-compose.yml specifies environment variables loaded from an external file?
- env_file (Correct answer)
- environment
- secrets
- configs
Correct answer: env_file
The env_file field loads environment variables from one or more external files into the container.
Question 5: What happens when you run 'docker compose up --scale web=3'?
- Creates 3 separate Compose projects
- Starts 3 instances of the web service (Correct answer)
- Allocates 3x CPU to the web service
- Runs the web service build 3 times
Correct answer: Starts 3 instances of the web service
--scale web=3 starts three replicas of the web service within the same Compose project.
Question 6: In Docker Compose, which volume type mounts a directory from the host machine directly into the container?
- Named volume
- Anonymous volume
- Bind mount (Correct answer)
- tmpfs mount
Correct answer: Bind mount
A bind mount maps a specific host path (e.g., ./data:/app/data) into the container.
Question 7: What is the purpose of the 'healthcheck' directive in a Docker Compose service definition?
- Monitors host CPU usage
- Defines a command Docker runs to test if the container is healthy (Correct answer)
- Sends alerts to monitoring systems
- Limits container restart attempts
Correct answer: Defines a command Docker runs to test if the container is healthy
The healthcheck directive specifies a command Docker executes periodically to determine if the container is functioning correctly.
Which Docker Compose command stops and removes containers, networks, and volumes defined in the compose file?