【Docker三连问】什么是Docker? | Docker风靡原因? | 如何安装使用Docker?

参考链接

官网:Home - Docker

文档:Docker Documentation | Docker Documentation

接下来让我们带着对【Docker】的三连问——What、Why、How,一起踏入【Docker】世界的大门。了解并学习Docker知识。

image-20220416001854571

前言

随着数字化转型浪潮的扑面而来。云原生技术的发展突飞猛进,企业上云成为了千行百业的普遍共识。Docker、K8S、云原生,人工智能、网络安全、大数据,可持续化部署,敏捷开发等新兴技术领域得到快速蓬勃发展。从虚拟化时代的起步发展,到进入容器化时代的如火如荼的发展,越来越多的开发者喜欢并拥抱上了容器化技术。基于中国云原生用户调查报告显示,在用户生产环境中,容器技术的采纳率已经接近70%。容器时代的发展,得益于虚拟化技术的成熟和普及,使得应用上云成为企业转型的关键阶段和有效控制运维成本,提升产业效能的重要措施。

一、什么是Docker?

首先,让我们一起来看看各大搜索引擎和厂商对Docker定义是怎样的:

  • Docker 是一个用于开发、发布和运行应用程序的开放平台。Docker 使您能够将应用程序与基础架构分离,以便您可以快速交付软件。 ——来自Docker官方

  • Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的 Linux或Windows操作系统的机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口。 ——来自百度百科

  • Docker 是一个开放源代码软件,是一个开放平台,用于开发应用、交付(shipping)应用、运行应用。Docker允许用户将基础设施(Infrastructure)中的应用单独分割出来,形成更小的颗粒(容器),从而提高交付软件的速度。 ——来自维基百科

  • Docker 是一个开源平台,用于构建、部署和管理容器化应用程序。 ——来自IBM

  • Docker 是一种开源项目,用于将应用程序自动部署为可在云或本地运行的便携式独立容器。 Docker 也是一家公司,它与云、Linux 和 Windows 供应商(包括 Microsoft)协作,致力于推广和发展这项技术。 ——来自微软

    扫描二维码关注公众号,回复: 14214519 查看本文章

从中,我们能够提取到几个关键字,开源 开放平台 容器 构建部署 快速交付 沙箱机制

根据上述的对Docker大同小异的定义和从提取到的关键字可知道,Docker是什么?

综上所述,可得知

Docker是一个开源的开放的容器化平台,用于构建开发部署运行应用,能够将应用程序与基础架构分离,满足快速交付、可移植性部署、应用程序之间相互独立等用户需求,应用程序之间相互隔离,互不影响。Docker通过隔离机制,将服务器的性能五发挥到极致。

二、Docker风靡原因?

Docker的优势所在

  • 能够使用容器构建、共享和运行应用程序。

    image-20220416003937055

  • 将软件打包成标准化单元,用于开发、装运和部署。

    image-20220416003644675

  • Docker容器无处不在:Linux,Windows,数据中心,云,无服务器等。
    image-20220416003842418

Docker与虚拟机比较

容器和虚拟机具有类似的资源隔离和分配优势,但功能不同,因为容器虚拟化操作系统而不是硬件。容器更便携、更高效。

  • 虚拟机运行结构图

虚拟机 (VM) 是将一台服务器转换为多台服务器的物理硬件的抽象。虚拟机管理程序允许在一台计算机上运行多个 VM。每个 VM 都包含操作系统、应用程序、必要的二进制文件和库的完整副本,占用数十 GB。VM 的启动速度也可能很慢。

image-20220416004436922

  • 容器运行结构图

容器是应用层的抽象,它将代码和依赖项打包在一起。多个容器可以在同一台计算机上运行,并与其他容器共享操作系统内核,每个容器在用户空间中作为隔离的进程运行。容器占用的空间比 VM 少(容器映像的大小通常为数十 MB),可以处理更多的应用程序,并且需要更少的 VM 和操作系统。

image-20220416004632628

容器还会在共享 OS 上将应用程序彼此隔离开。 容器化应用程序在容器主机上运行,而容器主机在 OS(Linux 或 Windows)上运行。 因此,容器的占用比虚拟机 (VM) 映像小得多。

每个容器可以运行整个 Web 应用或服务,Docker 主机是容器主机,而 App1、App2、Svc 1 和 Svc 2 是容器化应用程序或服务。

容器化可以将软件及其所有依赖项打包在一个自包含的软件包中,这样就可以省略麻烦的配置,直接运行。

