Docker Engine (Part 8)
Configuring Docker Container Hostnames, Restart Policies, and Live Restore
In production environments, running a container is only half the battle. You also need applications inside containers to identify themselves predictably, recover automatically from unexpected crashes, and stay online even when the Docker daemon itself restarts for maintenance.
This guide covers three essential reliability and configuration features in Docker: setting custom container hostnames (--hostname), configuring automatic container recovery (--restart), and enabling zero-downtime daemon restarts with Live Restore (live-restore).
1. Container Name vs. Container Hostname (--hostname)
When you launch a container with --name webapp:
docker container run -d --name webapp nginx
You are setting the Docker object name used by the Docker CLI and daemon—not the Linux hostname inside the container. By default, Docker sets the container's internal hostname to its 12-character short Container ID (e.g., 8a9f2b1c4e1d).
Why Set a Custom Hostname?
Many applications rely on the OS hostname to generate server URLs, tag application logs, or register nodes in a cluster. Leaving it as a random Container ID makes log aggregation and debugging harder.
To assign a human-readable internal hostname, use the --hostname (or -h) flag:
docker container run -it --name webapp --hostname webapp-prod ubuntu
Verify inside the container:
hostname
# Output: webapp-prod
(Note: Setting --hostname updates /etc/hostname and /etc/hosts inside the container, allowing processes inside to resolve the custom name locally without affecting how other containers resolve it over Docker DNS.)
2. Understanding Container Exit Codes
Before configuring restart rules, it helps to know why a container stopped. Every process exits with a numeric exit code (visible in docker container ls -a or docker container inspect):
Exit Code | Meaning | Common Cause |
| Clean / Successful Exit | The container's task finished normally, or the app caught |
| Application Error | Invalid CLI flag, unhandled exception, or missing config file. |
| Immediate Kill ( | Container exceeded memory limits ( |
3. Automatic Recovery with Restart Policies (--restart)
If a web server crashes at 3:00 AM due to a transient error, you want Docker to bring it back online immediately while you investigate the logs later. You configure this behavior using the --restart flag:
docker container run -d --name webapp --restart unless-stopped nginx
docker container run -d --name webapp --restart unless-stopped nginx
The Four Restart Policies
Policy | Restarts on Clean Exit (0)? | Restarts on Crash (Non-Zero)? | Restarts on Daemon Restart? | Respects Manual docker stop? |
| No | No | No | Yes |
| No | Yes (up to optional limit) | Only if it previously failed | Yes |
| Yes | Yes | Yes (even if manually stopped earlier!) | No (restarts on daemon reboot) |
| Yes | Yes | Yes (unless manually stopped) | Yes |
Choosing the Right Policy
on-failure:5: Best for batch jobs or worker scripts—restarts on errors up to 5 times, and stays stopped once the job finishes cleanly (exit 0).unless-stopped(Recommended for Web Apps): Keeps services running across crashes and server reboots, while respecting an administrator's manualdocker container stopcommand during maintenance.always: Useful for critical infrastructure agents that must always come back up when the host reboots, even if someone manually stopped them earlier.
Important Rules to Remember:
- 10-Second Startup Threshold: A restart policy only activates after a container has stayed up for at least 10 seconds. If a container fails immediately on boot (e.g., due to a syntax error), Docker applies an exponential backoff delay so it doesn't trap your CPU in an infinite crash loop.
- Updating Existing Containers: You can add or change a restart policy on an already running container without recreating it:
docker container update --restart unless-stopped webapp
4. Zero-Downtime Daemon Restarts with Live Restore
By default, when you stop or restart the Docker daemon (sudo systemctl restart docker), every running container on the host is shut down. During Docker Engine patch upgrades or daemon configuration reloads, this causes unnecessary downtime.
Because modern Docker delegates actual container execution to containerd and runC via containerd-shim, you can configure dockerd to detach cleanly from running containers when it stops and reattach to them when it starts back up. This feature is called Live Restore.
Step 1: Enable live-restore in /etc/docker/daemon.json
Edit (or create) /etc/docker/daemon.json and set "live-restore": true:
{
"live-restore": true
}
Step 2: Reload the Docker Daemon Without Dropping Containers
Use systemctl reload (which sends SIGHUP to dockerd) instead of restart so existing containers aren't interrupted while activating the setting:
sudo systemctl reload docker
Verify that Live Restore is active:
docker system info | grep -i "live restore"
# Output: Live Restore Enabled: true
Now, if dockerd crashes or you run sudo systemctl restart docker during a patch upgrade, your standalone containers remain up and serving traffic uninterrupted.
(Note: Live Restore applies to standalone containers—not Docker Swarm services—and is supported across minor patch updates rather than major engine version jumps.)
Quick Reference Cheat Sheet
Feature | Command / Config | Purpose |
Custom Hostname |
| Sets internal OS hostname instead of Container ID |
Auto-Restart (Web Apps) |
| Restarts on crash/reboot unless manually stopped |
Auto-Restart (Batch Jobs) |
| Restarts only on non-zero exit codes (max 3 tries) |
Update Policy Live |
| Changes restart policy on an existing container |
Live Restore |
| Keeps containers running during |
Comments
Join the discussionNo comments yet. Be the first to comment.