首页 > 其他分享 >How to enable auto restart of a docker container on system reboot ?

How to enable auto restart of a docker container on system reboot ?

时间:2023-06-17 18:11:43浏览次数:48  
标签:enable container auto will stopped docker Docker restart

How to enable auto restart of a docker container on system reboot ?

https://amalgjose.com/2021/02/12/how-to-enable-auto-restart-of-a-docker-container-on-system-reboot/#:~:text=How%20to%20enable%20auto%20restart%20of%20a%20docker,Ensure%20the%20docker%20container%20has%20restart%20policy%20configured.

启动docker服务

设置docker容器重启属性

If we reboot the machine, all the services in the machine will get stopped and during the restart, only the services that are configured with auto-restart will only start. To start all the docker containers on system reboot, we need to ensure the following things.

1. Ensure docker daemon restarts on system reboot.

The below command works on CentOS/ RHEL and Ubuntu.

sudo systemctl enable docker.service

2. Ensure the docker container has restart policy configured.

docker update --restart=always [container id or container name]

 

https://zhuanlan.zhihu.com/p/383382470

第三个问题需要做镜像的时候设置CMD 或者 ENTRYPOINT

要让docker容器内的服务能在开机加电后自动启动,要解决三个问题:

1.docker服务能随OS启动而启动

2.docker容器能随docker服务启动而启动

3.docker容器内的服务能随docker容器启动而启动

How to Use Docker Restart Policies to Keep Containers Running

重启策略针对monitoring docker instance

失败重启可以设置次数

 

There are currently four different restart policies:

  • no – This policy will never automatically start a container. This is the default policy for all containers created with docker run.
  • always – Docker will ensure the container is always running. If the container stops, it will be immediately restarted. You can still manually stop the container with docker stop but Docker will bring it back up next time the daemon restarts.
  • on-failure – The container will get restarted if it stops because of an error. Docker won’t bring the container up after the daemon restarts.
  • unless-stopped – This functions similarly to always. The difference is that Docker won’t ever restart the container if it has been manually stopped.

 

Restart Loops

Docker includes a couple of safeguards against perpetual restart loops. The first is a mandatory time delay before restart policies activate. Docker won’t begin monitoring restarts until a container has been running for at least 10 seconds. This prevents a failed container from continually restarting.

The other specialist behaviour concerns the docker stop command. Docker will always respect use of docker stop, so the container won’t immediately restart after you run the command. If you actually want to restart the container, use docker restart instead.

 

Limiting Restart Retries

The on-failure restart policy lets you specify how many retries should be attempted. Docker will give up and leave the container in a stopped state if it fails to start multiple times in succession.

docker run httpd:latest --restart on-failure:5

In this example, Docker will try to restart the container five times after a failure (non-zero exit code). If the container fails to start on the fifth attempt, no more retries will be attempted. This option is useful for containers where a persistent starting error is unlikely to be resolved without manual intervention.

 

Start containers automatically

https://docs.docker.com/config/containers/start-containers-automatically/

官网的参数解释。

always会在docker服务启动后, 启动容器

包括用户手动停止的容器

如果用户希望容器重启, 则用 unless-stopped

 

To configure the restart policy for a container, use the --restart flag when using the docker run command. The value of the --restart flag can be any of the following:

FlagDescription
no Do not automatically restart the container. (the default)
on-failure[:max-retries] Restart the container if it exits due to an error, which manifests as a non-zero exit code. Optionally, limit the number of times the Docker daemon attempts to restart the container using the :max-retries option.
always Always restart the container if it stops. If it is manually stopped, it is restarted only when Docker daemon restarts or the container itself is manually restarted. (See the second bullet listed in restart policy details)
unless-stopped Similar to always, except that when the container is stopped (manually or otherwise), it is not restarted even after Docker daemon restarts.

The following example starts a Redis container and configures it to always restart unless it is explicitly stopped or Docker is restarted.

 
 docker run -d --restart unless-stopped redis

This command changes the restart policy for an already running container named redis.

 
 docker update --restart unless-stopped redis

And this command will ensure all currently running containers will be restarted unless stopped.

 
 docker update --restart unless-stopped $(docker ps -q)

 

 

If restart policies don’t suit your needs, such as when processes outside Docker depend on Docker containers, you can use a process manager such as upstart, systemd, or supervisor instead.

 

标签:enable,container,auto,will,stopped,docker,Docker,restart
From: https://www.cnblogs.com/lightsong/p/17487995.html

