本文主要介绍在CentOS系统下安装Docker CE(社区版)的流程和简单配置
1. 系统要求
安装Docker,操作系统版本需要CentOS7或以上最新操作系统。
可使用以下命令查看操作系统版本:
cat /etc/redhat-release CentOS Linux release 8.2.2004 (Core)
推荐使用 overlay2
存储驱动。
2. 删除旧版本
sudo yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
需要删除旧版本docker
或者docker-engine
,以及相关的依赖,使用如上命令。
3. 设置Docker仓库
1.安装所需软件包,yum-utils提供了yum-config-manager工具,还包括devicemapper存储驱动所需要的device-mapper-persistent-data和lvm2。
#安装依赖包 sudo yum install -y yum-utils device-mapper-persistent-data lvm2
结果如下:
[root@webserver ~]# sudo yum install -y yum-utils > device-mapper-persistent-data > lvm2 Last metadata expiration check: 0:14:17 ago on Sun 05 Jul 2020 04:55:04 PM CST. Package device-mapper-persistent-data-0.8.5-3.el8.x86_64 is already installed. Package lvm2-8:2.03.08-3.el8.x86_64 is already installed. Dependencies resolved. ======================================================================================================================================== Package Architecture Version Repository Size ======================================================================================================================================== Installing: yum-utils noarch 4.0.12-3.el8 BaseOS 66 k Transaction Summary ======================================================================================================================================== Install 1 Package Total download size: 66 k Installed size: 20 k Downloading Packages: yum-utils-4.0.12-3.el8.noarch.rpm 18 kB/s | 66 kB 00:03 ---------------------------------------------------------------------------------------------------------------------------------------- Total 14 kB/s | 66 kB 00:04 Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Installing : yum-utils-4.0.12-3.el8.noarch 1/1 Running scriptlet: yum-utils-4.0.12-3.el8.noarch 1/1 Verifying : yum-utils-4.0.12-3.el8.noarch 1/1 Installed: yum-utils-4.0.12-3.el8.noarch Complete!
2.设置最新稳定的仓库。
#设置仓库 sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
4. 安装Docker
安装最新版本Docker和容器,或者特定版本的Docker。
#安装Docker sudo yum install docker-ce docker-ce-cli containerd.io
完成以上步骤,表示您已成功安装Docker CE。
requires containerd.io >= 1.2.2-3,错误建议安装低版本软件。
dnf install docker-ce-3:18.09.1 docker-ce-cli-1:18.09.1 containerd.io
错误信息:
[root@webserver ~]# sudo yum install docker-ce docker-ce-cli containerd.io Docker CE Stable - x86_64 18 kB/s | 25 kB 00:01 Error: Problem: package docker-ce-3:19.03.12-3.el7.x86_64 requires containerd.io >= 1.2.2-3, but none of the providers can be installed - cannot install the best candidate for the job - package containerd.io-1.2.10-3.2.el7.x86_64 is filtered out by modular filtering - package containerd.io-1.2.13-3.1.el7.x86_64 is filtered out by modular filtering - package containerd.io-1.2.13-3.2.el7.x86_64 is filtered out by modular filtering - package containerd.io-1.2.2-3.3.el7.x86_64 is filtered out by modular filtering - package containerd.io-1.2.2-3.el7.x86_64 is filtered out by modular filtering - package containerd.io-1.2.4-3.1.el7.x86_64 is filtered out by modular filtering - package containerd.io-1.2.5-3.1.el7.x86_64 is filtered out by modular filtering - package containerd.io-1.2.6-3.3.el7.x86_64 is filtered out by modular filtering (try to add '--skip-broken' to skip uninstallable packages or '--nobest' to use not only best candidate packages)
结果如下:
[root@webserver ~]# dnf install docker-ce-3:18.09.1 docker-ce-cli-1:18.09.1 containerd.io Last metadata expiration check: 0:03:54 ago on Sun 05 Jul 2020 05:17:13 PM CST. Dependencies resolved. ======================================================================================================================================== Package Architecture Version Repository Size ======================================================================================================================================== Installing: containerd.io x86_64 1.2.0-3.el7 docker-ce-stable 22 M docker-ce x86_64 3:18.09.1-3.el7 docker-ce-stable 19 M docker-ce-cli x86_64 1:18.09.1-3.el7 docker-ce-stable 14 M Installing dependencies: checkpolicy x86_64 2.9-1.el8 BaseOS 348 k container-selinux noarch 2:2.124.0-1.module_el8.2.0+305+5e198a41 AppStream 47 k libcgroup x86_64 0.41-19.el8 BaseOS 70 k policycoreutils-python-utils noarch 2.9-9.el8 BaseOS 251 k python3-audit x86_64 3.0-0.17.20191104git1c2f876.el8 BaseOS 86 k python3-libsemanage x86_64 2.9-2.el8 BaseOS 127 k python3-policycoreutils noarch 2.9-9.el8 BaseOS 2.2 M python3-setools x86_64 4.2.2-2.el8 BaseOS 601 k Enabling module streams: container-tools rhel8 Transaction Summary ======================================================================================================================================== Install 11 Packages Total download size: 58 M Installed size: 246 M Is this ok [y/N]: y Downloading Packages: (1/11): libcgroup-0.41-19.el8.x86_64.rpm 590 kB/s | 70 kB 00:00 (2/11): checkpolicy-2.9-1.el8.x86_64.rpm 2.4 MB/s | 348 kB 00:00 (3/11): python3-audit-3.0-0.17.20191104git1c2f876.el8.x86_64.rpm 4.5 MB/s | 86 kB 00:00 (4/11): policycoreutils-python-utils-2.9-9.el8.noarch.rpm 5.3 MB/s | 251 kB 00:00 (5/11): python3-libsemanage-2.9-2.el8.x86_64.rpm 4.6 MB/s | 127 kB 00:00 (6/11): python3-setools-4.2.2-2.el8.x86_64.rpm 6.1 MB/s | 601 kB 00:00 (7/11): container-selinux-2.124.0-1.module_el8.2.0+305+5e198a41.noarch.rpm 159 kB/s | 47 kB 00:00 (8/11): python3-policycoreutils-2.9-9.el8.noarch.rpm 13 MB/s | 2.2 MB 00:00 (9/11): docker-ce-cli-18.09.1-3.el7.x86_64.rpm 6.1 MB/s | 14 MB 00:02 (10/11): docker-ce-18.09.1-3.el7.x86_64.rpm 5.6 MB/s | 19 MB 00:03 (11/11): containerd.io-1.2.0-3.el7.x86_64.rpm 6.5 MB/s | 22 MB 00:03 ---------------------------------------------------------------------------------------------------------------------------------------- Total 12 MB/s | 58 MB 00:04 warning: /var/cache/dnf/docker-ce-stable-091d8a9c23201250/packages/containerd.io-1.2.0-3.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY Docker CE Stable - x86_64 5.8 kB/s | 1.6 kB 00:00 Importing GPG key 0x621E9F35: Userid : "Docker Release (CE rpm) <docker@docker.com>" Fingerprint: 060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35 From : https://download.docker.com/linux/centos/gpg Is this ok [y/N]: y Key imported successfully Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Installing : docker-ce-cli-1:18.09.1-3.el7.x86_64 1/11 Running scriptlet: docker-ce-cli-1:18.09.1-3.el7.x86_64 1/11 Installing : containerd.io-1.2.0-3.el7.x86_64 2/11 Running scriptlet: containerd.io-1.2.0-3.el7.x86_64 2/11 Installing : python3-setools-4.2.2-2.el8.x86_64 3/11 Installing : python3-libsemanage-2.9-2.el8.x86_64 4/11 Installing : python3-audit-3.0-0.17.20191104git1c2f876.el8.x86_64 5/11 Running scriptlet: libcgroup-0.41-19.el8.x86_64 6/11 Installing : libcgroup-0.41-19.el8.x86_64 6/11 Running scriptlet: libcgroup-0.41-19.el8.x86_64 6/11 Installing : checkpolicy-2.9-1.el8.x86_64 7/11 Installing : python3-policycoreutils-2.9-9.el8.noarch 8/11 Installing : policycoreutils-python-utils-2.9-9.el8.noarch 9/11 Running scriptlet: container-selinux-2:2.124.0-1.module_el8.2.0+305+5e198a41.noarch 10/11 Installing : container-selinux-2:2.124.0-1.module_el8.2.0+305+5e198a41.noarch 10/11 Running scriptlet: container-selinux-2:2.124.0-1.module_el8.2.0+305+5e198a41.noarch 10/11 Running scriptlet: docker-ce-3:18.09.1-3.el7.x86_64 11/11 Installing : docker-ce-3:18.09.1-3.el7.x86_64 11/11 Running scriptlet: docker-ce-3:18.09.1-3.el7.x86_64 11/11 Running scriptlet: container-selinux-2:2.124.0-1.module_el8.2.0+305+5e198a41.noarch 11/11 Running scriptlet: docker-ce-3:18.09.1-3.el7.x86_64 11/11 Verifying : container-selinux-2:2.124.0-1.module_el8.2.0+305+5e198a41.noarch 1/11 Verifying : checkpolicy-2.9-1.el8.x86_64 2/11 Verifying : libcgroup-0.41-19.el8.x86_64 3/11 Verifying : policycoreutils-python-utils-2.9-9.el8.noarch 4/11 Verifying : python3-audit-3.0-0.17.20191104git1c2f876.el8.x86_64 5/11 Verifying : python3-libsemanage-2.9-2.el8.x86_64 6/11 Verifying : python3-policycoreutils-2.9-9.el8.noarch 7/11 Verifying : python3-setools-4.2.2-2.el8.x86_64 8/11 Verifying : containerd.io-1.2.0-3.el7.x86_64 9/11 Verifying : docker-ce-3:18.09.1-3.el7.x86_64 10/11 Verifying : docker-ce-cli-1:18.09.1-3.el7.x86_64 11/11 Installed: checkpolicy-2.9-1.el8.x86_64 container-selinux-2:2.124.0-1.module_el8.2.0+305+5e198a41.noarch containerd.io-1.2.0-3.el7.x86_64 docker-ce-3:18.09.1-3.el7.x86_64 docker-ce-cli-1:18.09.1-3.el7.x86_64 libcgroup-0.41-19.el8.x86_64 policycoreutils-python-utils-2.9-9.el8.noarch python3-audit-3.0-0.17.20191104git1c2f876.el8.x86_64 python3-libsemanage-2.9-2.el8.x86_64 python3-policycoreutils-2.9-9.el8.noarch python3-setools-4.2.2-2.el8.x86_64 Complete!
运行docker命令,查看docker是否安装成功
#查看版本信息 docker version Client: Docker Engine - Community Version: 19.03.1 API version: 1.40 Go version: go1.12.5 Git commit: 74b1e89 Built: Thu Jul 25 21:21:07 2019 OS/Arch: linux/amd64 Experimental: false #启动Docker systemctl start docker #设置Docker开机启动 systemctl enable docker
5. Docker常见命令
#检测容器是否正常安装 docker info #显示服务器上镜像信息 docker images #显示正在运行的容器 docker ps #查看包括已经停止的所有容器 docker ps -a #显示最新启动的容器 docker ps -l #搜索所有镜像 docker search nginx #启动一个容器 hello-word docker run hello-world
6. 停止所有镜像
#停止所有容器 docker stop `docker ps -a -q`
7. 删除所有容器
#删除所有容器 docker rm `docker ps -a -q`
8. 删除所有镜像
#删除所有镜像
docker rmi `docker images -q`
9. 按条件删除镜像
删除None的镜像
docker rmi `docker images | awk '/^<none>/ { print $3 }'`
参考文章:https://docsxyz.com/wiki/docker/install