Why Containers Restart Automatically?
Docker containers restart automatically because the container process exits and Docker restart policies, orchestration systems, health checks, or container runtime mechanisms attempt to recover the container automatically.
How Docker Containers Work
A Docker container runs only while its main process is alive.
Container Starts
|
Main Process Runs
|
Process Exits
|
Container Stops
Docker considers the container finished when the main process exits.
Example
docker run ubuntu
The container exits immediately because no long-running process exists.
Container Lifecycle
Created
|
Started
|
Running
|
Exited
|
Restarted (optional)
Why Automatic Restart Exists
Modern production systems serving users from USA, UK, India, Europe, and global regions require high availability.
If a container crashes:
- Applications may go down
- APIs may fail
- Payments may stop
- User traffic may be lost
βAutomatic restart improves resiliency and reduces downtime.β
Real-Time Production Example
Nginx
API Gateway
Portfolio Service
Interview Service
Payment Service
MySQL
Redis
If the API Gateway crashes unexpectedly:
Traffic Fails
|
502 Errors
|
Users Cannot Access Platform
Restart policies automatically recover the service.
Main Reasons Containers Restart Automatically
| Reason | Example |
|---|---|
| Application crash | Unhandled exception |
| OOMKilled | Out of memory |
| Failed health check | Application unhealthy |
| Docker restart policy | restart: always |
| Host reboot | Server restarted |
| Orchestrator recovery | Kubernetes self-healing |
| Manual restart | docker restart |
1. Application Crash
The most common reason is application failure.
Example
Spring Boot Application
|
Database Connection Failure
|
Unhandled Exception
|
Java Process Exits
|
Container Stops
|
Docker Restarts Container
Common Crash Causes
- Null pointer exceptions
- Database unavailable
- Configuration errors
- Missing environment variables
- Dependency failures
2. Docker Restart Policies
Docker restart policies control automatic restart behavior.
Restart Policy Types
| Policy | Behavior |
|---|---|
| no | Never restart |
| always | Always restart |
| unless-stopped | Restart unless manually stopped |
| on-failure | Restart only on non-zero exit |
Example: Always Restart
docker run --restart=always nginx
Docker Compose Example
services:
api-gateway:
image: api-gateway:1.0
restart: unless-stopped
Restart Policy Flow
Container Crashes
|
Docker Checks Restart Policy
|
Restart Policy Allows Restart
|
Container Starts Again
3. OOMKilled (Out Of Memory)
Linux may kill containers when memory usage becomes excessive.
OOM Flow
Application Memory Leak
|
Container Uses Excess Memory
|
Linux OOM Killer Triggered
|
Process Killed
|
Container Exits
|
Docker Restarts Container
How to Check OOMKilled
docker inspect container-name --format='{{.State.OOMKilled}}'
Common Symptoms
- Container restarts repeatedly
- Application slow before crash
- Exit code 137
Fix
deploy:
resources:
limits:
memory: 1G
Java Memory Tuning
JAVA_OPTS=-Xms512m -Xmx768m
4. Failed Health Checks
Containers may restart if health checks fail.
Docker Health Check Example
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/actuator/health"]
interval: 30s
timeout: 5s
retries: 3
Health Check Failure Flow
Health Check Runs
|
Application Not Responding
|
Health Check Fails
|
Container Marked Unhealthy
|
Orchestrator Restarts Container
5. Kubernetes Self-Healing
Kubernetes automatically restarts failed containers.
Kubernetes Self-Healing Flow
Container Crashes
|
Kubelet Detects Failure
|
Pod Restarted
|
Traffic Restored
Kubernetes Restart Policies
| Policy | Behavior |
|---|---|
| Always | Always restart |
| OnFailure | Restart only on failure |
| Never | Do not restart |
6. Host Reboot
Containers may restart automatically after server reboot.
Flow
Host Server Reboots
|
Docker Daemon Starts
|
Restart Policies Applied
|
Containers Restarted
7. Crash Loop
Sometimes containers continuously restart in a loop.
Crash Loop Flow
Container Starts
|
Application Fails
|
Container Exits
|
Restart Policy Triggers
|
Container Starts Again
|
Same Failure Repeats
Symptoms
STATUS:
Restarting (1) 5 seconds ago
Common Crash Loop Causes
- Bad configuration
- Missing database
- Wrong environment variables
- Port conflicts
- Application bugs
How to Debug Restarting Containers
Check Status
docker ps -a
View Logs
docker logs container-name
Inspect Container
docker inspect container-name
Check Exit Code
docker inspect container-name --format='{{.State.ExitCode}}'
Common Exit Codes
| Exit Code | Meaning |
|---|---|
| 0 | Success |
| 1 | General failure |
| 137 | OOMKilled |
| 127 | Command not found |
Real Production Issue Example
Problem
API Gateway container restarted continuously in production.
Symptoms
Restarting (1) 10 seconds ago
Investigation
docker logs api-gateway
Error
Could not resolve placeholder 'PAYMENT_SERVICE_URL'
Root Cause
Missing environment variable.
Fix
environment:
PAYMENT_SERVICE_URL: http://payment-service:8084
Production Restart Architecture
+------------------------------------------------------+
| Docker Engine / Kubernetes |
+------------------------------------------------------+
| Health Checks |
| Restart Policies |
| Self-Healing Logic |
+------------------------------------------------------+
| API Gateway |
| Portfolio Service |
| Payment Service |
| Redis |
| MySQL |
+------------------------------------------------------+
| Monitoring + Alerting |
+------------------------------------------------------+
How to Prevent Unnecessary Restarts
- Use health checks properly
- Validate environment variables
- Add retry logic for databases
- Set memory limits carefully
- Monitor logs and metrics
- Use stable dependency startup ordering
- Handle exceptions gracefully
Monitoring Restarting Containers
Docker Stats
docker stats
Container Events
docker events
Prometheus Metrics
container_restart_count
Enterprise Monitoring Architecture
Containers
|
Prometheus
|
Grafana Dashboards
|
Alertmanager
|
Slack / Email Alerts
Common Interview Mistakes
- Saying Docker restarts containers automatically by default
- Ignoring restart policies
- Not mentioning OOMKilled
- Ignoring health checks
- Confusing Docker and Kubernetes restart behavior
Best Restart Policy for Production
Recommended
restart: unless-stopped
because:
- Restarts after crashes
- Restarts after server reboot
- Does not restart manually stopped containers
Interview Answer
Containers restart automatically when the main process inside the container exits and Docker restart policies or orchestration platforms like Kubernetes are configured to restart failed containers for high availability.
Common reasons include application crashes, missing environment variables, database connection failures, failed health checks, out-of-memory kills, host reboots, and orchestration self-healing mechanisms.
In production, restart policies such as always or
unless-stopped are commonly used together with monitoring,
health checks, and resource limits to maintain service reliability.
Quick Summary Table
| Reason | Typical Fix |
|---|---|
| Application crash | Fix code/configuration |
| OOMKilled | Increase memory/tune app |
| Failed health check | Fix application health endpoint |
| Missing environment variable | Update Compose/K8s config |
| Dependency unavailable | Add retries/health checks |
| Restart policy | Adjust restart configuration |
Useful Internal Links
- Docker Interview Questions
- Docker Compose Interview Questions
- Kubernetes Interview Questions
- DevOps Interview Questions
- Microservices Interview Questions
- Linux Interview Questions
Final Conclusion
Containers restart automatically to improve application availability and resilience in production systems.
Automatic restarts are usually triggered by application crashes, memory issues, failed health checks, restart policies, or orchestration self-healing mechanisms. Proper monitoring, debugging, health checks, and resource management are essential to prevent endless crash loops and maintain stable containerized infrastructure.