This article introduces how to ensure the normal operation of the container service when the container is self-started and the docker daemon process hangs or the docker is upgraded. Mainly consists of three parts
First, the daemon process starts automatically
When we installed docker, we introduced the command to start the docker daemon as follows, which needs to be executed manually.
systemctl start docker
If we want to start the Docker daemon automatically when the linux server starts, without manual intervention, just use the following command
systemctl enable docker.service;
You can use the following two commands in the appropriate scenario according to your own needs.
# 删除docker守护进程开机自启动配置
systemctl disable docker.service;
# 重新配置docker守护进程开机自启动
systemctl reenable docker.service;
2. The container starts automatically
After the docker daemon is automatically started, if we want some containers to automatically start and provide services, we can use the following command when the container is created:
# 注意`--restart unless-stopped`是能够实现自启动的关键参数
docker run -d --restart unless-stopped nginx
If we already have a runtime container and want it to increase the ability of the container to start automatically, use the following command
docker update --restart unless-stopped nginx
Restart Policy Description
Unless-stopped is the most commonly used restart strategy of the author, in addition to docker also provides other restart strategies, the value of --restart
can be any of the following:
restart policy flag | describe |
---|---|
no | Do not automatically restart containers. (default) |
on-failure[:max-retries] | If the container exits due to a program execution error (non-normal exit), restart the container. The optional configuration :max-retries indicates the maximum number of attempts to restart the container. If the number of times is exceeded, the restart will not be restarted. |
always | Always restart the container if it is in a stopped state. Exception: If the container was manually stopped by the operator, the always restart policy will only continue after the Docker daemon restarts or the container itself is manually restarted. |
unless-stopped | Containers that were already in the stopped state before stopping the Docker daemon will not be restarted. Others are the same as always. |
Why do I often use the unless-stopped restart strategy?
- There are many containers on one server, some of which I stopped manually (active behavior). I don't want these containers to run automatically even after restarting the server or restarting the docker daemon. So I don't have to always.
- Before restarting the server or restarting the docker daemon, the container in a good running state usually does not have the problem of failure to restart due to program running errors after restarting the server or restarting the docker daemon, so I do not use on-failure.
3. Ensure that the container service is normal during the stop of the daemon process
The above discussion in this article is how to ensure the running state of the container after the server restarts and the docker daemon restart, and try not to require manual participation. The main content of this section is: if we want to upgrade the version of docker, we need to stop the daemon process of docker, but we do not want to affect the container to provide services to users. That is: The docker daemon process is stopped, and I want to ensure that the docker container process is still running, how to do this?
By default, when the Docker daemon terminates, it shuts down all containers running on top of it. But it can be configured to keep the container running when the daemon process is unavailable. This feature is called live-restore . live-restore helps reduce the time a container is out of service due to daemon crashes, planned outages, or upgrades.
{
"live-restore": true
}
The configuration method is to add the above line to the /etc/docker/daemon.json
configuration file, so that after the daemon process stops, the container will not stop the service.
Welcome to pay attention to my announcement number: Antetokounmpo, reply 003 and present the PDF version of the column "The Way of Docker Cultivation" where this article is located, and more than 30 excellent docker articles. Antetokounmpo Blog: zimug.com
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。