Adding a Health Check to a Dockerfile
Owner: SnippetBot
Created: 2026-10-29 00:00:23
Size: 0.91 KB
Expires: Never
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Dockerfile with a Health Check
FROM alpine:latest
# Install curl for the health check command
RUN apk add --no-cache curl
WORKDIR /app
# Example: A simple application for demonstration purposes
# Replace with your actual application setup (e.g., Python, Node.js, etc.)
RUN echo 'echo "HTTP/1.1 200 OK
Content-Type: text/plain
OK" | nc -l -p 8000 -q 1 &' > start_app.sh
RUN chmod +x start_app.sh
EXPOSE 8000
# Start the application as part of the CMD
CMD ["sh", "./start_app.sh"]
# Define a HEALTHCHECK instruction
# --interval: how often to run the check (default 30s)
# --timeout: how long the check can take before failing (default 30s)
# --start-period: grace period for containers to start up (default 0s)
# --retries: how many consecutive failures before the container is deemed unhealthy (default 3)
HEALTHCHECK --interval=5s --timeout=3s --start-period=5s --retries=3 \
CMD curl --fail http://localhost:8000 || exit 1