这里介绍两种方式安装,压缩包方式和rpm安装包方式
1 rpm方式
1.1 前提条件
1.1.1 内核
Docker运行对内核要求比较高。
- CentOS 7(64位)
- CentOS 6.5(64位)及以后
CentOS 6.5及以后版本时,需要内核版本>=2.6.32-431,CentOS 7是3.10内核
# 查询内核信息
uname -r
1.1.2 Device Mapper
Docker默认使用AUFS作为存储驱动,但是AUFS并没有被包括在Linux的主线内核中。
CentOS中可以使用Device Mapper作为存储驱动,这是在2.6.9内核版本引入的新功能。我们需要先确认是否启用该功能:
ls -l /sys/class/misc/device-mapper
如果没有检测到Device Mapper,需要安装device-mapper软件包:
下载地址:http://mirror.centos.org/centos/7/updates/x86_64/Packages/device-mapper-1.02.170-6.el7_9.5.x86_64.rpm
# 安装
sudo yum install -y device-mapper
# 重新加载dm_mod内核模块
sudo modprobe dm_mod
1.2 安装
1.2.1 在线安装
- 设置源
# 更新yum
sudo yum update
# 卸载旧版docker
sudo yum remove docker docker-common docker-selinux docker-engine
# 安装需要的软件包,yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
# 设置yum源
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
- 安装
# 查看仓库中docker软件版本
yum list docker-ce.x86_64 --showduplicates | sort -r #从高到低列出Docker-ce的版本
# 安装最新版
sudo yum -y install docker-ce
1.2.2 离线安装
下载地址:https://download.docker.com/linux/centos/7/x86_64/stable/Packages/
rpm -ivh docker-ce-19.03.14-3.el7.x86_64.rpm
1.2.3 启动
# 启动并开机启动
sudo systemctl start docker
sudo systemctl enable docker
# 验证,有client和service两部分表示docker安装启动都成功
docker version
1.3 安装 docker-compose
地址:https://docs.docker.com/compose/install/
# 可离线下载,上传至/usr/local/bin 改名为 docker-compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
docker-compose --version
# 增加docker-compose自动补全功能
sudo curl -L https://raw.githubusercontent.com/docker/compose/1.29.2/contrib/completion/bash/docker-compose -o /etc/bash_completion.d/docker-compose
1.4 配置远程访问
vi /usr/lib/systemd/system/docker.service
再 ExecStart 后加上参数
-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
完整信息如下
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service containerd.service
Wants=network-online.target
Requires=docker.socket containerd.service
[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 -H fd:// --containerd=/run/containerd/containerd.sock -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3
# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s
# 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
# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
TasksMax=infinity
# 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
OOMScoreAdjust=-500
[Install]
WantedBy=multi-user.target
重启服务即可
systemctl daemon-reload
systemctl restart docker
2 tar.gz方式
2.1 下载
地址:https://download.docker.com/linux/static/stable/x86_64/
这里下载的是:docker-19.03.14
2.2 安装
将压缩包上传至服务器,解压
tar zxf docker-19.03.14.tgz
将docker 相关命令移到 /usr/bin,方便直接运行命令
mv docker/* /usr/bin/
2.3 自启服务
vi /etc/systemd/system/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
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=infinity
LimitNPROC=infinity
TimeoutStartSec=0
Delegate=yes
KillMode=process
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
添加执行权限
chmod +x /etc/systemd/system/docker.service
配置启动
systemctl daemon-reload
systemctl enable docker.service
systemctl start docker
查看版本
docker -v
2.4 配置
镜像配置
cat > /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
}
EOF
刷新配置
systemctl daemon-reload
systemctl restart docker