Add a health check to a Docker container running a simple Node.js application.
Question
Add a health check to a Docker container running a simple Node.js application. The health check should verify that the application is running and accessible.
Sample Healthcheck API in node.js,
app.js
Solution
Step 1: Create a Dockerfile
HEALTHCHECK
: The health check is added at the end of the Dockerfile. It checks the/health
endpoint of your application every 30 seconds.--interval=30s
: Check the health every 30 seconds.--timeout=5s
: If the health check takes longer than 5 seconds, it’s considered a failure.--start-period=5s
: Wait 5 seconds before starting the first health check.--retries=3
: If the health check fails 3 consecutive times, the container is considered unhealthy.
Step 2: Build and Run the Docker Image
First, build the Docker image,
Then, run the container,
Step 3: Verify the Health Check
To check the status of the container’s health, run
In the output, you should see a STATUS
column that will change from starting
to healthy
once the health check passes.
Last updated