Docker容器自动重启,restart=always参数
前言
服务器重启了,Docker容器没跟着起来?或者容器崩了,不自动重启?本文讲怎么配置自动重启。
一、restart参数
Docker用 --restart 参数控制容器的重启策略。
四种策略
| 策略 | 说明 |
|---|---|
no |
不自动重启(默认) |
on-failure |
非正常退出时重启 |
always |
总是自动重启 |
unless-stopped |
除非手动停了,否则一直重启 |
二、always策略
用法
docker run -d --name nginx --restart=always nginx
什么时候会重启
- 容器崩了(非正常退出)
- Docker服务重启
- 服务器重启(只要Docker开机自启)
特点
- 不管什么原因停了,都会自动起来
- 服务器重启后也会自动启动
三、unless-stopped策略
用法
docker run -d --name nginx --restart=unless-stopped nginx
和always的区别
- always:就算你手动停了,重启Docker后还是会起来
- unless-stopped:如果你手动停了,重启Docker后就不起来了
推荐用unless-stopped,更灵活。
四、on-failure策略
用法
docker run -d --name nginx --restart=on-failure:5 nginx
说明
- 只有非正常退出时才重启
:5表示最多重启5次,超过就不重启了
什么时候用
比如批处理任务,失败了重试几次,一直失败就算了。
五、no策略(默认)
docker run -d --name nginx --restart=no nginx
不自动重启,停了就停了。
六、修改已有容器的重启策略
运行中的容器
docker update --restart=always 容器名
不用删容器,直接改。
例子
docker update --restart=always nginx
docker update --restart=unless-stopped mysql
七、Docker Compose里的配置
services:
nginx:
image: nginx
restart: always
或者:
restart: unless-stopped
八、服务器开机自启
1. Docker服务开机自启
systemctl enable docker
2. 容器开机自启
只要容器设了 --restart=always 或 unless-stopped,Docker启动后容器就会自动启动。
验证
重启服务器试试:
reboot
重启完看看容器起来没:
docker ps
九、查看重启策略
docker inspect -f '{{.HostConfig.RestartPolicy.Name}}' 容器名
输出:
always
十、常见坑
坑1:设了always,服务器重启后没起来
原因: Docker服务没设开机自启。
解决:
systemctl enable docker
坑2:手动停了,又自动起来了
用了 always 策略,手动停了也会起来。
解决: 想彻底停掉:
docker update --restart=no 容器名
docker stop 容器名
坑3:一直重启,死循环
容器启动就崩,崩了又重启,死循环。
检查:
docker logs 容器名
看看为什么启动失败,先把问题修了。
坑4:on-failure重启次数
--restart=on-failure:5
最多重启5次,超过就不重启了。
注意: 这个5是次数,不是间隔。
十一、推荐配置
生产环境推荐
docker run -d \
--name nginx \
--restart=unless-stopped \
-p 80:80 \
nginx
- 服务器重启自动起来
- 手动停了就不自动起
- 灵活
数据库推荐
docker run -d \
--name mysql \
--restart=unless-stopped \
-p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=123456 \
-v /data/mysql:/var/lib/mysql \
mysql:5.7
总结
记住:
--restart=always总是自动重启--restart=unless-stopped推荐,手动停了不自动起- 改已有容器:
docker update --restart=xxx 容器名 - 服务器开机自启要先
systemctl enable docker
最常用:
docker run -d --name nginx --restart=unless-stopped nginx
遇到问题加QQ23979811 协助处理
Linux查看所有服务,systemctl命令大全
前言
CentOS7 之后用 systemctl 管理服务,代替了老的 service 和 chkconfig 命令。本文把常用的 systemctl 命令都整理出来,新手照着用就行。
查看服务
查看所有服务
systemctl list-units --type=service
列出所有正在运行的服务。
查看所有服务(包括没运行的)
systemctl list-unit-files --type=service
查看正在运行的服务
systemctl list-units --type=service --state=running
查看某个服务状态
systemctl status 服务名
比如看 nginx 状态:
systemctl status nginx
输出会显示服务是否在运行、PID、内存占用、最近日志等。
启动和停止服务
启动服务
systemctl start 服务名
比如:
systemctl start nginx
停止服务
systemctl stop 服务名
比如:
systemctl stop nginx
重启服务
systemctl restart 服务名
比如:
systemctl restart nginx
重载配置(不重启)
修改了配置文件后,用 reload 重载,不用重启:
systemctl reload 服务名
比如:
systemctl reload nginx
平滑重启(reload 不行就用这个)
systemctl reload-or-restart 服务名
设置开机自启
开启开机自启
systemctl enable 服务名
比如:
systemctl enable nginx
关闭开机自启
systemctl disable 服务名
比如:
systemctl disable nginx
查看是否开机自启
systemctl is-enabled 服务名
输出 enabled 就是开机自启,disabled 就是不是。
其他常用操作
查看服务是否在运行
systemctl is-active 服务名
输出 active 就是在运行,inactive 就是没运行。
查看服务日志
journalctl -u 服务名
比如看 nginx 日志:
journalctl -u nginx
实时看服务日志
journalctl -u 服务名 -f
和 tail -f 一样,实时输出最新日志。
查看服务开机自启状态
systemctl list-unit-files --type=service | grep enabled
列出所有开机自启的服务。
常见服务名
| 服务名 | 对应服务 |
|---|---|
| nginx | Nginx 网页服务 |
| httpd | Apache 网页服务 |
| mysqld | MySQL 数据库 |
| firewalld | 防火墙 |
| crond | 定时任务 |
| sshd | SSH 远程连接 |
| php-fpm | PHP 解析 |
| redis | Redis 缓存 |
常见坑
坑1:服务名写错了
执行 systemctl 报错 Unit xxx.service could not be found。
原因: 服务名写错了。
解决: 先列一下所有服务,找到正确的服务名:
systemctl list-unit-files --type=service | grep 关键词
坑2:启动了服务但还是访问不了
服务显示 active,但外部还是访问不了。
原因: 防火墙没开端口,或者服务本身配置错了。
解决: 先看防火墙开了没,再看服务日志:
journalctl -u 服务名
坑3:开机自启了但重启后没起来
设置了 enable,但重启后服务还是没启动。
原因: 服务配置有问题,启动失败了。
解决: 看日志找原因:
journalctl -u 服务名
坑4:用了老命令 service
执行 service nginx start 能用,但不推荐了。
原因: 这是老命令,新系统都用 systemctl。
解决: 换成 systemctl start nginx。
总结
| 操作 | 命令 |
|---|---|
| 查看所有服务 | systemctl list-units --type=service |
| 查看服务状态 | systemctl status 服务名 |
| 启动服务 | systemctl start 服务名 |
| 停止服务 | systemctl stop 服务名 |
| 重启服务 | systemctl restart 服务名 |
| 重载配置 | systemctl reload 服务名 |
| 开机自启 | systemctl enable 服务名 |
| 关闭开机自启 | systemctl disable 服务名 |
| 查看日志 | journalctl -u 服务名 |
systemctl 就这几个常用命令,记住就够用了。
遇到问题加QQ23979811 协助处理
Linux服务管理,systemctl常用命令汇总
前言
CentOS7 装完软件,怎么启动?怎么开机自启?服务挂了怎么查日志?这些都需要用到 systemctl 命令。CentOS7 开始用 systemd 代替了原来的 service 命令,systemctl 就是 systemd 的管理工具。本文就把 systemctl 常用命令汇总一下,日常管理服务够用了。
启动和停止服务
启动服务
systemctl start nginx
停止服务
systemctl stop nginx
重启服务
systemctl restart nginx
重新加载配置
systemctl reload nginx
注意: reload 和 restart 的区别:
reload:不重启进程,重新加载配置,不中断服务restart:重启进程,会短暂中断服务
查看服务状态
查看服务状态
systemctl status nginx
输出会显示服务是不是在运行、PID、内存占用、最近的日志等。
查看服务是不是在运行
systemctl is-active nginx
输出 active 就是在运行,inactive 就是没运行。
查看服务是不是开机自启
systemctl is-enabled nginx
输出 enabled 就是开机自启,disabled 就是不自启。
开机自启管理
设置开机自启
systemctl enable nginx
取消开机自启
systemctl disable nginx
同时启动并设置开机自启
systemctl enable --now nginx
一条命令搞定,不用分两步。
查看日志
查看服务日志
journalctl -u nginx
实时跟踪日志
journalctl -u nginx -f
和 tail -f 类似,实时滚动显示日志。
查看最近100行日志
journalctl -u nginx -n 100
查看今天的日志
journalctl -u nginx --since today
查看指定时间的日志
journalctl -u nginx --since "2026-10-05 10:00:00" --until "2026-10-05 11:00:00"
查看所有服务
列出所有正在运行的服务
systemctl list-units --type=service
列出所有服务(包括没运行的)
systemctl list-unit-files --type=service
按状态过滤
systemctl list-units --type=service --state=running
常用服务管理示例
启动 Nginx 并设置开机自启
systemctl start nginx
systemctl enable nginx
或者一条命令:
systemctl enable --now nginx
重启 Nginx
systemctl restart nginx
查看 Nginx 状态
systemctl status nginx
查看 Nginx 日志
journalctl -u nginx -f
常见坑
坑1:还在用 service 命令
CentOS6 用 service nginx start,CentOS7 开始用 systemctl start nginx。老命令还能用,但新系统推荐用 systemctl。
坑2:改了配置不生效
改了 Nginx 配置,忘了 reload,结果配置没生效。
解决: 改完配置记得 reload:
systemctl reload nginx
坑3:服务启动失败不知道为啥
服务启动失败,只看状态不知道具体原因。
解决: 看日志:
journalctl -u nginx
坑4:重启服务器后服务没自启
装完软件启动了,但没设置开机自启,重启之后服务就没了。
解决: 装完软件记得设置开机自启:
systemctl enable nginx
总结
systemctl 常用命令:
| 命令 | 作用 |
|---|---|
systemctl start 服务名 |
启动服务 |
systemctl stop 服务名 |
停止服务 |
systemctl restart 服务名 |
重启服务 |
systemctl reload 服务名 |
重新加载配置 |
systemctl status 服务名 |
查看服务状态 |
systemctl enable 服务名 |
设置开机自启 |
systemctl disable 服务名 |
取消开机自启 |
systemctl is-active 服务名 |
查看是否在运行 |
systemctl is-enabled 服务名 |
查看是否开机自启 |
journalctl -u 服务名 -f |
实时查看日志 |
记住这几个,CentOS7+ 日常管理服务就够用了。
遇到问题加QQ23979811 协助处理