A service in Docker Compose is a container configuration that defines how a container should run, including the image, ports, volumes, and environment variables.
Click to reveal answer
beginner
Which file is used to define services in Docker Compose?
Services are defined in the docker-compose.yml file using YAML syntax.
Click to reveal answer
beginner
How do you specify the image for a service in Docker Compose?
Use the image key under the service name to specify the Docker image to use, for example: image: nginx:latest.
Click to reveal answer
beginner
What is the purpose of the ports key in a service definition?
The ports key maps container ports to host machine ports, allowing access to the service from outside the container.
Click to reveal answer
beginner
How can you define environment variables for a Docker Compose service?
Use the environment key under the service to list environment variables as key-value pairs.
Click to reveal answer
Where do you define services in Docker Compose?
AIn the docker-compose.yml file
BIn the Dockerfile
CIn the .env file
DIn the docker CLI command
✗ Incorrect
Services are defined in the docker-compose.yml file using YAML syntax.
Which key specifies the Docker image for a service?
Aimage
Bbuild
Ccontainer
Dservice
✗ Incorrect
The 'image' key specifies which Docker image the service uses.
What does the 'ports' key do in a service definition?
ASets environment variables
BSpecifies the network
CDefines volumes
DMaps container ports to host ports
✗ Incorrect
The 'ports' key maps container ports to host machine ports for access.
How do you add environment variables to a service?
AUsing the 'env' key
BUsing the 'environment' key
CUsing the 'variables' key
DUsing the 'config' key
✗ Incorrect
The 'environment' key is used to define environment variables for a service.
Which file format is used for Docker Compose service definitions?
AJSON
BXML
CYAML
DINI
✗ Incorrect
Docker Compose uses YAML format in the docker-compose.yml file.
Explain how to define a simple web service in Docker Compose.
Think about the minimum keys needed to run a web server container.
You got /4 concepts.
Describe the role of environment variables in Docker Compose services and how to set them.
Consider how environment variables customize container behavior.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of defining services in a Docker Compose file?
easy
A. To install software inside containers manually
B. To write shell scripts for container startup
C. To group containers with their settings for easy management
D. To create virtual machines inside Docker
Solution
Step 1: Understand the role of services in Docker Compose
Services define how containers run, including images, ports, and environment variables.
Step 2: Recognize the benefit of grouping containers
Grouping containers with their settings allows easy start, stop, and management of multiple containers together.
Final Answer:
To group containers with their settings for easy management -> Option C
Quick Check:
Services group containers = D [OK]
Hint: Services group containers and settings in one file [OK]
Common Mistakes:
Confusing services with scripts
Thinking services create virtual machines
Believing services install software manually
2. Which of the following is the correct way to define a service named web using the image nginx in a Docker Compose YAML file?
easy
A. services:
web:
image: nginx
B. services:
web:
image nginx
C. service:
web:
image: nginx
D. services:
web:
img: nginx
Solution
Step 1: Check the correct YAML key for services
The top-level key must be services (plural), not service.
Step 2: Verify the syntax for image definition
The correct syntax is image: nginx with a colon and proper indentation.
Final Answer:
services:
web:
image: nginx -> Option A
Quick Check:
Correct YAML keys and syntax = B [OK]
Hint: Use 'services:' plural and 'image:' with colon [OK]
Common Mistakes:
Using 'service' instead of 'services'
Missing colon after 'image'
Using 'img' instead of 'image'
3. Given this Docker Compose snippet, what port will be accessible on the host machine?
services:
app:
image: myapp
ports:
- "8080:80"
medium
A. Port 80 on the host
B. Port 8080 on the host
C. Port 80 inside the container only
D. Port 8080 inside the container only
Solution
Step 1: Understand port mapping syntax
The format "host_port:container_port" means host port 8080 maps to container port 80.
Step 2: Identify which port is exposed on the host
Port 8080 on the host machine will forward traffic to port 80 inside the container.
Final Answer:
Port 8080 on the host -> Option B
Quick Check:
Host port is first in mapping = A [OK]
Hint: Host port is before colon in ports mapping [OK]
Common Mistakes:
Confusing host and container ports
Thinking container port is exposed on host
Ignoring quotes around ports
4. Identify the error in this Docker Compose service definition:
services:
db:
image: postgres
ports:
- "5432:"
environment:
POSTGRES_PASSWORD: example
medium
A. Missing container port in ports mapping
B. Environment variable name is incorrect
C. Image name should be capitalized
D. Service name cannot be 'db'
Solution
Step 1: Check ports syntax
Ports must be mapped as "host_port:container_port" or just container port. Here, "5432:" is invalid because missing container port after colon.
Step 2: Confirm environment and image correctness
Environment variable and image name are correct; service name 'db' is valid.
Final Answer:
Missing container port in ports mapping -> Option A
Quick Check:
Ports need host:container or container port only = A [OK]
Hint: Ports need host:container or container port only [OK]
Common Mistakes:
Using only one port number with quotes
Assuming environment variable names are wrong
Thinking service names have restrictions
5. You want to define two services, frontend and backend, where frontend depends on backend to start first. Which is the correct way to express this dependency in Docker Compose?