容器化的另一个优势在于可伸缩性。 通过为短期任务创建新容器,可以快速扩大。 从应用程序的角度来看,实例化映像(创建容器)类似于实例化 服务或 Web 应用等进程。 但出于可靠性考虑,在多个主机服务器上运行同一映像的多个实例时,通常要使每个容器(映像实例)在不同容错域中的不同主机服务器或 VM 中运行。

总而言之,容器在整个应用程序生命周期工作流中提供以下优点:隔离性、可移植性、灵活性、可伸缩性和可控性。 最重要的优点是可在开发和运营之间提供隔离。

三、如何安装使用Docker?

本实验是基于CentOS 7.9进行演示操作的。

配置Docker镜像

# 安装需要的依赖包
yum install -y yum-utils

# 设置阿里云docker镜像
yum-config-manager \
    --add-repo \
     https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

# 生成本地缓存
yum makecache

# 配置阿里云docker镜像加速
sudo mkdir -p /etc/docker

sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://w2kavmmf.mirror.aliyuncs.com"],
  "exec-opts": ["native.cgroupdriver=systemd"]
}
EOF

sudo systemctl daemon-reload

# 查看docker所有版本信息
yum list docker-ce --showduplicates | sort -r
[root@m1 ~]# yum list docker-ce --showduplicates | sort -r
Last metadata expiration check: 0:01:17 ago on Fri 01 Apr 2022 10:57:15 PM CST.
docker-ce.x86_64                3:20.10.9-3.el8                 docker-ce-stable
docker-ce.x86_64                3:20.10.8-3.el8                 docker-ce-stable
docker-ce.x86_64                3:20.10.7-3.el8                 docker-ce-stable
docker-ce.x86_64                3:20.10.6-3.el8                 docker-ce-stable
docker-ce.x86_64                3:20.10.5-3.el8                 docker-ce-stable
docker-ce.x86_64                3:20.10.4-3.el8                 docker-ce-stable
docker-ce.x86_64                3:20.10.3-3.el8                 docker-ce-stable
docker-ce.x86_64                3:20.10.2-3.el8                 docker-ce-stable
docker-ce.x86_64                3:20.10.14-3.el8                docker-ce-stable
docker-ce.x86_64                3:20.10.1-3.el8                 docker-ce-stable
docker-ce.x86_64                3:20.10.13-3.el8                docker-ce-stable
docker-ce.x86_64                3:20.10.12-3.el8                docker-ce-stable
docker-ce.x86_64                3:20.10.11-3.el8                docker-ce-stable
docker-ce.x86_64                3:20.10.10-3.el8                docker-ce-stable
docker-ce.x86_64                3:20.10.0-3.el8                 docker-ce-stable
docker-ce.x86_64                3:19.03.15-3.el8                docker-ce-stable
docker-ce.x86_64                3:19.03.14-3.el8                docker-ce-stable
docker-ce.x86_64                3:19.03.13-3.el8                docker-ce-stable
Available Packages

安装Docker

# 安装docker  docker-ce   ee企业版
yum install -y docker-ce docker-ce-cli containerd.io

# 启动Docker
systemctl start docker && systemctl enable docker && systemctl status docker

# 查看docker版本信息
# docker version
Client: Docker Engine - Community
 Version:           20.10.14
 API version:       1.41
 Go version:        go1.16.15
 Git commit:        a224086
 Built:             Thu Mar 24 01:47:44 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.14
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.15
  Git commit:       87a90dc
  Built:            Thu Mar 24 01:46:10 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.5.11
  GitCommit:        3df54a852345ae127d1fa3092b95168e4a88e2f8
 runc:
  Version:          1.0.3
  GitCommit:        v1.0.3-0-gf46b6ba
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

使用Docker命令

查看帮助命令。

[root@docker ~]# docker --help

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:
  app*        Docker App (Docker Inc., v0.9.1-beta3)
  builder     Manage builds
  buildx*     Docker Buildx (Docker Inc., v0.8.0-docker)
  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
  scan*       Docker Scan (Docker Inc., v0.17.0)
  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/

查看docker具体命令的使用方法。

# 查看docker镜像命令的使用
[root@docker ~]# docker image --help

Usage:  docker image COMMAND

Manage images

Commands:
  build       Build an image from a Dockerfile
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Display detailed information on one or more images
  load        Load an image from a tar archive or STDIN
  ls          List images
  prune       Remove unused images
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rm          Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

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

# 查看docker网络命令的使用
[root@docker ~]# docker network --help

Usage:  docker network COMMAND

Manage networks

Commands:
  connect     Connect a container to a network
  create      Create a network
  disconnect  Disconnect a container from a network
  inspect     Display detailed information on one or more networks
  ls          List networks
  prune       Remove all unused networks
  rm          Remove one or more networks

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

猜你喜欢

转载自blog.csdn.net/qq_45392321/article/details/124207828