浅谈Docker

126 阅读6分钟

浅谈Docker

概念

Docker是基于Go语言实现的云开源项目。

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

解决了运行环境和配置问题软件容器,方便做持续集成并有助于整体发布的容器虚拟化技术。

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

安装

Docker官网地址:www.docker.com

Docker Hub官网地址:hub.docker.com/

安装说明(!注意)

  1. Docker并非是一个通用的容器工具,它依赖于已存在并运行的Linux内核环境。
  2. Docker实质上是在已运行的Linux下制造了一个隔离的文件环境,因此它执行的效率几乎等同于所部署的Linux主机。
  3. 因此,Docker必须部署在Linux内核的系统上。 如果其他系统想要部署Docker就必须安装一个虚拟Linux环境。
  4. 目前,CentOS仅发行版本中的内核支持Docker。Docker运行在CentOS 7(64-bit)上,要求系统为64位、Linux系统内核版本为3.8以上,这里选用CentOS 7.x 。

查看自己的内核

[root@iZwz9b7fouo96fhfxbfa6gZ ~]# cat /etc/redhat-release 
CentOS Linux release 7.8.2003 (Core)
# uname命令用于打印当前系统相关信息(内核版本号、硬件架构、主机名称和操作系统类型等)。
[root@iZwz9b7fouo96fhfxbfa6gZ ~]# uname -r
3.10.0-1127.19.1.el7.x86_64
[root@iZwz9b7fouo96fhfxbfa6gZ ~]# 

Docker的基本组成

  • 镜像(image)

    Docker镜像(image)就是一个只读的模板。镜像可以用来创建Docker容器,一个镜像可以创建很多容器。

    它也相当于是一个root文件系统。比如官方镜像centOS7就包含了完整的一套centOS7最小的root文件系统。

    相当于容器的“源代码”,docker镜像文件类似于Java的类模板,而docker容器实例类似于java中new出来的实例对象。

    Docker面向对象
    容器对象
    镜像
  • 容器(container)

    1. 从面向对象的角度

      Docker利用容器(container)独立运行的一个或一组应用,应用程序或服务运行在容器里面,容器就类似于一个虚拟化的运行环境,容器是用镜像创建的运行实例。就像是Java中的类和实例对象一样,镜像是静态的定义,容器是镜像运行时的实体。容器为镜像提供了一个标准的和隔离的运行环境,它可以被启动、开始、停止、删除。每个容器都是相互隔离的、保证安全的平台。

    2. 从镜像容器的角度

      可以把容器看作是一个简易版的Linux环境(包括root用户权限、进程空间、用户空间和网络空间等)和运行在其中的应用程序。

  • 仓库(repository)

    仓库(repository)是集中存放镜像文件的场所。

    类似于:

    Maven仓库(存放各种jar包的地方)

    GitHub仓库(存放各种git项目的地方)

    Docker公司提供的官方Registry被称为Docker Hub,存放各种镜像模板的地方。

    仓库分为公开仓库(Public)和私有仓库(private)两种形势。

    最大的公开仓库是Docker Hub(hub.docker.com/),存放了数量庞大的镜像提供用户下载。

  • 总结

    Docker本身是一个容器运行载体或称之为管理引擎。我们把应用程序和配置依赖打包好形成一个可交付的运行环境,这个打包好的运行环境就是image镜像文件。只有通过这个镜像文件才能生成Docker容器实例(类似Java中new出来一个对象)。

    image文件可以看作是容器的模板。Docker根据image文件生成容器的实例。同一个image文件,可以生成多个同时运行的容器实例。

    镜像文件

    • image文件生成的容器实例,本身也是一个文件,成为镜像文件。

    容器实例

    • 一个容器运行一种服务,当我们需要的时候,就可以通过docker客户端创建一个对应的运行实例,也就是我们的容器

    仓库

    • 就是放一堆镜像的地方,我们可以把镜像发布到仓库中,需要的时候再从仓库中拉下来就可以了。

