Docker产生背景

传统认为,软件编码/测试结束后,所产出的成果是程序或是能够编译执行的二进制字节码。而为了让这些程序可以顺利执行,开发团队需要准备完整的部署文件,以清楚地告诉运维团队其应该使用的配置文件和软件环境,从而让运维团队得以部署这些应用程序。不过,即便如此,仍然常常发生部署失败的状况。Docker通过镜像,将除操作系统核心以外,运行程序所需要的系统环境,自下而上打包,达到应用程序跨平台间的无缝接轨运行。
简单说就是,
过去,开发人员交给运维人员的东西是 代码;
现在,开发人员交给运维人员的东西是 操作系统+运行环境+应用配置+代码。
打一个不是很恰当的比喻:
过去,是搬家;
现在,是搬楼。

什么是Docker

Docker是基于Go语言实现的云开源项目。
Docker的主要目标是,“Build,Ship and Run Any App,Anywhere”,也就是通过对应用组件的封装、分发、部署、运行等生命周期的管理,使用户的App(可以是一个Web应用或数据库应用等)及其运行环境能够做到“一次封装,到处运行”。

Docker能干什么

传统虚拟技术,比如VMware,是虚拟出一套硬件后,在其上运行一个完整操作系统,在该系统上再运行所需应用进程。
而容器内的应用进程直接运行于宿主的内核,容器没有自己的内核,而且没有进行硬件模拟,因此,容器要比传统虚拟机更轻便。
每个容器之间互相隔离,每个容器有自己的文件系统,容器之间进程不会互相影响,能区分计算资源。

Docker三要素
  • 容器(Container)
    容器是利用镜像创建的实例。
    容器可以被启动、开始、停止和删除。
    每个容器是互相隔离的、保证安全的平台。
  • 镜像(Image)
    我们把应用程序和配置依赖打包好形成一个可交付的运行环境,这个打包好的运行环境就是一个镜像文件。
    镜像文件是一个只读模板。
    镜像可以用来创建容器,一个镜像可以创建多个容器。
    镜像就好比是一个类,容器则是属于该类的一个实例。
  • 仓库
    仓库是存储镜像文件的场所。
    仓库分为公开仓库和私有仓库。
    Docker Hub是最大的公开仓库,但由于受限于国外网速,所以实际用得不多。
    国内公开仓库是阿里云、网易云等。
CentOS8 Docker安装
查看内核
  • uname -r
    uname,用于打印当前系统相关信息,如内核版本号、硬件架构、主机名称和操作系统类型等。
    在这里插入图片描述
  • cat /etc/redhat-release
    在这里插入图片描述
使用Docker仓库进行安装

docker-ce,社区版docker;docker-ee,企业版docker。社区版免费,故安装docker-ce。

第一步:安装需要的软件包:yum-utils、device-mapper-persistent-data和lvm2。

yum-utils提供了yum-config-manager,device-mapper存储驱动程序需要device-mapper-persistent-data和lvm2。

yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2

详细如下:

[root@localhost ~]# yum install -y yum-utils \
>   device-mapper-persistent-data \
>   lvm2
CentOS Linux 8 - AppStream                      4.3 MB/s | 8.4 MB     00:01    
CentOS Linux 8 - BaseOS                         1.7 MB/s | 4.5 MB     00:02    
CentOS Linux 8 - Extras                         5.7 kB/s |  10 kB     00:01    
Package device-mapper-persistent-data-0.8.5-4.el8.x86_64 is already installed.
Package lvm2-8:2.03.09-5.el8.x86_64 is already installed.
Dependencies resolved.
================================================================================
 Package                      Arch       Version               Repository  Size
================================================================================
Installing:
 yum-utils                    noarch     4.0.18-4.el8          baseos      71 k
