创建容器时没有添加参数 --restart=always ,导致的后果是:当 Docker 重启时,容器未能自动启动。

现在要添加该参数怎么办呢,方法有二:

1、Docker 命令修改

docker container update --restart=always 容器名字

实例操作如下:

[root@rabbitmq ~]# docker container update --restart=always rabbitmq 
rabbitmq
[root@rabbitmq ~]# systemctl restart docker
[root@rabbitmq ~]# docker ps 
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                                                                                        NAMES
043b63bbda84        redis:5.0.5           "docker-entrypoint.s…"   46 hours ago        Up 7 seconds        0.0.0.0:7005->7005/tcp, 6379/tcp, 0.0.0.0:17005->17005/tcp                                   redis-7005
670ddcd33d91        redis:5.0.5           "docker-entrypoint.s…"   46 hours ago        Up 7 seconds        0.0.0.0:7004->7004/tcp, 6379/tcp, 0.0.0.0:17004->17004/tcp                                   redis-7004
b3785e9796c6        redis:5.0.5           "docker-entrypoint.s…"   46 hours ago        Up 7 seconds        0.0.0.0:7003->7003/tcp, 6379/tcp, 0.0.0.0:17003->17003/tcp                                   redis-7003
42f19c26df01        redis:5.0.5           "docker-entrypoint.s…"   46 hours ago        Up 8 seconds        0.0.0.0:7002->7002/tcp, 6379/tcp, 0.0.0.0:17002->17002/tcp                                   redis-7002
d7f31eec4052        redis:5.0.5           "docker-entrypoint.s…"   46 hours ago        Up 8 seconds        0.0.0.0:7001->7001/tcp, 6379/tcp, 0.0.0.0:17001->17001/tcp                                   redis-7001
874b9667b48c        redis:5.0.5           "docker-entrypoint.s…"   46 hours ago        Up 8 seconds        0.0.0.0:7000->7000/tcp, 6379/tcp, 0.0.0.0:17000->17000/tcp                                   redis-7000
b045b37d244d        rabbitmq:management   "docker-entrypoint.s…"   2 days ago          Up 8 seconds        4369/tcp, 5671/tcp, 0.0.0.0:5672->5672/tcp, 15671/tcp, 25672/tcp, 0.0.0.0:15672->15672/tcp   rabbitmq
[root@rabbitmq ~]# 

2、直接改配置文件

经测试后无效,修改配置文件后,启动容器后,该参数有自动变成了no,修改不生效

首先停止容器,不然无法修改配置文件
配置文件路径为:/var/lib/docker/containers/容器ID
在该目录下找到一个文件 hostconfig.json ,找到该文件中关键字 RestartPolicy
修改前配置:"RestartPolicy":{"Name":"no","MaximumRetryCount":0}
修改后配置:"RestartPolicy":{"Name":"always","MaximumRetryCount":0}
最后启动容器。
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