安装步骤

!注意:一定保证Linux服务器网络连接正常。

安装地址:docs.docker.com/engine/inst…

  1. 确定你是centOS 7及以上版本

    [root@iZwz9b7fouo96fhfxbfa6gZ ~]# cat /etc/redhat-release 
    CentOS Linux release 7.8.2003 (Core)
    
  2. 卸载旧版本(确定没下过docker的兄弟可以跳过此步骤)

    $ sudo yum remove docker \
                      docker-client \
                      docker-client-latest \
                      docker-common \
                      docker-latest \
                      docker-latest-logrotate \
                      docker-logrotate \
                      docker-engine
    
  3. yum安装gcc相关

    #执行:yum -y install gcc
    ​
    [root@iZwz9b7fouo96fhfxbfa6gZ ~]# yum -y install gcc
    Loaded plugins: fastestmirror
    Determining fastest mirrors
    base                                                                                                                            | 3.6 kB  00:00:00     
    epel                                                                                                                            | 4.7 kB  00:00:00     
    extras                                                                                                                          | 2.9 kB  00:00:00     
    updates                                                                                                                         | 2.9 kB  00:00:00     
    (1/3): epel/x86_64/updateinfo                                                                                                   | 1.0 MB  00:00:00     
    (2/3): epel/x86_64/primary_db                                                                                                   | 7.0 MB  00:00:00     
    (3/3): updates/7/x86_64/primary_db                                                                                              |  13 MB  00:00:00     
    Package gcc-4.8.5-44.el7.x86_64 already installed and latest version
    Nothing to do
    [root@iZwz9b7fouo96fhfxbfa6gZ ~]# 
    
    #执行:yum -y install gcc-c++
    ​
    [root@iZwz9b7fouo96fhfxbfa6gZ ~]# yum -y install gcc-c++
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    Package gcc-c++-4.8.5-44.el7.x86_64 already installed and latest version
    Nothing to do
    [root@iZwz9b7fouo96fhfxbfa6gZ ~]# 
    
  4. 安装需要的软件包

    #执行:yum install -y yum-utils
    ​
    [root@iZwz9b7fouo96fhfxbfa6gZ ~]# yum install -y yum-utils
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    Resolving Dependencies
    --> Running transaction check
    ---> 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
    --> Running transaction check
    ---> 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 python-chardet.noarch 0:2.2.1-3.el7 will be installed
    --> Finished Dependency Resolution
    ​
    Dependencies Resolved
    ​
    =======================================================================================================================================================
     Package                                 Arch                            Version                                   Repository                     Size
    =======================================================================================================================================================
    Installing:
     yum-utils                               noarch                          1.1.31-54.el7_8                           base                          122 k
    Installing for dependencies:
     python-chardet                          noarch                          2.2.1-3.el7                               base                          227 k
     python-kitchen                          noarch                          1.1.1-5.el7                               base                          267 k
    ​
    Transaction Summary
    =======================================================================================================================================================
    Install  1 Package (+2 Dependent packages)
    ​
    Total download size: 616 k
    Installed size: 2.8 M
    Downloading packages:
    (1/3): python-chardet-2.2.1-3.el7.noarch.rpm                                                                                    | 227 kB  00:00:00     
    (2/3): yum-utils-1.1.31-54.el7_8.noarch.rpm                                                                                     | 122 kB  00:00:00     
    (3/3): python-kitchen-1.1.1-5.el7.noarch.rpm                                                                                    | 267 kB  00:00:00     
    -------------------------------------------------------------------------------------------------------------------------------------------------------
    Total                                                                                                                  3.8 MB/s | 616 kB  00:00:00     
    Running transaction check
    Running transaction test
    Transaction test succeeded
    Running transaction
      Installing : python-chardet-2.2.1-3.el7.noarch                                                                                                   1/3 
      Installing : python-kitchen-1.1.1-5.el7.noarch                                                                                                   2/3 
      Installing : yum-utils-1.1.31-54.el7_8.noarch                                                                                                    3/3 
      Verifying  : python-kitchen-1.1.1-5.el7.noarch                                                                                                   1/3 
      Verifying  : yum-utils-1.1.31-54.el7_8.noarch                                                                                                    2/3 
      Verifying  : python-chardet-2.2.1-3.el7.noarch                                                                                                   3/3 
    ​
    Installed:
      yum-utils.noarch 0:1.1.31-54.el7_8                                                                                                                   
    ​
    Dependency Installed:
      python-chardet.noarch 0:2.2.1-3.el7                                        python-kitchen.noarch 0:1.1.1-5.el7                                       
    ​
    Complete!
    [root@iZwz9b7fouo96fhfxbfa6gZ ~]# 
    
  5. 设置stable镜像仓库

    !注意,不推荐官网设置地址,因为国内访问国外非常慢,容易出错,官网推荐仓库设置地址:

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

    ★推荐使用阿里云的

    #执行:yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    ​
    [root@iZwz9b7fouo96fhfxbfa6gZ ~]# 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
    [root@iZwz9b7fouo96fhfxbfa6gZ ~]# 
    
  6. 更新yum软件包索引

    #执行:yum makecache fast
    ​
    [root@iZwz9b7fouo96fhfxbfa6gZ ~]# yum makecache fast
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    base                                                                                                                            | 3.6 kB  00:00:00     
    docker-ce-stable                                                                                                                | 3.5 kB  00:00:00     
    epel                                                                                                                            | 4.7 kB  00:00:00     
    extras                                                                                                                          | 2.9 kB  00:00:00     
    updates                                                                                                                         | 2.9 kB  00:00:00     
    (1/2): docker-ce-stable/7/x86_64/updateinfo                                                                                     |   55 B  00:00:00     
    (2/2): docker-ce-stable/7/x86_64/primary_db                                                                                     |  70 kB  00:00:00     
    Metadata Cache Created
    [root@iZwz9b7fouo96fhfxbfa6gZ ~]# 
    
  7. 安装Docker CE

    #执行:yum install docker-ce docker-ce-cli containerd.io
    ​
    [root@iZwz9b7fouo96fhfxbfa6gZ ~]# yum install docker-ce docker-ce-cli containerd.io
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    Package 3:docker-ce-20.10.12-3.el7.x86_64 already installed and latest version
    Package 1:docker-ce-cli-20.10.12-3.el7.x86_64 already installed and latest version
    Package containerd.io-1.4.12-3.1.el7.x86_64 already installed and latest version
    Nothing to do
    [root@iZwz9b7fouo96fhfxbfa6gZ ~]# 
    
  8. 启动docker

    #执行:systemctl start docker[root@iZwz9b7fouo96fhfxbfa6gZ ~]# systemctl start docker
    [root@iZwz9b7fouo96fhfxbfa6gZ ~]# 
    ​
    -----------------------------------------------------------------------------------------------------------------------------
    ​
    #查看docker是否成功启动
    #执行:ps -ef|grep docker[root@iZwz9b7fouo96fhfxbfa6gZ ~]# ps -ef|grep docker
    root     18337     1  0 15:02 ?        00:00:00 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
    root     18718 14866  0 15:04 pts/0    00:00:00 grep --color=auto docker
    [root@iZwz9b7fouo96fhfxbfa6gZ ~]# 
    
  9. 测试

    #查看docker信息
    #执行:docker version
    #Client:客户端;Server:服务端(后台守护进程)
    
    [root@iZwz9b7fouo96fhfxbfa6gZ ~]# docker version
    Client: Docker Engine - Community
     Version:           20.10.12
     API version:       1.41
     Go version:        go1.16.12
     Git commit:        e91ed57
     Built:             Mon Dec 13 11:45:41 2021
     OS/Arch:           linux/amd64
     Context:           default
     Experimental:      true
    
    Server: Docker Engine - Community
     Engine:
      Version:          20.10.12
      API version:      1.41 (minimum version 1.12)
      Go version:       go1.16.12
      Git commit:       459d0df
      Built:            Mon Dec 13 11:44:05 2021
      OS/Arch:          linux/amd64
      Experimental:     false
     containerd:
      Version:          1.4.12
      GitCommit:        7b11cfaabd73bb80907dd23182b9347b4245eb5d
     runc:
      Version:          1.0.2
      GitCommit:        v1.0.2-0-g52b36a2
     docker-init:
      Version:          0.19.0
      GitCommit:        de40ad0
    [root@iZwz9b7fouo96fhfxbfa6gZ ~]# 
    

