[docker series] container keeps alive after container self-start and daemon stop

insert image description here

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 docker is upgraded. It mainly contains 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, 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 by the author. In addition, docker also provides other restart strategies. --restartThe value 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-retriesindicates 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, a container in a good running state usually does not have the problem of inability to restart due to program running errors after restarting the server or restarting the docker daemon, so I don't 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 status 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 /etc/docker/daemon.jsonto add the above line to the configuration file, so that after the daemon process is stopped, the container will not stop the service.

insert image description here

Guess you like

Origin blog.csdn.net/hanxiaotongtong/article/details/124023613