【容器化】Linux环境Docker在线与离线安装手册
一、安装前准备
系统要求
- 支持64位CentOS 7/8、Ubuntu 16.04+、Debian 9+等发行版。
- Linux内核版本≥3.10(通过
uname -r
查看)。
Docker 19.03:支持 Linux内核3.10及以上。
Docker 20.10:虽然仍然可以在3.10上运行,但建议使用内核4.0及以上以获得更好的性能和功能。
- 建议使用root权限或sudo命令执行安装。
二、在线安装流程
方式1:一键脚本安装(推荐)
# 官方脚本(默认使用阿里云镜像源)
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
方式2:手动分步安装
1. 更新软件源与依赖包
sudo yum update -y
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
2. 添加Docker官方仓库
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo # 替换为阿里云镜像
3. 安装Docker引擎
sudo yum install -y docker-ce docker-ce-cli containerd.io
4. 启动服务并设置自启
sudo systemctl start docker
sudo systemctl enable docker
三、离线安装流程
1. 下载离线包
-
访问Docker官方下载页获取
docker-<version>.tgz
-
示例版本:
docker-27.1.0.tgz
2. 解压并部署文件
tar -xvf docker-27.1.0.tgz
sudo cp docker/* /usr/bin/
3. 注册系统服务
创建/etc/systemd/system/docker.service
文件,内容参考《linux中离线安装docker》中配置文件模板,保存后执行:
vi /etc/systemd/system/docker.service
docker.service
中的内容为:
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
[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
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
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
4. 启动docker
chmod +x /etc/systemd/system/docker.service #设置权限
systemctl daemon-reload
systemctl start docker #启动Docker
5. 验证状态
systemctl status docker #查看Docker状态
docker -v #查看Docker版本
四、验证安装
检查版本信息
docker version # 显示Client和Server版本
运行测试容器
sudo docker run hello-world # 输出"Hello from Docker!"即成功
五、常见问题解决
权限问题
sudo groupadd docker # 创建docker用户组(若不存在)
sudo usermod -aG docker $USER # 将当前用户加入组
newgrp docker # 刷新组权限
镜像加速配置
编辑/etc/docker/daemon.json
,添加:https://registry.cn-hangzhou.aliyuncs.com
{"registry-mirrors": ["https://<your-mirror-id>.mirror.aliyuncs.com"]}
重启服务
sudo systemctl restart docker