运行hello-world

#直接执行:docker run hello-world
​
[root@iZwz9b7fouo96fhfxbfa6gZ ~]# docker run hello-world
​
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@iZwz9b7fouo96fhfxbfa6gZ ~]# 

docker run究竟干了什么

image-20220210161651252.png

卸载docker

#执行(停止docker进程服务):systemctl stop docker
#执行(卸载docker引擎、CLI 和容器包):[root@iZwz9b7fouo96fhfxbfa6gZ ~]# yum remove docker-ce docker-ce-cil containerd.io#执行(主机上的映像、容器、卷或自定义配置文件不会自动删除。删除所有映像、容器和卷:):[root@iZwz9b7fouo96fhfxbfa6gZ ~]# rm -rf /var/lib/docker
[root@iZwz9b7fouo96fhfxbfa6gZ ~]# rm -rf /var/lib/containerd

阿里云镜像加速

  • 打开【阿里云】搜索到【容器镜像服务】点击【镜像工具-镜像加速器】获取加速器地址

image-20220210155602408.png

  • 配置镜像加速器
#执行:mkdir -p /etc/docker/
​
[root@iZwz9b7fouo96fhfxbfa6gZ ~]# mkdir -p /etc/docker/
[root@iZwz9b7fouo96fhfxbfa6gZ ~]# 
​
--------------------------------------------------------------------------------------------------------------------------------
​
#执行shell脚本:
​
tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://s5rvr8ab.mirror.aliyuncs.com"]
}
EOF
​
[root@iZwz9b7fouo96fhfxbfa6gZ ~]# tee /etc/docker/daemon.json <<-'EOF'
> {
>   "registry-mirrors": ["https://s5rvr8ab.mirror.aliyuncs.com"]
> }
> EOF
{
  "registry-mirrors": ["https://s5rvr8ab.mirror.aliyuncs.com"]
}
[root@iZwz9b7fouo96fhfxbfa6gZ ~]# 
​
--------------------------------------------------------------------------------------------------------------------------------
​
#执行(激活docker配置文件):systemctl daemon-reload
​
[root@iZwz9b7fouo96fhfxbfa6gZ ~]# systemctl daemon-reload
[root@iZwz9b7fouo96fhfxbfa6gZ ~]# 
​
--------------------------------------------------------------------------------------------------------------------------------
​
#执行(重启docker服务):systemctl restart docker
​
[root@iZwz9b7fouo96fhfxbfa6gZ ~]# systemctl restart docker
[root@iZwz9b7fouo96fhfxbfa6gZ ~]# 

底层原理

  • 为什么Docker会比VM虚拟机快

    1. docker有着比虚拟机更少的抽象层

      由于docker不需要Hypervisor(虚拟机)实现硬件资源虚拟化,运行在docker容器上的程序直接使用的都是实际物理机的硬件资源。因此在CPU、内存利用率上docker将会在效率上有明显优势。

    2. docker利用的是宿主机的内核,而不需要加载操作OS内核

      当新建一个容器时,docker不需要和虚拟机一样重新加载一个操作系统内核。进而避免引寻、加载操作系统内核返回等比较费时费资源的过程,当新建一个虚拟机时,虚拟机软件需要加载OS,返回新建过程是分钟级别的。而docker由于直接利用宿主机的操作系统,则省略了返回过程,因此新建一个docker容器只需要几秒钟。