0
0
Dockerdevops~30 mins

Container health checks in Docker - Mini Project: Build & Apply

Choose your learning style9 modes available
Container health checks
📖 Scenario: You are managing a web application running inside a Docker container. To keep the app reliable, you want Docker to check if the app is healthy and restart it if it is not working properly.
🎯 Goal: Learn how to add a health check to a Docker container using a Dockerfile. You will create a simple web server, add a health check command, and see the health status.
📋 What You'll Learn
Create a Dockerfile with a basic web server
Add a health check command using HEALTHCHECK
Use a simple command to test the server health
Build and run the Docker container
Check the container health status
💡 Why This Matters
🌍 Real World
In real projects, health checks help keep applications running smoothly by automatically restarting containers that are not responding.
💼 Career
Knowing how to add and use container health checks is important for DevOps roles to maintain reliable and self-healing systems.
Progress0 / 4 steps
1
Create a basic Dockerfile with a web server
Create a file named Dockerfile with these exact lines to set up a simple web server using nginx image: FROM nginx:alpine
Docker
Need a hint?

This sets the base image to nginx on Alpine Linux, which is a small web server.

2
Add a health check command to the Dockerfile
Add a HEALTHCHECK instruction to the Dockerfile that runs the command curl -f http://localhost/ || exit 1 every 30 seconds with a timeout of 5 seconds. Use HEALTHCHECK --interval=30s --timeout=5s CMD curl -f http://localhost/ || exit 1.
Docker
Need a hint?

The health check runs curl to check if the web server responds successfully.

3
Build and run the Docker container with health check
Build the Docker image with the tag mynginx using docker build -t mynginx .. Then run the container in detached mode with the name webserver using docker run -d --name webserver mynginx.
Docker
Need a hint?

Use the exact commands to build and run the container so Docker can track health.

4
Check the container health status
Use the command docker ps to see the container status. The STATUS column should show Up (healthy) if the health check passes.
Docker
Need a hint?

Look for the word healthy in the status column to confirm the health check works.