Upgrading:
 device-mapper                x86_64     8:1.02.175-5.el8      baseos     375 k
 device-mapper-event          x86_64     8:1.02.175-5.el8      baseos     269 k
 device-mapper-event-libs     x86_64     8:1.02.175-5.el8      baseos     269 k
 device-mapper-libs           x86_64     8:1.02.175-5.el8      baseos     408 k
 dnf-plugins-core             noarch     4.0.18-4.el8          baseos      69 k
 lvm2                         x86_64     8:2.03.11-5.el8       baseos     1.6 M
 lvm2-libs                    x86_64     8:2.03.11-5.el8       baseos     1.1 M
 python3-dnf-plugins-core     noarch     4.0.18-4.el8          baseos     234 k

Transaction Summary
================================================================================
Install  1 Package
Upgrade  8 Packages

Total download size: 4.4 M
Downloading Packages:
(1/9): device-mapper-1.02.175-5.el8.x86_64.rpm  232 kB/s | 375 kB     00:01    
(2/9): yum-utils-4.0.18-4.el8.noarch.rpm         39 kB/s |  71 kB     00:01    
(3/9): device-mapper-event-1.02.175-5.el8.x86_6 144 kB/s | 269 kB     00:01    
(4/9): device-mapper-event-libs-1.02.175-5.el8. 670 kB/s | 269 kB     00:00    
(5/9): device-mapper-libs-1.02.175-5.el8.x86_64 757 kB/s | 408 kB     00:00    
(6/9): dnf-plugins-core-4.0.18-4.el8.noarch.rpm 119 kB/s |  69 kB     00:00    
(7/9): lvm2-2.03.11-5.el8.x86_64.rpm            1.9 MB/s | 1.6 MB     00:00    
(8/9): lvm2-libs-2.03.11-5.el8.x86_64.rpm       1.6 MB/s | 1.1 MB     00:00    
(9/9): python3-dnf-plugins-core-4.0.18-4.el8.no 304 kB/s | 234 kB     00:00    
--------------------------------------------------------------------------------
Total                                           649 kB/s | 4.4 MB     00:06     
warning: /var/cache/dnf/baseos-929b586ef1f72f69/packages/yum-utils-4.0.18-4.el8.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS Linux 8 - BaseOS                         1.6 MB/s | 1.6 kB     00:00    
Importing GPG key 0x8483C65D:
 Userid     : "CentOS (CentOS Official Signing Key) <security@centos.org>"
 Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1 
  Running scriptlet: device-mapper-libs-8:1.02.175-5.el8.x86_64             1/1 
  Upgrading        : device-mapper-libs-8:1.02.175-5.el8.x86_64            1/17 
  Upgrading        : device-mapper-8:1.02.175-5.el8.x86_64                 2/17 
  Upgrading        : device-mapper-event-libs-8:1.02.175-5.el8.x86_64      3/17 
  Upgrading        : device-mapper-event-8:1.02.175-5.el8.x86_64           4/17 
  Running scriptlet: device-mapper-event-8:1.02.175-5.el8.x86_64           4/17 
  Upgrading        : lvm2-libs-8:2.03.11-5.el8.x86_64                      5/17 
  Upgrading        : python3-dnf-plugins-core-4.0.18-4.el8.noarch          6/17 
  Upgrading        : dnf-plugins-core-4.0.18-4.el8.noarch                  7/17 
  Installing       : yum-utils-4.0.18-4.el8.noarch                         8/17 
  Upgrading        : lvm2-8:2.03.11-5.el8.x86_64                           9/17 
  Running scriptlet: lvm2-8:2.03.11-5.el8.x86_64                           9/17 
  Cleanup          : dnf-plugins-core-4.0.17-5.el8.noarch                 10/17 
  Running scriptlet: lvm2-8:2.03.09-5.el8.x86_64                          11/17 
  Cleanup          : lvm2-8:2.03.09-5.el8.x86_64                          11/17 
  Running scriptlet: lvm2-8:2.03.09-5.el8.x86_64                          11/17 
  Cleanup          : python3-dnf-plugins-core-4.0.17-5.el8.noarch         12/17 
  Cleanup          : lvm2-libs-8:2.03.09-5.el8.x86_64                     13/17 
  Running scriptlet: device-mapper-event-8:1.02.171-5.el8.x86_64          14/17 
  Cleanup          : device-mapper-event-8:1.02.171-5.el8.x86_64          14/17 
  Cleanup          : device-mapper-event-libs-8:1.02.171-5.el8.x86_64     15/17 
  Cleanup          : device-mapper-libs-8:1.02.171-5.el8.x86_64           16/17 
  Cleanup          : device-mapper-8:1.02.171-5.el8.x86_64                17/17 
  Running scriptlet: device-mapper-8:1.02.171-5.el8.x86_64                17/17 
  Verifying        : yum-utils-4.0.18-4.el8.noarch                         1/17 
  Verifying        : device-mapper-8:1.02.175-5.el8.x86_64                 2/17 
  Verifying        : device-mapper-8:1.02.171-5.el8.x86_64                 3/17 
  Verifying        : device-mapper-event-8:1.02.175-5.el8.x86_64           4/17 
  Verifying        : device-mapper-event-8:1.02.171-5.el8.x86_64           5/17 
  Verifying        : device-mapper-event-libs-8:1.02.175-5.el8.x86_64      6/17 
  Verifying        : device-mapper-event-libs-8:1.02.171-5.el8.x86_64      7/17 
  Verifying        : device-mapper-libs-8:1.02.175-5.el8.x86_64            8/17 
  Verifying        : device-mapper-libs-8:1.02.171-5.el8.x86_64            9/17 
  Verifying        : dnf-plugins-core-4.0.18-4.el8.noarch                 10/17 
  Verifying        : dnf-plugins-core-4.0.17-5.el8.noarch                 11/17 
  Verifying        : lvm2-8:2.03.11-5.el8.x86_64                          12/17 
  Verifying        : lvm2-8:2.03.09-5.el8.x86_64                          13/17 
  Verifying        : lvm2-libs-8:2.03.11-5.el8.x86_64                     14/17 
  Verifying        : lvm2-libs-8:2.03.09-5.el8.x86_64                     15/17 
  Verifying        : python3-dnf-plugins-core-4.0.18-4.el8.noarch         16/17 
  Verifying        : python3-dnf-plugins-core-4.0.17-5.el8.noarch         17/17 
