Enhancing Docker Usage Experience: Elegantly Setting Docker Restart Policies for ZenTao
Encountering a Minor Issue with Unexpected Service Stoppage
Recently, I set up two PostGIS databases using Docker, one for the testing environment and another for the production environment. Later, when adjusting the Docker configuration and restarting Docker, I discovered that the database in the testing environment had crashed, while the production environment database was running normally.
Upon investigation, I found that the two services had different restart policies.
To check the current restart policy of a container, use the following command:
docker inspect --format='{{.HostConfig.RestartPolicy.Name}}' container_name_or_id
Consequently, I set the restart policy of the testing environment container testpostgis to unless-stopped:
docker update --restart unless-stopped container_name_or_id
Problem solved perfectly!
Improving the Running Method: Elegant Use of the Open-Source Project ZenTao
If you also use ZenTao for requirement management, you’ll find that with the official running command, ZenTao needs to be manually restarted if Docker restarts. By using the unless-stopped parameter, you can make ZenTao automatically resume service after restarting Docker or the server.
ZenTao Docker startup documentation:
https://www.zentao.net/book/zentaopms/docker-1111.html
Using unless-stop in conjunction with Docker’s auto-start on boot can significantly reduce operational risks.
There are four restart policies:
no
(no restart): This is the default restart policy, suitable for data analysis scripts. For example, a Python script that runs nightly to process log files and generate reports. The script exits naturally after running and doesn't need to restart.on-failure
(restart on failure): Suitable for scheduled task services. For instance, a cron job scheduler is expected to run normally and exit, but if it crashes due to an unexpected error, it needs to restart to ensure subsequent tasks can be executed.always
(always restart): Suitable for web servers. For example, Nginx needs to run 24/7 without interruption to serve website visits.unless-stopped
(always restart unless stopped): Suitable for databases in development environments. Developers may need to frequently modify configurations or data and can manually stop the container instead of deleting it each time.
Summary
Mastering these restart policies can make us more adept at using Docker to run services. Even when encountering events like Docker restarts or server reboots, we can methodically restore various services.