在阿里云Ubuntu实例上安装Docker后,可以通过以下步骤设置开机自启:
方法一:使用 systemctl(推荐)
1. 启用Docker服务开机自启
sudo systemctl enable docker
2. 验证是否设置成功
sudo systemctl is-enabled docker
如果显示 enabled 表示设置成功。
3. 启动Docker服务(如果尚未运行)
sudo systemctl start docker
4. 检查Docker服务状态
sudo systemctl status docker
方法二:使用 update-rc.d(旧版本系统)
对于较旧的Ubuntu版本(14.04及之前):
sudo update-rc.d docker defaults
验证设置
重启系统测试
sudo reboot
重启后检查Docker是否运行
# 检查Docker服务状态
sudo systemctl status docker
# 或运行一个简单的Docker命令
docker version
docker ps
其他相关命令
禁用开机自启
sudo systemctl disable docker
查看所有开机自启服务
sudo systemctl list-unit-files --type=service | grep enabled
注意事项
- 权限问题:确保使用
sudo执行命令 - Docker安装:确保Docker已正确安装
- 系统版本:阿里云Ubuntu实例通常使用较新的LTS版本,推荐使用
systemctl - 防火墙:如果使用阿里云安全组,确保开放相关端口
完整安装和设置流程(如果还未安装Docker)
# 1. 更新包索引
sudo apt-get update
# 2. 安装必要的依赖
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
# 3. 添加Docker官方GPG密钥
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# 4. 添加Docker仓库
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
# 5. 安装Docker
sudo apt-get update
sudo apt-get install docker-ce
# 6. 设置开机自启
sudo systemctl enable docker
sudo systemctl start docker
# 7. 验证安装
sudo docker run hello-world
这样设置后,每次阿里云Ubuntu实例重启时,Docker服务都会自动启动。
CLOUD技术笔记