Docker -- 容器技术概述

x33g5p2x  于2022-06-08 转载在 Docker  
字(6.2k)|赞(0)|评价(0)|浏览(289)

参考:
1.《每天5分钟玩转Docker容器技术》
2. https://mp.weixin.qq.com/s/SsXIuDqeze_et8vWQ8fr3g

容器核心知识有三点:

  • What:什么是容器
  • Why:为什么需要容器
  • How:容器是怎样工作的

1、What

1.1 概述

  • 通俗来说,容器就是一种可以使得应用程序在任何虚拟软件上以相同方式运行的一种技术;
  • 通俗来说,容器就是将我们的某一个应用程序运行所必须的相关细节封装起来,使得这个功能可以在任何的操作系统上运行。

1.2 组成

由上面的概念来看,容器由两部分组成:

  • 应用程序本身
  • 运行时所需要的依赖

1.3 容器与虚拟机的区别

将容器与虚拟机进行对比,主要是因为他俩都可以为应用提供封装和隔离;

图中:

  • APP:表示应用程序;
  • Bins/Libs:表示依赖的库;
  • GuestOS:表示虚拟机的操作系统;
  • Hypervisor:用来协调多个虚拟机;
  • Host OS:物理机的操作系统;
  • Server:服务器;

从图中我们可以看出:

异同点虚拟机容器
依赖每个应用程序对应一个依赖部分应用程序可以对应同一个依赖
操作系统每个应用程序对应一个操作系统所有应用程序可以共享一个操作系统

2、Why

回顾上边的概述,容器使得同一款应用程序可以在不同的操作系统上以相同的方式运行;

换句话说,容器使得应用程序具备了超强的可移植性。

所以,我们需要容器;

2.1 软件系统现状

  • 现如今,几乎所有的应用软件都采用三层架构,并且每一个应用都可能包含多种服务,每种服务都有自己的依赖;
  • 现如今,应用程序会被部署在不同的环境中,服务在运行时就可能需要动态的迁移;
  • 所以,如何让每种服务能够在所有的部署环境中顺利的运行?

2.2 集装箱

在这里,我们先将之前的问题放一放,来了解了解集装箱;

在集装箱发明之前,我们传统运输行业存在着类似于软件系统所面临的问题:

  • 在每一次运输时,货主与承运方都会担心因货物类型不同而导致损失,比如,将铁通放在了一堆香蕉上;

  • 不是所有的运输都只需要一种交通工具,当A和B两地之间存在一条河时,这时候运输过程就比较麻烦了:

  • 先将服务装上车,运到码头,卸货,装船;到岸后,卸货,装车;到目的地,最后卸货;

  • 在以上过程中,不光装卸货浪费时间,并且还容易导致货物损坏;

集装箱发明了,所以,问题迎刃而解了:

  • 有了集装箱,我们将香蕉和铁通放在各自的集装箱中;
  • 在运输过程中,集装箱都是密封的,只有到达目的地才会被打开;
  • 而且统一规格的集装箱更可以高效的实现装卸、重叠和运输;

2.3 Docker

对于这里突然出现的 Docker,暂时不用关注太多,只需要了解到,Docker 是用来创建容器的工具就行;

Docker 将集装箱的思想运用到软件打包上,将任何应用及其依赖打包成一个可以在任何操作系统上运行的容器;

其实,集装箱和容器的英文都是“container”,翻译成容器是因为国内软件领域约定俗称的缘故吧;

从 docker 的 logo,就可以看出,容器就是一堆集装箱:

Docker 与集装箱特性对比

特性集装箱docker
打包对象几乎所有货物任何软件及其依赖
硬件依赖标准形状和接口允许集装箱被装卸到各种交通工具,整个运输过程无须打开容器无需修改便可运行在所有的平台上—虚拟机,物理机…
隔离性集装箱可以重叠起来运输,香蕉再也不会被铁通压烂资源、网络、库都是隔离的,不会出现依赖问题
自动化标准接口使集装箱很容易自动装卸和移动提供run、start、stop等标准化操作,非常适合自动化
高效性无需开箱,可以在各种交通工具间快速搬运轻量级,能够快速启动和迁移
职责分工货主只需考虑把什么放到集装箱;承运方只考虑怎么运输集装箱;开发人员只考虑怎么写代码;运维人员只关心如何配置基础环境;

容器的优势

  • 对于开发人员:Build Once,Run Anywhere;
  • 对于运维人员:Configure Once,Run Anything;

3、How

