Docker Containerization Docker Compose and Multi-Container Applications 4 — Questions and Answers
Question 1: What is the role of the COMPOSE_PROJECT_NAME environment variable?
- Specifies the default compose file path
- Sets the prefix used for naming containers, networks, and volumes (Correct answer)
- Defines which services to start by default
- Configures the Docker registry for image pulls
Correct answer: Sets the prefix used for naming containers, networks, and volumes
COMPOSE_PROJECT_NAME sets the project name prefix applied to all resource names, allowing multiple isolated Compose stacks on the same host.
Question 2: In Docker Compose, what distinguishes a 'named volume' from a 'bind mount'?
- Named volumes are faster than bind mounts
- Named volumes are managed by Docker and persist independently of the host path (Correct answer)
- Bind mounts cannot be shared between services
- Named volumes are only available in Docker Swarm
Correct answer: Named volumes are managed by Docker and persist independently of the host path
Named volumes are created and managed by Docker in its own storage area, while bind mounts reference a specific host filesystem path.
Question 3: How do you view real-time logs from all services in a running Docker Compose application?
- docker compose status --logs
- docker compose logs -f (Correct answer)
- docker compose monitor
- docker ps --logs
Correct answer: docker compose logs -f
docker compose logs -f streams logs from all services in real time, similar to tail -f.
Question 4: What does the 'profiles' feature in Docker Compose allow you to do?
- Set resource profiles like CPU and memory limits
- Selectively activate groups of services for different environments (Correct answer)
- Configure logging profiles per service
- Define network QoS profiles
Correct answer: Selectively activate groups of services for different environments
Profiles let you mark services with a profile name so they only start when that profile is explicitly activated via --profile flag.
Question 5: When using Docker Compose with 'docker compose exec', what does this command do differently than 'docker compose run'?
- exec creates a new container; run executes in an existing one
- exec runs a command in an already running container; run starts a new container (Correct answer)
- exec is for one-off commands; run is for long-running processes
- There is no functional difference between the two
Correct answer: exec runs a command in an already running container; run starts a new container
docker compose exec runs a command inside an already running container, while docker compose run starts a fresh container for a one-off command.
Question 6: Which Compose configuration correctly maps host port 8080 to container port 80 for a web service?
- ports: ['80:8080']
- ports: ['8080:80'] (Correct answer)
- expose: ['8080:80']
- bind: ['8080->80']
Correct answer: ports: ['8080:80']
The format is 'HOST_PORT:CONTAINER_PORT', so '8080:80' maps host port 8080 to container port 80.
Question 7: What is the purpose of the 'configs' top-level key in Docker Compose (v3.3+)?
- Stores environment-specific variable overrides
- Mounts non-sensitive configuration files into containers (Correct answer)
- Defines Docker daemon configuration
- Specifies Compose CLI configuration options
Correct answer: Mounts non-sensitive configuration files into containers
The configs key allows you to mount non-sensitive configuration files into containers, similar to secrets but without encryption.
What is the role of the COMPOSE_PROJECT_NAME environment variable?