Table of Contents

一、Docker 概述

开发人员也需要基础的运维操作。

开发一些 jar 运行需要依赖(redis、mysql等环境),重新安装部署会很麻烦,可以使用Docker 打包jar包及所依赖的服务环境。

相比于Vmware 等其他虚拟化技术更加轻量级。

Docker 是基于 Go 语言开发的!开源项目!

官网:https://www.docker.com/

image-20211013204900239

文档地址:https://docs.docker.com/ Docker 的文档是超级详细的。

仓库地址:https://hub.docker.com/

二、Docker 基础概念

1. Docker 与 传统虚拟化技术比较

img

img

  • 传统虚拟机技术是虚拟出一套硬件后,在其上运行一个完整操作系统,在该系统上再运行所需应用进程;
  • 容器内的应用进程直接运行于宿主的内核,容器内没有自己的内核,而且也没有进行硬件虚拟。因此容器要比传统虚拟机更为轻便。

2. 为什么要使用 Docker

image-20211013205814632

  1. 更高效的利用系统资源
  2. 更快速的启动时间
  3. 一致的运行环境
  4. 持续交付和部署
  5. 更轻松的迁移
  6. 更轻松的维护和扩展

3. Docker 的基本组成

img

三、Docker 基本概念

Docker – 从入门到实践

Docker

Docker 包括三个基本概念、

  • 镜像(Image)

    • Docker 镜像是用于创建 Docker 容器的模板。
    • 镜像构建时,会一层层构建,前一层是后一层的基础。每一层构建完就不会再发生改变,后一层上的任何改变只发生在自己这一层。
    • 容器与镜像的关系类似于面向对象编程中的对象与类.
  • 容器(Container)

    • 容器是独立运行的一个或一组应用,容器的实质就是进程。
    • 可以将容器看作对应镜像的一个实例。
  • 仓库(Repository)

    与 git 仓库相似,通常,一个仓库会包含同一个软件不同版本的镜像,而标签就常用于对应该软件的各个版本。我们可以通过 <仓库名>:<标签> 的格式来指定具体是这个软件哪个版本的镜像。如果不给出标签,将以 latest 作为默认标签。

四、安装 Docker

进入 Docker 官网:https://docs.docker.com/engine/install/centos/

找到对应操作系统的安装教程,我使用的是 Centos 7。

image-20211021130915088

  1. 查看本机系统版本是否满足要求

    1
    2
    
    [user-for-dockerStudy@izuf6adsdfwblstgm2jy8sz ~]$ cat /etc/redhat-release
    CentOS Linux release 7.3.1611 (Core) 
    
  2. 卸载老版本 docker or docker-engine

    image-20211021134139237

    提示不包含软件包,则表明正常。

  3. 使用 yum 安装

    执行以下命令安装依赖包:

    1
    2
    3
    
    $ sudo yum install -y yum-utils \
               device-mapper-persistent-data \
               lvm2
    

    执行下面的命令添加 yum 软件源:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    $ sudo yum-config-manager \
        --add-repo \
        https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo
    
    
    # 官方源
    # $ sudo yum-config-manager \
    #     --add-repo \
    #     https://download.docker.com/linux/centos/docker-ce.repo    
    
  4. enable 最新版 Docker CE

    1
    2
    
    # $ sudo yum-config-manager --enable docker-ce-edge
    $ sudo yum-config-manager --enable docker-ce-nightly
    
  5. 安装 Docker CE

    更新 yum 软件源缓存,并安装 docker-ce

    1
    2
    
    $ sudo yum makecache fast
    $ sudo yum install docker-ce
    
  6. 启动 Docker CE

    1
    2
    
    $ sudo systemctl enable docker
    $ sudo systemctl start docker
    
  7. 测试 Docker 是否安装正确

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    
    $ sudo docker run hello-world
    
    Unable to find image 'hello-world:latest' locally
    latest: Pulling from library/hello-world
    2db29710123e: Pull complete 
    Digest: sha256:37a0b92b08d4919615c3ee023f7ddb068d12b8387475d64c622ac30f45c29c51
    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/
    
  8. 配置 Docker 镜像加速器

    image-20211021141316505

    1
    2
    3
    4
    5
    6
    7
    8
    
    sudo mkdir -p /etc/docker
    sudo tee /etc/docker/daemon.json <<-'EOF'
    {
      "registry-mirrors": ["https://hoh0x7h1.mirror.aliyuncs.com"]
    }
    EOF
    sudo systemctl daemon-reload
    sudo systemctl restart docker