Back to articles
How to Monitor Docker Container Health and Auto-Restart on Failure
How-ToDevOps

How to Monitor Docker Container Health and Auto-Restart on Failure

via Dev.to DevOpsYash

How to Monitor Docker Container Health and Auto-Restart on Failure Your container is running but not actually working. It's accepting connections but returning 500s. Or it started but hasn't finished initializing. Docker's built-in health checks handle all of this. The Basic Health Check FROM node:20-alpine WORKDIR /app COPY . . RUN npm ci # Health check: runs every 30s, fails after 3 attempts HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 CMD curl -f http://localhost:3000/health || exit 1 CMD ["node", "server.js"] Or in docker-compose.yml: services : app : image : your-app healthcheck : test : [ " CMD" , " curl" , " -f" , " http://localhost:3000/health" ] interval : 30s timeout : 10s retries : 3 start_period : 30s # Grace period on startup Check Container Health Status # See health status docker ps # Detailed health info docker inspect --format = '{{json .State.Health}}' <container> | python3 -m json.tool # Watch health checks in real time watch -n 5 'docker i

Continue reading on Dev.to DevOps

Opens in a new tab

Read Full Article
8 views

Related Articles