๐ Scenario: You are setting up a simple web service using Docker Compose. To make sure your service is running correctly, you want to add a health check. This health check will help Docker know if your service is healthy or not.
๐ฏ Goal: Build a Docker Compose file that defines a web service with a health check. The health check will run a command inside the container to verify the service is working.
๐ What You'll Learn
Create a Docker Compose file with a service named web
Use the official nginx:alpine image for the web service
Add a health check to the web service that runs wget --no-verbose --tries=1 --spider http://localhost/
Set the health check interval to 10 seconds
Print the health status of the web service using docker compose ps
๐ก Why This Matters
๐ Real World
Health checks help keep your applications running smoothly by letting Docker know if a service is working correctly. This is important in real projects to restart or replace failing containers automatically.
๐ผ Career
Understanding health checks in Docker Compose is useful for DevOps roles, site reliability engineers, and developers who deploy applications using containers.
Progress0 / 4 steps
1
Create the basic Docker Compose file
Create a file named docker-compose.yml with a service called web that uses the image nginx:alpine.
Docker
Hint
Use the services key and define web under it with the image set to nginx:alpine.
2
Add a health check command
Add a healthcheck section under the web service with a test command that runs wget --no-verbose --tries=1 --spider http://localhost/.
Docker
Hint
The test command should be an array starting with CMD followed by the wget command.
3
Set the health check interval
Add the interval key under healthcheck with the value 10s to run the health check every 10 seconds.
Docker
Hint
The interval key controls how often the health check runs. Use 10s for 10 seconds.
4
Display the health status of the web service
Run the command docker compose ps in your terminal to see the health status of the web service.
Docker
Hint
Use docker compose ps to check the status. The health column should show healthy after some time.
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