menu 学习笔记
search self_improvement
目录

CentOS 7 安装 Docker 的几种方式

huty
huty 2022年11月09日  ·  阅读 1,561

YUM 安装

1、移除旧的 Docker

yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine

2、配置 Docker 官方的 YUM 仓库

yum install -y yum-utils
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

3、查询可用版本

yum list docker-ce --showduplicates|grep "^doc"|sort -r

4、安装 Docker

  • 安装最新版本
yum install docker-ce docker-ce-cli containerd.io -y
  • 安装指定版本
yum install docker-ce-版本号 -y

示例(安装 3:20.10.23-3.el7 版本):

yum install docker-ce-3:20.10.23-3.el7

5、启动 Docker 服务

  • 配置开机自启动
systemctl enable docker
  • 重启 Docker 服务
systemctl restart docker

6、验证 Docker 是否安装成功

  • 查看 Docker 版本号
docker version
  • 查看 Docker 详细信息
docker info

说明:Docker 安装后文件的位置

  • 执行文件目录: /usr/bin
  • 配置文件目录: /etc/docker
  • 数据文件目录: /var/lib/docker

二进制安装

1、准备相关目录

此处 Docker 二进制文件目录为 /opt/docker/bin 、Docker 数据目录为 /opt/docker/data

mkdir -pv /opt/docker/{bin,data}

2、创建 Docker 用户组

groupadd -r docker

3、准备系统环境

modprobe br_netfilter
echo 'net.bridge.bridge-nf-call-ip6tables = 1' > /etc/sysctl.d/docker.conf
echo 'net.bridge.bridge-nf-call-iptables = 1' >> /etc/sysctl.d/docker.conf
echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.d/docker.conf
sysctl --system

4、下载官方的二进制包

下载地址:
x86_64:https://download.docker.com/linux/static/stable/x86_64/
aarch64:https://download.docker.com/linux/static/stable/aarch64/
示例如下:

wget https://download.docker.com/linux/static/stable/x86_64/docker-20.10.21.tgz

5、解压二进制包文件到自定义目录

解压上一步中下载的二进制包到自定义目录(此处将文件解压到 /opt/docker/bin 目录)

tar zxvf docker-20.10.21.tgz -C /opt/docker/bin --strip-components 1

解压后文件夹中包含的文件如下:

-rwxr-xr-x 1 1000 1000 38995448 Oct 26 02:03 containerd
-rwxr-xr-x 1 1000 1000  7446528 Oct 26 02:03 containerd-shim
-rwxr-xr-x 1 1000 1000  9646080 Oct 26 02:03 containerd-shim-runc-v2
-rwxr-xr-x 1 1000 1000 20750336 Oct 26 02:03 ctr
-rwxr-xr-x 1 1000 1000 48047088 Oct 26 02:03 docker
-rwxr-xr-x 1 root root 44949504 Nov  8 10:00 docker-compose
-rwxr-xr-x 1 1000 1000 57789848 Oct 26 02:03 dockerd
-rwxr-xr-x 1 1000 1000   765808 Oct 26 02:03 docker-init
-rwxr-xr-x 1 1000 1000  2555160 Oct 26 02:03 docker-proxy
-rwxr-xr-x 1 1000 1000 13847864 Oct 26 02:03 runc

6、配置二进制文件权限

chmod -R 755 /opt/docker/bin

7、配置环境变量

vim ~/.bashrc
# Docker Env Begin
export DOCKER_HOME=/opt/docker/bin
export PATH=$PATH:$DOCKER_HOME
# Docker Env End
vim /etc/profile
# Docker Env Begin
export DOCKER_HOME=/opt/docker/bin
export PATH=$PATH:$DOCKER_HOME
# Docker Env End
source /etc/profile

8、编辑 docker.service 文件

vim /lib/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
Requires=docker.socket

[Service]
Type=notify
Environment=PATH=/opt/docker/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# 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=/opt/docker/bin/dockerd -H unix://var/run/docker.sock
ExecReload=/bin/kill -s HUP
# 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

9、编辑 docker.socket 文件

vim /lib/systemd/system/docker.socket
 [Unit]
Description=Docker Socket for the API
PartOf=docker.service

[Socket]
ListenStream=/var/run/docker.sock
SocketMode=0660
SocketUser=root
SocketGroup=docker

[Install]
WantedBy=sockets.target

10、编辑 daemon.json 文件

vim /etc/docker/daemon.json
{
  "exec-opts" : ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-level": "warn",
  "bip": "10.21.21.1/24",
  "data-root": "/opt/docker/data",
  "log-opts": {
    "max-size": "100m",
    "max-file": "3"
  },
  "registry-mirrors": ["https://registry.docker-cn.com","https://docker.mirrors.ustc.edu.cn","https://dockerproxy.com","https://ccr.ccs.tencentyun.com","http://hub-mirror.c.163.com"],
   "live-restore": true
}

11、启动 Docker 服务

systemctl enable docker
systemctl restart docker

12、验证 Docker 是否安装成功

docker version

安装 Docker Compose

1、进入 Docker 二进制文件目录

默认目录:

cd /usr/bin

自定义目录:

cd /opt/docker/bin

2、下载 Docker Compose 二进制文件

下载地址:
https://github.com/docker/compose/releases
示例如下:

wget https://github.com/docker/compose/releases/download/v2.12.2/docker-compose-linux-x86_64 -O docker-compose

3、配置 Docker Compose 二进制文件权限

chmod 755 docker-compose
chown 1000.1000 docker-compose

4、验证 Docker Compose 是否安装成功

docker-compose version
分类: CentOS Docker
标签: CentOS Docker

评论已关闭