A health check is a way to test if a container is working correctly by running a command inside it regularly. It helps Docker know if the app inside the container is healthy or not.
Click to reveal answer
beginner
Which key is used to define a health check in a Docker Compose service?
The key healthcheck is used under a service in the docker-compose.yml file to define commands and settings for health checks.
Click to reveal answer
intermediate
What does the test option inside a health check specify?
The test option defines the command Docker runs inside the container to check if it is healthy. For example, test: ["CMD", "curl", "-f", "http://localhost"].
Click to reveal answer
intermediate
What happens if a health check fails multiple times in Docker Compose?
If the health check command fails repeatedly, Docker marks the container as unhealthy. This can be used to trigger restarts or alerts.
Click to reveal answer
intermediate
Name two timing options you can set for a health check in Docker Compose.
You can set interval (how often to run the check) and timeout (how long to wait for the check to finish). Others include retries and start_period.
Click to reveal answer
Which key defines a health check in a Docker Compose service?
Atesthealth
Bcheckhealth
Chealth
Dhealthcheck
✗ Incorrect
The correct key to define health checks in Docker Compose is healthcheck.
What does the test option inside a health check do?
ARuns a command inside the container to check health
BSets the health check interval
CDefines the container restart policy
DSpecifies the container image
✗ Incorrect
The test option runs a command inside the container to verify if it is healthy.
If a health check fails repeatedly, Docker marks the container as:
Aunhealthy
Bstopped
Cpaused
Drunning
✗ Incorrect
Repeated health check failures cause Docker to mark the container as unhealthy.
Which of these is NOT a valid health check timing option?
Aretries
Btimeout
Crestart
Dinterval
✗ Incorrect
restart is not a health check timing option; it is a separate container policy.
How often does Docker run the health check command by default if not specified?
A10 seconds
B30 seconds
C1 minute
D5 seconds
✗ Incorrect
By default, Docker runs health checks every 30 seconds if interval is not set.
Explain how to add a health check to a service in a Docker Compose file.
Think about the keys under the service in docker-compose.yml
You got /3 concepts.
Describe what happens when a container's health check fails multiple times.
Focus on Docker's reaction to health check results
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of a health check in a Docker Compose service?
easy
A. To restart the container automatically
B. To verify if the container's application is running correctly
C. To build the container image
D. To expose ports to the host machine
Solution
Step 1: Understand health check purpose
Health checks run commands inside the container to confirm the app is working well.
Step 2: Differentiate from other Docker tasks
Restarting, building, and exposing ports are separate Docker tasks, not health checks.
Final Answer:
To verify if the container's application is running correctly -> Option B
Quick Check:
Health check = verify app status [OK]
Hint: Health checks confirm app health inside containers [OK]
Common Mistakes:
Confusing health checks with container restart policies
Thinking health checks build images
Mixing health checks with port mapping
2. Which of the following is the correct syntax to add a health check in a Docker Compose service?
easy
A. healthcheck: { run: "curl http://localhost" }
B. health_check: { command: "curl http://localhost" }
C. healthcheck: { test: ["CMD", "curl", "-f", "http://localhost"] }
D. healthcheck: { check: "curl http://localhost" }
Solution
Step 1: Recall Docker Compose healthcheck syntax
The correct key is 'healthcheck' with a 'test' array specifying the command.
Step 2: Validate command format
The 'test' must be an array starting with 'CMD' followed by the command and arguments.
A. The test command must be an array starting with CMD
B. The interval value must be in milliseconds
C. Retries cannot be less than 3
D. The healthcheck key should be under volumes
Solution
Step 1: Check healthcheck test syntax
Docker Compose requires the test command as an array starting with 'CMD' or 'CMD-SHELL'.
Step 2: Identify why string command fails
Using a plain string without array causes Compose to ignore or fail the health check.
Final Answer:
The test command must be an array starting with CMD -> Option A
Quick Check:
Use array with CMD for test command [OK]
Hint: Healthcheck test must be array with CMD or CMD-SHELL [OK]
Common Mistakes:
Writing test as plain string instead of array
Misplacing healthcheck under wrong keys
Using invalid interval units
5. You want a service in Docker Compose to be considered healthy only if a custom script check.sh inside the container returns success within 5 seconds. Which Compose healthcheck configuration is best?
hard
A. healthcheck:
test: ["CMD-SHELL", "./check.sh"]
interval: 10s
retries: 3