Docker 学习 一:Docker 是什么

Docker 学习 一

Docker 是什么

网址:hub.docker.com/

docker对进程进行封装隔离,属于 操作系统层面的虚拟化技术

由于隔离的进程独立于宿主和其它的隔离的进程,因此也称其为容器

docker 应用场景

  • 自动化测试和持续集成、发布
  • Web 应用的自动化打包和发布
  • 后台应用易部署

docker 的优势

  • 快速, 一致的交付应用程序
  • 可移植,可扩展
  • 轻巧,快速,经济,高效,压榨linux自身资源

Docker 能做什么?

先来说说 Docker 和虚拟机有啥不一样的

以前的虚拟机这样的,系统占用资源大,很多步骤是冗余的,并且启动还很慢,不能忍

现在的 Docker 是这个样子的,

容器之间互相隔离,互补干扰,一起运行在同一个操作系统上,最大化使用操作系统资源

Docker 技术和虚拟机技术的不同?

  • 每个容器间都是相互隔离的,他们有属于自己的文件系统,相互不会有影响
  • 容器没有自己的内核,没有自己的硬件,容器内的应用是直接运行在宿主机的内核中
  • 传统的虚拟机是虚拟出一个硬件,运行完成的操作系统,在其上面运行应用

那么 Docker 具体能做什么?

做 DevOps

做 DevOps 有如下几个提升点:

  • 应用可以更快捷的部署和交付

以前麻烦的安装步骤一去不复返,使用 Docker 容器化后,打包镜像发布测试,一键部署及运行

  • 可以更方便的升级和扩容

使用 Docker,将项目打包成镜像,升级方便,扩容方便

  • 开发,运维,测试都会更简单

再也不用担心开发环境,测试环境,运维环境不一致的情况了

  • 更高效的利用资源

Dcoker 是运行在宿主机的内核中,可以在这台物理主机上部署多个 Docker 实例

Docker 的组成

Docker 使用客户端-服务器 (C/S) 架构模式,使用远程API来管理和创建 Docker 容器

Docker 的三个基本概念:

图片来源于网络

  • 镜像

相当于是一个 root 文件系统,类似于一个模板,这是静态的

  • 容器

相当于从模板拉出来的一个实例,容器通过镜像来创建,我们可以对他做创建,启动,停止,暂停,删除等操作

  • 仓库

用来保存镜像的,可以看做是一个代码控制中心

Docker 的安装和使用

安装

网络上安装 Docker 的方式大致有如下几种:

  • 官方脚本自动安装
  • 使用 Docker 仓库安装
  • 使用 ==shell== 脚本安装

咱们以 ubuntu 的系统为例子,使用 Docker 仓库的方式进行安装,我的ubuntu 系统版本如下:

# cat /etc/os-release
NAME="Ubuntu"
VERSION="18.04.5 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.5 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic

设置仓库

安装依赖包

sudo apt-get update
sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common

添加 Docker 的官方 GPG 密钥:

curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

验证秘钥,可以直接搜索 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88 后面的8个字符

sudo apt-key fingerprint 0EBFCD88

有如下输出为正确设置了秘钥

pub   rsa4096 2017-02-22 [SCEA]
      9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid           [ unknown] Docker Release (CE deb) <docker@docker.com>
sub   rsa4096 2017-02-22 [S]

设置稳定版仓库

sudo add-apt-repository \
   "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/ \
  $(lsb_release -cs) \
  stable"

安装 Docker

安装最新的 Docker 版本

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

安装完成后,验证是否OK

可以通过 docker version 来查看 docker 的版本

# docker version
Client: Docker Engine - Community
 Version:           20.10.7
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        f0df350
 Built:             Wed Jun  2 11:56:40 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.7
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       b0f5bc3
  Built:            Wed Jun  2 11:54:48 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.8
  GitCommit:        7eba5930496d9bbe375fdf71603e610ad737d2b2
 runc:
  Version:          1.0.0
  GitCommit:        v1.0.0-0-g84113ee
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

运行一个 hello-world

sudo docker run hello-world

出现如下信息,为 docker 安装成功

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/

当然,你也可以选择不安装最新的,安装自己指定的版本也可

  1. 使用 ==apt-cache madison docker-ce== 查看仓库中可用的版本

  2. 使用 ==sudo apt-get install docker-ce= docker-ce-cli= containerd.io== 安装指定版本的 Docker

  3. 使用 ==sudo docker run hello-world== 验证是否安装成功

  • 查看 docker 镜像
# docker images
REPOSITORY        TAG       IMAGE ID       CREATED        SIZE
hello-world       latest    d1165f221234   4 months ago   13.3kB
  • docker 镜像
卸载镜像
sudo apt-get purge docker-ce docker-ce-cli containerd.io

删除安装目录
/var/lib/docker 是docker 的默认安装路径
 sudo rm -rf /var/lib/docker
 sudo rm -rf /var/lib/containerd

镜像加速

如果是使用阿里云服务器的小伙伴可以看这一步

配置镜像加速,需要 docker 的安装版本在 1.10.0 以上,我们当前安装的 docker 版本为 1.41,完全符合

我们可以通过修改 daemon 配置文件 /etc/docker/daemon.json 来使用加速器,执行如下指令

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://uhr5ub75.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

docker run 的流程

  • docker run 现在本地找对应的镜像,若有则直接运行
  • 若没有就去docker hub 上下载,若有就下载到本地后运行
  • 若没有就直接报错

Docker 的底层原理

Docker 是如何工作的?

docker 是一个C/S 模型,docker 的后台守护进行运行在主机上,客户端和服务端通过套接字 Socket 通信

docker 服务端收到 docker 客户端的指令时,则执行该指令

为什么 Docker 比 虚拟机快呢?

在网络上找了一张图,咱们对比一下就明确了

如图,Docker 比虚拟机快的原因如下:

  • docker 比虚拟机的抽象层更少
  • docker 利用的是宿主机的内核,而虚拟机是需要新建一个 OS

基于如上 2 点,虚拟机启动时,会加载操作系统,启动慢,时间基本上是分钟级

docker 启动的时候,不需要加载操作系统内核,因此快,时间基本上是秒级

参考资料:

docker docs

欢迎点赞,关注,收藏

朋友们,你的支持和鼓励,是我坚持分享,提高质量的动力

好了,本次就到这里

技术是开放的,我们的心态,更应是开放的。拥抱变化,向阳而生,努力向前行。

我是小魔童哪吒,欢迎点赞关注收藏,下次见~

本作品采用《CC 协议》,转载必须注明作者和本文链接
关注微信公众号:阿兵云原生
讨论数量: 1

你标题docker打错了

2年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!