Docker和传统虚拟机区别

  • 传统虚拟机是虚拟出一套硬件系统,运行一个完整的操作系统,然后再系统上安装和运行软件
  • docker容器不需要自己的内核,因此非常轻量级
  • docker容器内都有一个属于自己的文件系统,每个容器之间互相隔离,之间互不影响

    Docker的DevOps思想(开发、运维)

  • 更快速地交付和部署

    • 传统部署:需要提供一堆配置文件、部署文档
    • docker:打包镜像,测试发布,一键运行
  • 更便捷的升级和扩缩容
    • 一键即可在集群上打包 部署
  • 更简单的系统运维
  • 更高效的利用计算资源
    • 通过容器技术可以在一个服务器上部署很多个tomcat,将服务器的性能发挥到极致

      docker架构图

      镜像(image)
      容器(container)
      启动的镜像叫做容器,可以把容器理解为一个简易的linux系统
      仓库(repository)
      仓库是存放镜像的地方,仓库分为共有仓库(官方的为docker hub,国内的镜像仓库有阿里云、网易等等仓库)、私有仓库(自己搭建的私有镜像仓库)
      docker安装&阿里云镜像加速 - 图1

      docker安装

      帮助文档:https://docs.docker.com/engine/install/centos/
      su先进入root用户模式
      Install Docker Engine on CentOS

      linux确认环境

      查看文件
      1. [admin@localhost /]$ ls
      2. bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
      查看系统环境
      1. # 系统内核是 3.10 以上的
      2. [admin@localhost /]$ uname -r
      3. 4.18.0-305.3.1.el8.x86_64
      查看系统版本
      1. [admin@localhost /]$ cat /etc/os-release
      2. NAME="CentOS Linux"
      3. VERSION="8"
      4. ID="centos"
      5. ID_LIKE="rhel fedora"
      6. VERSION_ID="8"
      7. PLATFORM_ID="platform:el8"
      8. PRETTY_NAME="CentOS Linux 8"
      9. ANSI_COLOR="0;31"
      10. CPE_NAME="cpe:/o:centos:centos:8"
      11. HOME_URL="https://centos.org/"
      12. BUG_REPORT_URL="https://bugs.centos.org/"
      13. CENTOS_MANTISBT_PROJECT="CentOS-8"
      14. CENTOS_MANTISBT_PROJECT_VERSION="8"

      1、卸载旧的版本

      1. sudo yum remove docker \
      2. docker-client \
      3. docker-client-latest \
      4. docker-common \
      5. docker-latest \
      6. docker-latest-logrotate \
      7. docker-logrotate \
      8. docker-engine
      命令回显
      1. No match for argument: docker
      2. No match for argument: docker-client
      3. No match for argument: docker-client-latest
      4. No match for argument: docker-common
      5. No match for argument: docker-latest
      6. No match for argument: docker-latest-logrotate
      7. No match for argument: docker-logrotate
      8. No match for argument: docker-engine
      9. No packages marked for removal.
      10. Dependencies resolved.
      11. Nothing to do.
      12. Complete!

      2、需要的安装包

      1. sudo yum install -y yum-utils
      命令回显 ```shell CentOS Linux 8 - AppStream 4.5 MB/s | 8.2 MB 00:01
      CentOS Linux 8 - BaseOS 447 kB/s | 4.5 MB 00:10
      CentOS Linux 8 - Extras 11 kB/s | 9.8 kB 00:00

      Dependencies resolved.

      Package Architecture Version Repository Size

      Installing: yum-utils noarch 4.0.18-4.el8 baseos 71 k

Transaction Summary

Install 1 Package

Total download size: 71 k Installed size: 22 k Downloading Packages:

yum-utils-4.0.18-4.el8.noarch.rpm 51 kB/s | 71 kB 00:01

Total 29 kB/s | 71 kB 00:02
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 714 kB/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 Installing : yum-utils-4.0.18-4.el8.noarch 1/1 Running scriptlet: yum-utils-4.0.18-4.el8.noarch 1/1 Verifying : yum-utils-4.0.18-4.el8.noarch 1/1 Installed products updated.

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

Complete!

  1. <a name="PDNVl"></a>
  2. ## 3、设置镜像的仓库
  3. ```shell
  4. yum-config-manager \
  5. --add-repo \
  6. https://download.docker.com/linux/centos/docker-ce.repo # 默认是从国外的
  7. yum-config-manager \
  8. --add-repo \
  9. http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # 推荐使用阿里云,十分快

更新yum软件包索引

  1. yum makecache

4、安装docker docker-ce 社区 ee 企业版

  1. sudo yum install docker-ce docker-ce-cli containerd.io

