0
0
Dockerdevops~30 mins

Alert setup for container health in Docker - Mini Project: Build & Apply

Choose your learning style9 modes available
Alert Setup for Container Health
📖 Scenario: You are managing a Docker container running a simple web application. You want to make sure the container is healthy and get alerted if it becomes unhealthy. This helps keep your app running smoothly.
🎯 Goal: Set up a Docker container with a health check and create a simple alert command to notify if the container is unhealthy.
📋 What You'll Learn
Create a Docker container running the nginx image
Add a health check command to the container configuration
Write a command to check the container's health status
Print an alert message if the container is unhealthy
💡 Why This Matters
🌍 Real World
Monitoring container health is important to keep applications running smoothly and avoid downtime.
💼 Career
DevOps engineers often set up health checks and alerts to maintain reliable containerized applications.
Progress0 / 4 steps
1
Create a Docker container running nginx
Write the docker run command to start a container named mynginx using the nginx image in detached mode.
Docker
Need a hint?

Use docker run -d --name mynginx nginx to start the container in the background.

2
Add a health check to the container
Write a docker run command to start a container named mynginx with a health check that runs curl -f http://localhost/ || exit 1 every 10 seconds, with a timeout of 5 seconds and retries set to 3.
Docker
Need a hint?

Use --health-cmd, --health-interval, --health-timeout, and --health-retries options with docker run.

3
Check the container's health status
Write a docker inspect command to get the health status of the container named mynginx. Use jq to extract the health status from the JSON output.
Docker
Need a hint?

Use docker inspect --format='{{json .State.Health.Status}}' mynginx and save it to a variable.

4
Print an alert if the container is unhealthy
Write a bash if statement that checks if the variable health_status equals unhealthy. If yes, print "Alert: Container mynginx is unhealthy!".
Docker
Need a hint?

Use if [ "$health_status" = "unhealthy" ] and echo to print the alert.