Installed products updated.

Upgraded:
  device-mapper-8:1.02.175-5.el8.x86_64                                         
  device-mapper-event-8:1.02.175-5.el8.x86_64                                   
  device-mapper-event-libs-8:1.02.175-5.el8.x86_64                              
  device-mapper-libs-8:1.02.175-5.el8.x86_64                                    
  dnf-plugins-core-4.0.18-4.el8.noarch                                          
  lvm2-8:2.03.11-5.el8.x86_64                                                   
  lvm2-libs-8:2.03.11-5.el8.x86_64                                              
  python3-dnf-plugins-core-4.0.18-4.el8.noarch                                  

Installed:
  yum-utils-4.0.18-4.el8.noarch                                                 

Complete!
第二步:设置仓库

使用国内镜像,比如阿里云源镜像

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

详细如下:

[root@localhost ~]# yum-config-manager \
>     --add-repo \
>     http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Adding repo from: http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
第三步:安装docker engine,包括docker-ce、docker-ce-cli和containerd.io
yum install docker-ce docker-ce-cli containerd.io

详细如下:

[root@localhost ~]# yum install docker-ce docker-ce-cli containerd.io
Docker CE Stable - x86_64                        22 kB/s |  15 kB     00:00    
Dependencies resolved.
================================================================================
 Package                   Arch   Version                Repository        Size