5、启动docker

  1. sudo systemctl start docker

6、使用docker version 是否安装成功

  1. [root@localhost /]# docker version
  2. Client: Docker Engine - Community
  3. Version: 20.10.7
  4. API version: 1.41
  5. Go version: go1.13.15
  6. Git commit: f0df350
  7. Built: Wed Jun 2 11:56:24 2021
  8. OS/Arch: linux/amd64
  9. Context: default
  10. Experimental: true
  11. Server: Docker Engine - Community
  12. Engine:
  13. Version: 20.10.7
  14. API version: 1.41 (minimum version 1.12)
  15. Go version: go1.13.15
  16. Git commit: b0f5bc3
  17. Built: Wed Jun 2 11:54:48 2021
  18. OS/Arch: linux/amd64
  19. Experimental: false
  20. containerd:
  21. Version: 1.4.8
  22. GitCommit: 7eba5930496d9bbe375fdf71603e610ad737d2b2
  23. runc:
  24. Version: 1.0.0
  25. GitCommit: v1.0.0-0-g84113ee
  26. docker-init:
  27. Version: 0.19.0
  28. GitCommit: de40ad0

7、测试hello world

  1. sudo docker run hello-world

命令回显

  1. Unable to find image 'hello-world:latest' locally
  2. latest: Pulling from library/hello-world
  3. b8dfde127a29: Pull complete
  4. Digest: sha256:df5f5184104426b65967e016ff2ac0bfcd44ad7899ca3bbcf8e44e4461491a9e
  5. Status: Downloaded newer image for hello-world:latest
  6. Hello from Docker!
  7. This message shows that your installation appears to be working correctly.
  8. To generate this message, Docker took the following steps:
  9. 1. The Docker client contacted the Docker daemon.
  10. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
  11. (amd64)
  12. 3. The Docker daemon created a new container from that image which runs the
  13. executable that produces the output you are currently reading.
  14. 4. The Docker daemon streamed that output to the Docker client, which sent it
  15. to your terminal.
  16. To try something more ambitious, you can run an Ubuntu container with:
  17. $ docker run -it ubuntu bash
  18. Share images, automate workflows, and more with a free Docker ID:
  19. https://hub.docker.com/
  20. For more examples and ideas, visit:
  21. https://docs.docker.com/get-started/

8、查看一下下载的这个hello-world镜像

  1. docker images

命令回显

  1. REPOSITORY TAG IMAGE ID CREATED SIZE
  2. hello-world latest d1165f221234 4 months ago 13.3kB

9、了解:卸载docker

1、卸载依赖

  1. sudo yum remove docker-ce docker-ce-cli containerd.io

2、删除资源

  1. sudo rm -rf /var/lib/docker
  2. sudo rm -rf /var/lib/containerd

3、docker的默认工作路径

  1. /var/lib/containerd

阿里云镜像加速

系统环境:CentOS7
1. 访问 https://www.aliyun.com/ ,搜索“容器镜像服务”
docker安装&阿里云镜像加速 - 图2
2. 点击“管理控制台”,注册一个属于自己的阿里云账户(可复用淘宝账号)
3. 登录,根据引导进入主界面
docker安装&阿里云镜像加速 - 图3
4. 点击“镜像加速器”,获取加速器地址,下面也有操作文档
docker安装&阿里云镜像加速 - 图4
5. 添加或修改/etc/docker/daemon.json配置加速器

  1. mkdir -p /etc/docker
  2. vim /etc/docker/daemon.json
  3. {
  4. # 添加阿里云上面的加速器地址:
  5. "registry-mirrors": ["https://{xxxxxxx}.mirror.aliyuncs.com"]
  6. }
  7. systemctl daemon-reload
  8. systemctl restart docker

这里提供一个可用的加速器:

  1. {
  2. "registry-mirrors": ["https://9cpn8tt6.mirror.aliyuncs.com"]
  3. }

回顾HelloWorld流程

image.png
image.png

底层原理

Docker是怎么工作的?

Docker是一个Client - Server 结构的系统,Docker的守护进程运行在主机上。通过Socket从客户端访问!
Docker-Server接收到Docker-Client 的指令,就会执行这个命令!
image.png

Docker为什么比VM快?

1、Docker有着比虚拟机更少的抽象层
2、Docker利用的是宿主机的内核,vm需要是Guest OS。
image.png
所以说,新建一个容器时,docker直接运行在宿主机内核上,运行级别为秒级;虚拟机是需要重新加载一个Guest OS内核,运行级别为分钟级。

docker和VM的对比

docker安装&阿里云镜像加速 - 图9