How to Automatically Restart Docker Containers
Docker containers are widely used for deploying modern applications and services. However, one common issue administrators face is containers stopping unexpectedly.
If a container crashes or the server restarts, your application may go offline unless the container restarts automatically.
Fortunately, Docker provides built-in restart policies that ensure containers start automatically.
In this guide, we will explain how to configure Docker container automatic restart using several methods.
Why Docker Containers Stop
Before configuring automatic restart, it helps to understand why containers stop.
Common reasons include:
- server reboot
- application crash
- memory limit reached
- manual container stop
- Docker daemon restart
Without restart policies, containers remain stopped until manually restarted.
Docker Restart Policies Explained
Docker includes several restart policies.
no
The container will not restart automatically.
--restart=no
always
The container always restarts when it stops.
--restart=always
unless-stopped
Container restarts automatically unless manually stopped.
--restart=unless-stopped
This is the most commonly used policy.
on-failure
Container restarts only if it exits with an error.
--restart=on-failure
Method 1: Enable Restart When Running Container
You can enable automatic restart when creating a container.
Example:
docker run -d --restart unless-stopped nginx
This ensures the container restarts after crashes or system reboot.
Method 2: Update Existing Container Restart Policy
If the container is already running, you can update its restart policy.
Example:
docker update --restart unless-stopped container_name
Check configuration using:
docker inspect container_name
Method 3: Configure Restart in Docker Compose
Docker Compose allows you to define restart policies in the configuration file.
Example docker-compose.yml:
version: '3'
services:
wordpress:
image: wordpress
restart: unless-stopped
ports:
- "8080:80"
Run the container:
docker-compose up -d
Now the container will automatically restart.
Method 4: Restart Containers After System Reboot
If the server reboots, Docker restart policies ensure containers start automatically.
Check containers:
docker ps
If restart policies are configured correctly, containers will start immediately.
Monitoring Docker Containers
To ensure containers remain healthy, monitor them regularly.
Useful commands:
docker ps
docker logs container_name
docker stats
These commands help detect performance problems before crashes occur.
Best Hosting for Docker Deployments
Running Docker containers requires reliable infrastructure.
Managed cloud platforms provide scalable environments for containerized applications.
For example, Cloudways managed hosting offers optimized cloud servers that work well for Docker-based applications.
You can explore their platform here:
For users looking for simple WordPress hosting environments before moving to Docker deployments, Bluehost is another good option.
Choosing reliable infrastructure improves uptime and reduces downtime caused by server failures.
Best Practices for Docker Reliability
To ensure containers remain stable, follow these best practices.
Use Restart Policies
Always configure restart policies in production environments.
Monitor Container Logs
Check logs regularly to identify application errors.
Limit Resource Usage
Prevent containers from consuming excessive memory or CPU.
Example:
docker run -d --memory="512m" nginx
Use Health Checks
Docker supports health checks that monitor container status.
Example:
HEALTHCHECK CMD curl --fail http://localhost || exit 1
Conclusion
Docker containers can stop unexpectedly due to crashes, server reboots, or resource issues.
Using Docker restart policies ensures that containers automatically recover without manual intervention.
The most commonly used policy is:
--restart unless-stopped
By configuring restart policies, monitoring logs, and using reliable infrastructure, you can ensure that your containerized applications remain available at all times.
