一、Docker安装(CentOS系统)
官网参考手册:https://docs.docker.com/engine/install/centos/
前期准备
要安装 Docker 引擎,您需要 CentOS 7 或 8 的维护版本。不支持或测试存档版本。
# 查看当前系统版本
[root@localhost ~]# cat /etc/centos-release
CentOS Linux release 7.9.2009 (Core)
#查看内核版本(不低于3.10)
[root@localhost ~]# uname -r
3.10.0-1160.el7.x86_64
查看docker是否已安装
# 如下显示,则未安装
[root@localhost ~]# docker version
-bash: docker: 未找到命令
# 若已安装,需要换版本时,删除旧版本
[root@localhost ~]#
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
安装依赖及Docker
#安装依赖
[root@localhost ~]#
sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
# 依赖安装后,修改Docker 仓库
[root@localhost ~]#
sudo yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 在存储库中列出可用版本
[root@localhost ~]# yum list docker-ce --showduplicates | sort -r
docker-ce.x86_64 3:20.10.7-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.6-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.5-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.4-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.3-3.el7 docker-ce-stable
......
docker-ce.x86_64 17.06.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.3.ce-1.el7 docker-ce-stable
docker-ce.x86_64 17.03.2.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable
# 安装docker,中间提示,输入y即可。
# 安装最新版:sudo yum install docker-ce docker-ce-cli containerd.io
# 安装指定版:sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
# 例:sudo yum install docker-ce-20.10.6 docker-ce-cli-20.10.6 containerd.io
[root@localhost ~]# sudo yum install docker-ce docker-ce-cli containerd.io
......
已安装:
containerd.io.x86_64 0:1.4.9-3.1.el7 docker-ce.x86_64 3:20.10.7-3.el7 docker-ce-cli.x86_64 1:20.10.7-3.el7
作为依赖被安装:
audit-libs-python.x86_64 0:2.8.5-4.el7 checkpolicy.x86_64 0:2.5-8.el7 container-selinux.noarch 2:2.119.2-1.911c772.el7_8 docker-ce-rootless-extras.x86_64 0:20.10.7-3.el7
docker-scan-plugin.x86_64 0:0.8.0-3.el7 fuse-overlayfs.x86_64 0:0.7.2-6.el7_8 fuse3-libs.x86_64 0:3.6.1-4.el7 libcgroup.x86_64 0:0.41-21.el7
libsemanage-python.x86_64 0:2.5-14.el7 policycoreutils-python.x86_64 0:2.5-34.el7 python-IPy.noarch 0:0.75-6.el7 setools-libs.x86_64 0:3.3.8-4.el7
slirp4netns.x86_64 0:0.4.3-4.el7_8
完毕!
验证
[root@localhost ~]# docker --version
Docker version 20.10.7, build f0df350
启动Docker
[root@localhost ~]# sudo systemctl start docker
验证hello-world
[root@localhost ~]# sudo docker run hello-world
Hello from Docker!
......
For more examples and ideas, visit:
https://docs.docker.com/get-started/
设置开机启动
[root@localhost ~]# systemctl enable docker.service
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
卸载Docker
[root@localhost ~]# sudo yum remove docker-ce docker-ce-cli containerd.io
二、Docker使用(CentOS系统)
查看安装包
[root@localhost ~]# yum list installed | grep docker
删除安装包
[root@localhost ~]# yum -y remove xxxx
删除镜像、容器、配置文件等内容
[root@localhost ~]# sudo rm -rf /var/lib/docker
[root@localhost ~]# sudo rm -rf /var/lib/containerd
删除所有数据卷
[root@localhost ~]# docker volume rm $(sudo docker volume ls -q)
查看正在运行的Docker容器
[root@localhost test-network]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
查看全部的Docker容器
[root@localhost test-network]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8048ec9181c4 hello-world "/hello" 44 hours ago Exited (0) 44 hours ago sharp_leakey
2c4cb4d5c443 hello-world "/hello" 45 hours ago Exited (0) 45 hours ago compassionate_mirzakhani
1568df53b91f hello-world "/hello" 45 hours ago Exited (0) 45 hours ago bold_neumann
删除Docker容器
# 删除前查看
[root@localhost test-network]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8048ec9181c4 hello-world "/hello" 44 hours ago Exited (0) 44 hours ago sharp_leakey
2c4cb4d5c443 hello-world "/hello" 45 hours ago Exited (0) 45 hours ago compassionate_mirzakhani
1568df53b91f hello-world "/hello" 45 hours ago Exited (0) 45 hours ago bold_neumann
# 若要删除一个运行中的容器,需要加-f参数: docker rm -f <CONTAINER ID>
[root@localhost test-network]# docker rm -f 2c4cb4d5c443
2c4cb4d5c443
# 删除后查看
[root@localhost test-network]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8048ec9181c4 hello-world "/hello" 45 hours ago Exited (0) 45 hours ago sharp_leakey
1568df53b91f hello-world "/hello" 45 hours ago Exited (0) 45 hours ago bold_neumann