docker 如何开启自启
时间: 2023-10-18 22:16:11 浏览: 35
Docker 可以通过在 Linux 系统上使用 systemd 或者 Upstart 来设置自启动。下面以 systemd 为例,具体步骤如下:
1. 创建一个 systemd 的 unit 文件 `/etc/systemd/system/docker.service`,可以使用以下命令:
```
$ sudo nano /etc/systemd/system/docker.service
```
2. 编辑文件内容,将以下内容复制到文件中:
```
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target docker.socket firewalld.service
Wants=network-online.target
Requires=docker.socket
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
```
3. 保存并关闭文件。
4. 重新加载 systemd 配置:
```
$ sudo systemctl daemon-reload
```
5. 启用 Docker 服务:
```
$ sudo systemctl enable docker.service
```
6. 重启系统,Docker 将自动启动。
如果需要禁用 Docker 自启动,可以使用以下命令:
```
$ sudo systemctl disable docker.service
```