Docker 就是一个 C/S(客户端/服务器) 架构,客户端与服务器进行交互时:

  • 客户端向服务器发送请求将镜像从服务端拉去下来;
  • 服务器负责构建、运行和分发 Docker 镜像;
  • Docker 客户端和服务端可运行在同一台机器上,可以通过 RESTFul、stock 或网络接口与远程 Docker 服务端进行通信;

由上图可得,Docker 的核心组件包括以下:

  • Docker Client
  • Docker daemon
  • Docker Image
  • Docker Container
  • Docker Registry

3.1 Docker Client

  • Docker 客户端其实就是 Docker 提供的命令行界面工具,便于 Docker 用户与 Docker 进行交互;

  • 最常用的 Docker 客户端就是 docker命令;

  • docker 命令可以很方便的在 host 上构建和运行 docker 容器

root@selenium:~# docker

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/root/.docker")
  -c, --context string     Name of the context to use to connect to the daemon (overrides DOCKER_HOST env
                           var and default context set with "docker context use")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to
  -l, --log-level string   Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/root/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/root/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/root/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Management Commands:
  builder     Manage builds
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  image       Manage images
  manifest    Manage Docker image manifests and manifest lists
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a Dockerfile
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  events      Get real time events from the server
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  login       Log in to a Docker registry
  logout      Log out from a Docker registry
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  ps          List containers
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  version     Show the Docker version information
  wait        Block until one or more containers stop, then print their exit codes

Run 'docker COMMAND --help' for more information on a command.

To get more help with docker, check out our guides at https://docs.docker.com/go/guides/

3.2 Docker daemon

  • docker daemon 负责响应客户端的请求,负责创建、运行、监控容器,构建、存储镜像;

由 Dockers Daemon 架构图可知:

  1. Docker Daemon 通过 Docker Server 模块接收 Docker Client 的请求;
  • 然后在 Engine 中处理请求;
  1. 其中,Job 是根据请求类型,创建出的任务;
  2. Job 在运行过程中有以下几种可能:
  • 从 Docker Registry 获取镜像;
  • 通过 graphdriver 执行容器镜像的本地化操作;
  • 通过 networkdriver 执行容器网络环境的配置;
  • 通过 execdriver 执行容器内部运行的执行工作;

Docker Daemon 启动也是通过 docker命令完成的;

docker --daemon=true
docker -d
dpcker -d=true

默认配置下,Docker Daemon 只能响应来自本地 Host 的客户端请求;

如果需要允许远程客户端请求,则需要在配置文件中打开 TCP监听:

  • 编辑配置文件 /etc/systemd/system/multi-user.target.wants/docker.service,在环境变量 ExecStart后面添加 -H tcp://0.0.0.0,允许来自任意 IP 的客户端连接;
  • 重启 Docker Daemon
systemctl daemon-reload
systemctl restart docker.service
  • 通过 -H参数与远程服务器通信
docker -H 192.168.1.1 info

3.3 docker image

Docker Image 可以看作是一个操作系统光盘,这个操作系统里边包含了你的程序、依赖、配置等,这个光盘你使用后,其中的数据也不会改变,所以可以将其看作一个只读模板,我们用它来创建 Docker 容器;

Docker 镜像的生成方法:

  • 从无到有开始创建;
  • 下载并使用别人创建好的现成的镜像;
  • 在现有镜像上进行修改,增添变成自己需要的新镜像;
    可以通过将镜像内容和创建步骤描述在一个文本文件中,这个文件就是传说中的 Dockerfile

通过命令 docker build <dockerfile> 来构建 Docker 镜像;

3.4 Docker Registry

Docker Registry,顾名思义就是存放 Docker Image 的仓库;

  • 我们在客户端通过 docker pull/push命令通过服务器与仓库进行通信;

3.5 Docker Container

  • Docker Container 就是 Docker Image 运行的实例;
  • 是真正运行项目的程序、消耗系统资源和提供服务的地方;

3.6 一个完整的例子

  • 因为本地没有配置好 Docker 就不做具体的演示,以图示理解一下;

  1. 如果本地 Registry 含有我们需要的镜像,那么使用 docker run ubuntu后,Daemon 就可以直接启动 ubuntu 容器;
  2. 如果本地 Registry 没有我们所需要的镜像,那么 Daemon 会从 Docker Hub(远端的 Registry)将我们需要的镜像下载到本地 Registry,然后启动 ubuntu 容器;

开发者涨薪指南

48位大咖的思考法则、工作方式、逻辑体系

相关文章