Docker Events & Health Checks
#Docker Events & Health Checks
After this lesson you'll know:
- how to monitor the daemon with docker events
- how healthchecks auto-heal containers
- what docker inspect reveals about container state
- how to automate monitoring
#docker events โ the daemon live feed
The Docker daemon emits events for every action. You can watch them in real time:
# All events live
docker events
# Filter for specific events
docker events --filter 'type=container' --filter 'event=start'
docker events --filter 'event=die' --filter 'event=oom'
# As JSON for scripting
docker events --format '{{json .}}'
Start a new container in a second terminal โ can you see the events in the first terminal?
#Healthchecks โ the self-healing mechanism
A Healthcheck regularly tests if a container is healthy. Docker marks the container as healthy or unhealthy โ and Swarm automatically replaces unhealthy containers:
# In your Dockerfile (permanent)
FROM node:20-alpine
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
CMD curl -f http://localhost:3000/health || exit 1
CMD ["node", "server.js"]
# Or at container start
docker run \
--health-cmd='curl -f http://localhost/health || exit 1' \
--health-interval=10s \
--health-retries=3 \
--health-start-period=15s \
nginx
# Check status
docker inspect --format='{{.State.Health.Status}}' container-name
# โ healthy / unhealthy / starting
#docker inspect โ the Swiss Army Knife
docker inspect gives you all details about a container, image, volume, or network as JSON:
# Container details
docker inspect container-name
# Network config only
docker inspect --format='{{json .NetworkSettings}}' container-name
# Mount status
docker inspect --format='{{json .Mounts}}' container-name
# State (running, exit code, restart count)
docker inspect --format='{{json .State}}' container-name
# Health history
docker inspect --format='{{json .State.Health}}' container-name
#Automation & Alerting
With Events + Healthchecks + a few lines of scripting, you can monitor your containers:
# Monitor all container exits
while read -r event; do
container=$(echo "$event" | grep -oP 'name=K[^)]+')
status=$(echo "$event" | grep -oP 'exitCode=K[0-9]+')
if [ "$status" != "0" ]; then
echo "โ ๏ธ Container $container crashed (Exit code: $status)" | mail -s "Docker Alert" [email protected]
fi
done < <(docker events --filter 'event=die' --filter 'type=container')
For production, use proper tools like Watchtower (auto-updates), Diun (image notifications), or a full monitoring platform. But a simple script is a great start.
#โ Try it out
# Terminal 1:
docker events --filter 'event=die'
# Terminal 2:
docker run alpine sh -c "exit 1"
# โ In Terminal 1 you'll see the event!
# Try out a healthcheck
docker run -d --name healthtest --health-cmd='exit 0' --health-interval=2s alpine sleep 30
sleep 5
docker inspect --format='{{.State.Health.Status}}' healthtest
# โ healthy
docker rm -f healthtest
#๐ Summary
- docker events shows all daemon actions in real time
- Healthchecks let Docker auto-detect broken containers
- docker inspect provides all container details
- Events + Healthchecks + scripting = simple monitoring
Test your knowledge with a quick quiz!
4 questions ยท +40 XP