================================================================================
Installing:
 containerd.io             x86_64 1.4.9-3.1.el8          docker-ce-stable  30 M
 docker-ce                 x86_64 3:20.10.8-3.el8        docker-ce-stable  22 M
 docker-ce-cli             x86_64 1:20.10.8-3.el8        docker-ce-stable  29 M
Installing dependencies:
 container-selinux         noarch 2:2.162.0-1.module_el8.4.0+830+8027e1c4
                                                         appstream         52 k
 docker-ce-rootless-extras x86_64 20.10.8-3.el8          docker-ce-stable 4.6 M
 docker-scan-plugin        x86_64 0.8.0-3.el8            docker-ce-stable 4.2 M
 fuse-overlayfs            x86_64 1.4.0-3.module_el8.4.0+830+8027e1c4
                                                         appstream         72 k
 fuse3                     x86_64 3.2.1-12.el8           baseos            50 k
 fuse3-libs                x86_64 3.2.1-12.el8           baseos            94 k
 libslirp                  x86_64 4.3.1-1.module_el8.4.0+575+63b40ad7
                                                         appstream         69 k
 slirp4netns               x86_64 1.1.8-1.module_el8.4.0+641+6116a774
                                                         appstream         51 k
Enabling module streams:
 container-tools                  rhel8                                        

Transaction Summary
================================================================================
Install  11 Packages

Total download size: 90 M
Installed size: 377 M
Is this ok [y/N]: y
Downloading Packages:
(1/11): fuse-overlayfs-1.4.0-3.module_el8.4.0+8 4.1 kB/s |  72 kB     00:17    
(2/11): libslirp-4.3.1-1.module_el8.4.0+575+63b 3.9 kB/s |  69 kB     00:17    
(3/11): slirp4netns-1.1.8-1.module_el8.4.0+641+ 766 kB/s |  51 kB     00:00    
(4/11): container-selinux-2.162.0-1.module_el8. 2.9 kB/s |  52 kB     00:17    
(5/11): fuse3-3.2.1-12.el8.x86_64.rpm           165 kB/s |  50 kB     00:00    
(6/11): fuse3-libs-3.2.1-12.el8.x86_64.rpm      175 kB/s |  94 kB     00:00    
(7/11): docker-ce-20.10.8-3.el8.x86_64.rpm      5.3 MB/s |  22 MB     00:04    
(8/11): docker-ce-rootless-extras-20.10.8-3.el8 5.4 MB/s | 4.6 MB     00:00    
(9/11): containerd.io-1.4.9-3.1.el8.x86_64.rpm  5.4 MB/s |  30 MB     00:05    
(10/11): docker-scan-plugin-0.8.0-3.el8.x86_64. 6.4 MB/s | 4.2 MB     00:00    
(11/11): docker-ce-cli-20.10.8-3.el8.x86_64.rpm 3.7 MB/s |  29 MB     00:07    
--------------------------------------------------------------------------------
Total                                           3.3 MB/s |  90 MB     00:27     
warning: /var/cache/dnf/docker-ce-stable-ab4061364e2cf0db/packages/containerd.io-1.4.9-3.1.el8.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY
Docker CE Stable - x86_64                       4.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://mirrors.aliyun.com/docker-ce/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-scan-plugin-0.8.0-3.el8.x86_64                 1/11 
  Running scriptlet: docker-scan-plugin-0.8.0-3.el8.x86_64                 1/11 
  Installing       : docker-ce-cli-1:20.10.8-3.el8.x86_64                  2/11 
  Running scriptlet: docker-ce-cli-1:20.10.8-3.el8.x86_64                  2/11 
  Running scriptlet: container-selinux-2:2.162.0-1.module_el8.4.0+830+8    3/11 
  Installing       : container-selinux-2:2.162.0-1.module_el8.4.0+830+8    3/11 
  Running scriptlet: container-selinux-2:2.162.0-1.module_el8.4.0+830+8    3/11 
  Installing       : containerd.io-1.4.9-3.1.el8.x86_64                    4/11 
  Running scriptlet: containerd.io-1.4.9-3.1.el8.x86_64                    4/11 
  Installing       : fuse3-libs-3.2.1-12.el8.x86_64                        5/11 
  Running scriptlet: fuse3-libs-3.2.1-12.el8.x86_64                        5/11 
  Installing       : fuse3-3.2.1-12.el8.x86_64                             6/11 
  Installing       : fuse-overlayfs-1.4.0-3.module_el8.4.0+830+8027e1c4    7/11 
  Running scriptlet: fuse-overlayfs-1.4.0-3.module_el8.4.0+830+8027e1c4    7/11 
  Installing       : libslirp-4.3.1-1.module_el8.4.0+575+63b40ad7.x86_6    8/11 
  Installing       : slirp4netns-1.1.8-1.module_el8.4.0+641+6116a774.x8    9/11 
  Installing       : docker-ce-rootless-extras-20.10.8-3.el8.x86_64       10/11 
  Running scriptlet: docker-ce-rootless-extras-20.10.8-3.el8.x86_64       10/11 
  Installing       : docker-ce-3:20.10.8-3.el8.x86_64                     11/11 
  Running scriptlet: docker-ce-3:20.10.8-3.el8.x86_64                     11/11 
  Running scriptlet: container-selinux-2:2.162.0-1.module_el8.4.0+830+8   11/11 
  Running scriptlet: docker-ce-3:20.10.8-3.el8.x86_64                     11/11 
  Verifying        : container-selinux-2:2.162.0-1.module_el8.4.0+830+8    1/11 
  Verifying        : fuse-overlayfs-1.4.0-3.module_el8.4.0+830+8027e1c4    2/11 
  Verifying        : libslirp-4.3.1-1.module_el8.4.0+575+63b40ad7.x86_6    3/11 
  Verifying        : slirp4netns-1.1.8-1.module_el8.4.0+641+6116a774.x8    4/11 
  Verifying        : fuse3-3.2.1-12.el8.x86_64                             5/11 
  Verifying        : fuse3-libs-3.2.1-12.el8.x86_64                        6/11 
  Verifying        : containerd.io-1.4.9-3.1.el8.x86_64                    7/11 
  Verifying        : docker-ce-3:20.10.8-3.el8.x86_64                      8/11 
  Verifying        : docker-ce-cli-1:20.10.8-3.el8.x86_64                  9/11 
  Verifying        : docker-ce-rootless-extras-20.10.8-3.el8.x86_64       10/11 
  Verifying        : docker-scan-plugin-0.8.0-3.el8.x86_64                11/11 
