1.1 Docker安装(V20.10.9)
1.1.1 准备工作
操作系统 | IP 地址 |
CentOS 7.9 | 192.168.255.100 |
1.1.2 关闭防火墙
# 关闭防火墙
systemctl stop firewalld
# 禁用防火墙开机自动启动
systemctl disable firewalld
1.1.3 关闭SELinux
# 查看 SELinux 是否开启
getenforce
# 永久关闭 SELinux ,需要重启
sed -i 's/enforcing/disabled/' /etc/selinux/config
# 关闭当前会话的 SELinux ,重启之后无效
setenforce 0
1.1.4 关闭swap分区
# 查看 swap 分区是否存在
free -h
# 关闭当前会话的 swap ,重启之后无效
swapoff -a
# 永久关闭 swap ,需要重启
sed -ri 's/.*swap.*/#&/' /etc/fstab
# 查看 swap 分区是否存在
free -h
1.1.5 卸载旧版本
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
1.1.6 安装相关组件
#yum 安装 gcc 相关
yum -y install gcc gcc-c++
#安装所需软件包
yum install -y yum-utils device-mapper-persistent-data lvm2
1.1.7 设置 stable 镜像仓库
# 官方源
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
# aliyun 源
yum-config-manager \
--add-repo \
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
1.1.8 更新yum软件包索引
yum makecache fast
1.1.9 安装Docker
yum -y install docker-ce-3:20.10.9-3.el7.x86_64 \
docker-ce-cli-1:20.10.9-3.el7.x86_64 \
containerd.io
1.1.10 启动Docker
# 启动 Docker 并设置开机自启
systemctl enable --now docker
#验证 Docker 是否安装成功
docker version
systemctl status docker
1.1.11 配置阿里云镜像加速
sudo mkdir -pv /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
"exec-opts": ["native.cgroupdriver=systemd"],
"registry-mirrors": [
"https://du3ia00u.mirror.aliyuncs.com",
"https://hub-mirror.c.163.com",
"https://mirror.baidubce.com",
"https://registry.docker-cn.com",
"https://docker.mirrors.sjtug.sjtu.edu.cn"
],
"live-restore": true,
"log-driver":"json-file",
"log-opts": {"max-size":"500m", "max-file":"3"},
"max-concurrent-downloads": 10,
"max-concurrent-uploads": 5,
"storage-driver": "overlay2"
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
1.1.12 问题解决
- 如果发现 WARNING: IPv4 forwarding is disabled. Networking will not work. 这样的警告。解决方案如下:
vim /etc/sysctl.conf
net.ipv4.ip_forward=1 # 添加这段代码
systemctl restart network && systemctl restart docker
1.2 常规容器安装示例
1.2.1 安装MySQL
#搜索镜像
docker search mysql
#拉取镜像
docker pull mysql:5.7
#启动容器
docker run -d -p 3306:3306 --name mysql5.7 \
-v /var/mysql5.7/conf:/etc/mysql/conf.d \
-v /var/mysql5.7/logs:/var/log/mysql \
-v /var/mysql5.7/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
-e TZ=Asia/Shanghai \
-e MYSQL_DATABASE=ssm \
--restart=always mysql:5.7 \
--lower_case_table_names=1 \
--character-set-server=utf8mb4 \
--collation-server=utf8mb4_general_ci \
--default-authentication-plugin=mysql_native_password