相关文章

  • PCIe问题举例: Enable SR-IOV导致资源分配失败
    资源分配问题分析过程:1.在shell下和OS都能复制到现象;2.测试CRBBIOS,发现CRBBIOS在shell下没有复制到现象,在OS下复制到现象;3.比较AoqinBIOS和CRBBIOS,发现在setup里面把PCIeSR-IOVdisable,shell下就能正常分配到Region0/2的资源;----看来跟PCIeSR-IOV功能有关4.Region0/2的......
  • 解决方案 | pyautogui实现等待特定图片(对话框)出现(可设置等待超时时间)
    1、问题为了等待某个【转换完毕】的图片(对话框)出现,如何设置?   2、解决方案代码如下下面代码实现的是:设置超时时间为10s,当你在完成前序工作以后,代码开始进入等待,一旦你的特定图片出现了,马上探测到它的位置并且实现点击按钮。(注意:如果等待时间超过了10s,那么就会返回提示:T......
  • 运行python -m uiautomator2 init报错AttributeError: module 'collections' has no a
    报错信息:Traceback(mostrecentcalllast):File"E:\Carte\BB\17-SiteLeadership\alte\IonelBalauta\Aryeht\Task1-Traducetotsite-ul\DoarGoogleWeb\Andreea\Meditatii\Sedinta9(2022)(EMAIL)\BEBE-PARSING-Python(fararedenumire2).p......
  • Android_Uiautomatorviewer闪退问题,已解决
    现象1、cmd运行   (1)cdsdk的tools文件夹下   (2)./uiautomatorviewer2、提示错误信息-Djava.ext.dirs=/Users/env/android-sdk/tools/libisnotsupported.Use-classpathinstead.Error:CouldnotcreatetheJavaVirtualMachine.Error:Afatalexceptionh......
  • 通过APIM 包装Power Automate HTTP Request
    1.首先需要在flow中创建“whenaHTTPrequestisreceived” 当创建好,并且把body填写好保存之后,会返回urlhttps://prod-27.southeastasia.logic.azure.com:443/workflows/febbc789e0ad4ed2b649b5f068bee657/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftr......
  • 经纬恒润AUTOSAR成功适配国芯科技国产车规级芯片
        近日,经纬恒润AUTOSAR基础软件产品INTEWORK-EAS-CP成功适配国芯科技的CCFC2012BC产品家族。同时,经纬恒润完成了对国芯科技的MCAL软件适配和工程集成,为国芯科技提供了全套AUTOSAR解决方案。 基于国芯科技CCFC2012BC完成AUTOSAR适配     通过本次合作,国芯科......
  • C++面试八股文:了解auto关键字吗?
    某日二师兄参加XXX科技公司的C++工程师开发岗位第15面:面试官:了解auto关键字吗?二师兄:嗯,了解一些(我很熟悉)。面试官:说一说auto的用法吧?二师兄:auto主要是为了编译器进行类型推导。比如:autoi=42; //i被推导位int型std::vector<int>vi;for(autoit=vi.cbegin();it!=v......
  • [-003-]-Python3+Unittest+Uiautomation Windows桌面App UI自动化之控件查找
    1、获取所有的子控件:control.GetChildren()2、获取首个子控件:control.GetFirstChildControl()3、获取最后一个子控件:control.GetLastChildControl()4、获取下一个兄弟控件:control.GetNextSiblingControl()5、获取前一个兄弟控件:control.GetPreviousSiblingContr......
  • AutodeskUniversity--Revit开发在BIM项目中的演进及与人工智能的结合(梁裕卿)
    课程说明本课程将分享同济大学建筑设计研究院(集团)有限公司(以下简称TJAD)上海建筑数字建造工程技术研究中心(以下简称上海建筑数字中心)打造Revit插件产品的经验,包括完整的需求,软件架构、开发、第三方程序集的使用、测试及打包过程。同时介绍Revit开发在BIM项目中是如何逐步迭......
  • pyautogui.screenshot('test.png') 报错:PyAutoGUI was unable to import pyscreeze (T
    根据提示需要importpyscreeze还需要Pillow.通过pip3list可以查看到已经安装PyScreeze==0.1.29但是不是我手动安装的,应该是Pillow或者pyautogui下载的时候依赖了pyscreeze并且安装了最新版本0.1.29。通过依赖树可以看到是pyautogui依赖了sudopip3installpipdeptree安装查......