Docker系列-2.Docker安装
Docker支持多种操作系统版本,包括CentOS、Ubuntu、Windows、Osx等,本文主要介绍CentOS7下docker的安装和配置。这里采用制作docker模板机的方式。
1. 确认系统版本
Docker要求CentOs7以上的系统版本和3.10以上的kernel版本。
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[root@localhost ~]# uname -a
Linux localhost.localdomain 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
2. 卸载旧版本
[root@localhost ~]# sudo yum remove docker
> docker-client
> docker-client-latest
> docker-common
> docker-latest
> docker-latest-logrotate
> docker-logrotate
> docker-engine
Loaded plugins: fastestmirror
No Match for argument: docker
No Match for argument: docker-client
No Match for argument: docker-client-latest
No Match for argument: docker-common
No Match for argument: docker-latest
No Match for argument: docker-latest-logrotate
No Match for argument: docker-logrotate
No Match for argument: docker-engine
No Packages marked for removal
[root@localhost ~]#
3. 安装docker
3.1 安装必要包支持
[root@localhost ~]# yum install -y yum-utils device-mapper-persistent-date lvm2
Loaded plugins: fastestmirror
Determining fastest mirrors
* base: mirrors.bfsu.edu.cn
* extras: mirrors.bfsu.edu.cn
* updates: mirrors.163.com
base | 3.6 kB 00:00
extras | 2.9 kB 00:00
updates | 2.9 kB 00:00
(1/2): extras/7/x86_64/primary_db | 205 kB 00:00
(2/2): updates/7/x86_64/primary_db | 3.0 MB 00:01
No package device-mapper-persistent-date available.
Resolving Dependencies
--> Running transaction check
---> Package lvm2.x86_64 7:2.02.180-8.el7 will be updated
---> Package lvm2.x86_64 7:2.02.186-7.el7_8.2 will be an update
--> Processing Dependency: lvm2-libs = 7:2.02.186-7.el7_8.2 for package: 7:lvm2-2.02.186-7.el7_8.2.x86_64
---> Package yum-utils.noarch 0:1.1.31-54.el7_8 will be installed
--> Processing Dependency: python-kitchen for package: yum-utils-1.1.31-54.el7_8.noarch
--> Processing Dependency: libxml2-python for package: yum-utils-1.1.31-54.el7_8.noarch
--> Running transaction check
---> Package libxml2-python.x86_64 0:2.9.1-6.el7.4 will be installed
--> Processing Dependency: libxml2 = 2.9.1-6.el7.4 for package: libxml2-python-2.9.1-6.el7.4.x86_64
---> Package lvm2-libs.x86_64 7:2.02.180-8.el7 will be updated
---> Package lvm2-libs.x86_64 7:2.02.186-7.el7_8.2 will be an update
--> Processing Dependency: device-mapper-event = 7:1.02.164-7.el7_8.2 for package: 7:lvm2-libs-2.02.186-7.el7_8.2.x86_64
---> Package python-kitchen.noarch 0:1.1.1-5.el7 will be installed
--> Processing Dependency: python-chardet for package: python-kitchen-1.1.1-5.el7.noarch
--> Running transaction check
---> Package device-mapper-event.x86_64 7:1.02.149-8.el7 will be updated
---> Package device-mapper-event.x86_64 7:1.02.164-7.el7_8.2 will be an update
--> Processing Dependency: device-mapper-event-libs = 7:1.02.164-7.el7_8.2 for package: 7:device-mapper-event-1.02.164-7.el7_8.2.x86_64
--> Processing Dependency: device-mapper = 7:1.02.164-7.el7_8.2 for package: 7:device-mapper-event-1.02.164-7.el7_8.2.x86_64
---> Package libxml2.x86_64 0:2.9.1-6.el7_2.3 will be updated
---> Package libxml2.x86_64 0:2.9.1-6.el7.4 will be an update
---> Package python-chardet.noarch 0:2.2.1-3.el7 will be installed
--> Running transaction check
---> Package device-mapper.x86_64 7:1.02.149-8.el7 will be updated
--> Processing Dependency: device-mapper = 7:1.02.149-8.el7 for package: 7:device-mapper-libs-1.02.149-8.el7.x86_64
---> Package device-mapper.x86_64 7:1.02.164-7.el7_8.2 will be an update
---> Package device-mapper-event-libs.x86_64 7:1.02.149-8.el7 will be updated
---> Package device-mapper-event-libs.x86_64 7:1.02.164-7.el7_8.2 will be an update
--> Running transaction check
---> Package device-mapper-libs.x86_64 7:1.02.149-8.el7 will be updated
---> Package device-mapper-libs.x86_64 7:1.02.164-7.el7_8.2 will be an update
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
yum-utils noarch 1.1.31-54.el7_8 updates 122 k
Updating:
lvm2 x86_64 7:2.02.186-7.el7_8.2 updates 1.3 M
Installing for dependencies:
libxml2-python x86_64 2.9.1-6.el7.4 base 247 k
python-chardet noarch 2.2.1-3.el7 base 227 k
python-kitchen noarch 1.1.1-5.el7 base 267 k
Updating for dependencies:
device-mapper x86_64 7:1.02.164-7.el7_8.2 updates 295 k
device-mapper-event x86_64 7:1.02.164-7.el7_8.2 updates 191 k
device-mapper-event-libs x86_64 7:1.02.164-7.el7_8.2 updates 190 k
device-mapper-libs x86_64 7:1.02.164-7.el7_8.2 updates 324 k
libxml2 x86_64 2.9.1-6.el7.4 base 668 k
lvm2-libs x86_64 7:2.02.186-7.el7_8.2 updates 1.1 M
Transaction Summary
================================================================================
Install 1 Package (+3 Dependent packages)
Upgrade 1 Package (+6 Dependent packages)
Total download size: 4.9 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
(1/11): device-mapper-1.02.164-7.el7_8.2.x86_64.rpm | 295 kB 00:00
(2/11): device-mapper-event-1.02.164-7.el7_8.2.x86_64.rpm | 191 kB 00:00
(3/11): device-mapper-event-libs-1.02.164-7.el7_8.2.x86_64 | 190 kB 00:00
(4/11): device-mapper-libs-1.02.164-7.el7_8.2.x86_64.rpm | 324 kB 00:00
(5/11): lvm2-2.02.186-7.el7_8.2.x86_64.rpm | 1.3 MB 00:00
(6/11): lvm2-libs-2.02.186-7.el7_8.2.x86_64.rpm | 1.1 MB 00:00
(7/11): libxml2-python-2.9.1-6.el7.4.x86_64.rpm | 247 kB 00:01
(8/11): libxml2-2.9.1-6.el7.4.x86_64.rpm | 668 kB 00:01
(9/11): python-chardet-2.2.1-3.el7.noarch.rpm | 227 kB 00:00
(10/11): python-kitchen-1.1.1-5.el7.noarch.rpm | 267 kB 00:00
(11/11): yum-utils-1.1.31-54.el7_8.noarch.rpm | 122 kB 00:00
--------------------------------------------------------------------------------
Total 2.1 MB/s | 4.9 MB 00:02
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Updating : 7:device-mapper-libs-1.02.164-7.el7_8.2.x86_64 1/18
Updating : 7:device-mapper-1.02.164-7.el7_8.2.x86_64 2/18
Updating : 7:device-mapper-event-libs-1.02.164-7.el7_8.2.x86_64 3/18
Updating : 7:device-mapper-event-1.02.164-7.el7_8.2.x86_64 4/18
Updating : 7:lvm2-libs-2.02.186-7.el7_8.2.x86_64 5/18
Installing : python-chardet-2.2.1-3.el7.noarch 6/18
Installing : python-kitchen-1.1.1-5.el7.noarch 7/18
Updating : libxml2-2.9.1-6.el7.4.x86_64 8/18
Installing : libxml2-python-2.9.1-6.el7.4.x86_64 9/18
Installing : yum-utils-1.1.31-54.el7_8.noarch 10/18
Updating : 7:lvm2-2.02.186-7.el7_8.2.x86_64 11/18
Cleanup : 7:lvm2-2.02.180-8.el7.x86_64 12/18
Cleanup : 7:lvm2-libs-2.02.180-8.el7.x86_64 13/18
Cleanup : 7:device-mapper-event-1.02.149-8.el7.x86_64 14/18
Cleanup : 7:device-mapper-event-libs-1.02.149-8.el7.x86_64 15/18
Cleanup : 7:device-mapper-1.02.149-8.el7.x86_64 16/18
Cleanup : 7:device-mapper-libs-1.02.149-8.el7.x86_64 17/18
Cleanup : libxml2-2.9.1-6.el7_2.3.x86_64 18/18
Verifying : 7:device-mapper-event-1.02.164-7.el7_8.2.x86_64 1/18
Verifying : 7:lvm2-libs-2.02.186-7.el7_8.2.x86_64 2/18
Verifying : 7:device-mapper-1.02.164-7.el7_8.2.x86_64 3/18
Verifying : python-kitchen-1.1.1-5.el7.noarch 4/18
Verifying : 7:lvm2-2.02.186-7.el7_8.2.x86_64 5/18
Verifying : 7:device-mapper-libs-1.02.164-7.el7_8.2.x86_64 6/18
Verifying : libxml2-python-2.9.1-6.el7.4.x86_64 7/18
Verifying : 7:device-mapper-event-libs-1.02.164-7.el7_8.2.x86_64 8/18
Verifying : libxml2-2.9.1-6.el7.4.x86_64 9/18
Verifying : yum-utils-1.1.31-54.el7_8.noarch 10/18
Verifying : python-chardet-2.2.1-3.el7.noarch 11/18
Verifying : 7:lvm2-2.02.180-8.el7.x86_64 12/18
Verifying : 7:device-mapper-event-1.02.149-8.el7.x86_64 13/18
Verifying : libxml2-2.9.1-6.el7_2.3.x86_64 14/18
Verifying : 7:lvm2-libs-2.02.180-8.el7.x86_64 15/18
Verifying : 7:device-mapper-1.02.149-8.el7.x86_64 16/18
Verifying : 7:device-mapper-libs-1.02.149-8.el7.x86_64 17/18
Verifying : 7:device-mapper-event-libs-1.02.149-8.el7.x86_64 18/18
Installed:
yum-utils.noarch 0:1.1.31-54.el7_8
Dependency Installed:
libxml2-python.x86_64 0:2.9.1-6.el7.4 python-chardet.noarch 0:2.2.1-3.el7
python-kitchen.noarch 0:1.1.1-5.el7
Updated:
lvm2.x86_64 7:2.02.186-7.el7_8.2
Dependency Updated:
device-mapper.x86_64 7:1.02.164-7.el7_8.2
device-mapper-event.x86_64 7:1.02.164-7.el7_8.2
device-mapper-event-libs.x86_64 7:1.02.164-7.el7_8.2
device-mapper-libs.x86_64 7:1.02.164-7.el7_8.2
libxml2.x86_64 0:2.9.1-6.el7.4
lvm2-libs.x86_64 7:2.02.186-7.el7_8.2
Complete!
[root@localhost ~]#
3.2 选用国内镜像repo
a 添加新的的repo(阿里云为例)
[root@localhost ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror
adding repo from: http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
b 刷新yum缓存
[root@localhost /]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: base docker-ce-stable extras updates
Cleaning up list of fastest mirrors
[root@localhost /]# yum makecache fast
Loaded plugins: fastestmirror
Determining fastest mirrors
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
base | 3.6 kB 00:00:00
docker-ce-stable | 3.5 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/6): base/7/x86_64/group_gz | 153 kB 00:00:00
(2/6): extras/7/x86_64/primary_db | 205 kB 00:00:00
(3/6): updates/7/x86_64/primary_db | 3.0 MB 00:00:00
(4/6): docker-ce-stable/x86_64/updateinfo | 55 B 00:00:00
(5/6): docker-ce-stable/x86_64/primary_db | 45 kB 00:00:00
(6/6): base/7/x86_64/primary_db | 6.1 MB 00:00:02
Metadata Cache Created
[root@localhost /]#
3.3 查看仓库版本
yum list docker-ce --showduplicates | sort -r
* updates: mirrors.aliyun.com
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror
* extras: mirrors.aliyun.com
docker-ce.x86_64 3:19.03.9-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.8-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.7-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.6-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.5-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.4-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.3-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.2-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.1-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.12-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.11-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.10-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.0-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.9-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.8-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.7-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.6-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.5-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.4-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.3-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.2-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.1-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.0-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.3.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.2.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.0.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.03.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 18.03.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.12.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.12.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.09.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.09.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.2.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.1.ce-1.el7.centos 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
* base: mirrors.aliyun.com
Available Packages
[root@localhost /]#
3.4 安装指定版本docker
Last login: Mon Jul 27 12:34:46 2020 from 192.168.56.1
[root@localhost ~]# yum -y remove docker-ce.x86_64
Loaded plugins: fastestmirror
No Match for argument: docker-ce.x86_64
No Packages marked for removal
[root@localhost ~]# clear
[root@localhost ~]# yum install -y docker-ce-18.03.1.ce
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package docker-ce.x86_64 0:18.03.1.ce-1.el7.centos will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=============================================================================================
Package Arch Version Repository Size
=============================================================================================
Installing:
docker-ce x86_64 18.03.1.ce-1.el7.centos docker-ce-stable 35 M
Transaction Summary
=============================================================================================
Install 1 Package
Total download size: 35 M
Installed size: 35 M
Downloading packages:
docker-ce-18.03.1.ce-1.el7.centos.x86_64.rpm | 35 MB 00:00:02
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : docker-ce-18.03.1.ce-1.el7.centos.x86_64 1/1
Verifying : docker-ce-18.03.1.ce-1.el7.centos.x86_64 1/1
Installed:
docker-ce.x86_64 0:18.03.1.ce-1.el7.centos
Complete!
[root@localhost ~]#
4. 启动验证
[root@localhost ~]# systemctl start docker
[root@localhost ~]# docker version
Client:
Version: 18.03.1-ce
API version: 1.37
Go version: go1.9.5
Git commit: 9ee9f40
Built: Thu Apr 26 07:20:16 2018
OS/Arch: linux/amd64
Experimental: false
Orchestrator: swarm
Server:
Engine:
Version: 18.03.1-ce
API version: 1.37 (minimum version 1.12)
Go version: go1.9.5
Git commit: 9ee9f40
Built: Thu Apr 26 07:23:58 2018
OS/Arch: linux/amd64
Experimental: false
5. 抽取测试镜像
[root@localhost ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:49a1c8800c94df04e9658809b006fd8a686cab8028d33cfba2cc049724254202
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
[root@localhost ~]#