docker常用命令及设置开机自启

该文介绍了管理Docker的基本命令,包括启动和停止Docker服务,查看其状态和版本,以及列举容器。此外,还详细阐述了如何设置Docker开机自启以及容器的自动重启策略,包括`--restart`参数的不同选项,如`no`,`on-failure`,`always`,和`unless-stopped`。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

常用基本命令

# 启动docker
systemctl start docker
# 停止dokcer
systemctl stop docker
# 查看docker状态
systemctl status docker 
# 重启docker
systemctl restart docker
# 设置docker 开机自启
systemctl enable docker
# 查看docker 版本
docker version
# 查看docker 镜像
docker images
# 查看docker 运行列表
docker ps 
# 查看docker (运行、停止)
docker ps -aq
# 删除容器
docker rm -f 容器ID/容器名称(CONTAINER ID/NAMES)
#  删除多个容器(空格隔开)
docker rm -f 容器ID/容器名称 容器ID/容器名称 ...
# 删除全部容器
docker rm -f $(docker ps -aq)

设置docker开机自启

systemctl enable docker

设置容器自启

1. 创建容器时设置:

docker run -d --restart=always --name 容器名称 镜像名称
# 例如:
docker  run --restart=always 56f0b18af626

2. 更新已有容器设置:

docker update --restart=always 容器ID
# 例如:
docker  update --restart=always 56f0b18af626

–restart具体参数,详见官方文档说明

PolicyResult
noDo not automatically restart the container when it exits. This is the default. 默认设置,容器退出时不重启容器。
on-failure[:max-retries]Restart only if the container exits with a non-zero exit status. Optionally, limit the number of restart retries the Docker daemon attempts. 在容器非正常退出时重启容器,最多重启max-retries次。
alwaysAlways restart the container regardless of the exit status. When you specify always, the Docker daemon will try to restart the container indefinitely. The container will also always start on daemon startup, regardless of the current state of the container. 总是重启容器。
unless-stoppedAlways restart the container regardless of the exit status, including on daemon startup, except if the container was put into a stopped state before the Docker daemon was stopped. 总是重启容器,除非在Docker守护程序停止之前容器就处于停止状态。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值