Installed products updated.

Installed:
  container-selinux-2:2.162.0-1.module_el8.4.0+830+8027e1c4.noarch              
  containerd.io-1.4.9-3.1.el8.x86_64                                            
  docker-ce-3:20.10.8-3.el8.x86_64                                              
  docker-ce-cli-1:20.10.8-3.el8.x86_64                                          
  docker-ce-rootless-extras-20.10.8-3.el8.x86_64                                
  docker-scan-plugin-0.8.0-3.el8.x86_64                                         
  fuse-overlayfs-1.4.0-3.module_el8.4.0+830+8027e1c4.x86_64                     
  fuse3-3.2.1-12.el8.x86_64                                                     
  fuse3-libs-3.2.1-12.el8.x86_64                                                
  libslirp-4.3.1-1.module_el8.4.0+575+63b40ad7.x86_64                           
  slirp4netns-1.1.8-1.module_el8.4.0+641+6116a774.x86_64                        

Complete!
第四步:启动docker
systemctl start docker
第五步:验证安装是否ok
docker run hello-world

详细如下:

[root@localhost lib]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete 
Digest: sha256:df5f5184104426b65967e016ff2ac0bfcd44ad7899ca3bbcf8e44e4461491a9e
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/
参考文章

docker官网:install on CentOS
菜鸟教程:CentOS Docker安装

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