Docker技术(容器虚拟化技术)

Docker介绍

Docker的主要目标是“Build,Ship and Run Any App,Anywhere”,
也就是通过对应用组件的封装、分发、部署、运行等生命周期的管理,
使用户的APP(可以是一个WEB应用或数据库应用等等)及其运行环境能够做到“一次封装,到处运行”。

Linux 容器技术的出现就解决了这样一个问题,而 Docker 就是在它的基础上发展过来的。
将应用运行在 Docker 容器上面,而 Docker 容器在任何操作系统上都是一致的,
这就实现了跨平台、跨服务器。只需要一次配置好环境,换到别的机子上就可以一键部署好, 大大简化了操作

Docker是解决运行环境和配置问题的软件容器 , 方便做持续集中并有助于整体发布的容器虚拟化技术

虚拟化技术与容器虚拟化技术

虚拟化技术

虚拟机(virtual machine)就是带环境安装的一种解决方案。
它可以在一种操作系统里面运行另一种操作系统,比如在Windows 系统里面运行Linux 系统。
应用程序对此毫无感知,因为虚拟机看上去跟真实系统一模一样
缺点 :1 资源占用多 2 冗余步骤多 3 启动慢

容器虚拟化技术

Linux 容器(Linux Containers,缩写为 LXC)。
Linux 容器不是模拟一个完整的操作系统,而是对进程进行隔离。
有了容器,就可以将软件运行所需的所有资源打包到一个隔离的容器中。
容器与虚拟机不同,不需要捆绑一整套操作系统,只需要软件工作所需的库资源和设置。
系统因此而变得高效轻量并保证部署在任何环境中的软件都能始终如一地运行。

区别与联系

  • 虚拟机虽然可以隔离出很多「子电脑」,但占用空间更大,启动更慢。虚拟机软件可能还要花钱,例如VMWare;
  • 容器技术不需要虚拟出整个操作系统,只需要虚拟一个小规模的环境,类似「沙箱」;
  • 运行空间,虚拟机一般要几 GB 到 几十 GB 的空间,而容器只需要 MB 级甚至 KB 级;

认识Docker

Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口。Docker 技术的三大核心概念,分别是:镜像 Image、容器 Container、仓库 Repository。

核心概念

  1. Docker 本身并不是容器,它是创建容器的工具,是应用容器引擎
  2. Docker 三大核心概念,分别是:镜像 Image,容器 Container、仓库 Repository;
  3. Docker 技术使用 Linux 内核和内核功能(例如 Cgroups 和 namespaces)来分隔进程,以便各进程相互独立运行。
  4. 由于 Namespace 和 Cgroups 功能仅在 Linux 上可用,因此容器无法在其他操作系统上运行。那么 Docker 如何在 macOS 或 Windows 上运行?Docker 实际上使用了一个技巧,并在非 Linux 操作系统上安装 Linux 虚拟机,然后在虚拟机内运行容器。
  5. 镜像是一个可执行包,其包含运行应用程序所需的代码、运行时的库、环境变量和配置文件.容器是镜像的运行时实例。

Docker技术的基础

  • namespace

容器隔离的基础,保证A容器看不到B容器. 6个名空间:User,Mnt,Network,UTS,IPC,Pid.

  • cgroups

容器资源统计和隔离。主要用到的cgroups子系统:cpu,blkio,device,freezer,memory

  • unionfs

典型:aufs/overlayfs,分层镜像实现的基础

Docker原理

Docker是一个Client-Server结构的系统,Docker守护进程运行在主机上,
然后通过Socket连接从客户端访问,守护进程从客户端接受命令并管理运行在主机上的容器。

Docker安装

[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ls
CentOS-Base.repo  epel-modular.repo     epel.repo                  epel-testing.repo epel-playground.repo  epel-testing-modular.repo  redhat.repo

[root@localhost yum.repos.d]# wget http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

## 安装容器相关工具
[root@localhost yum.repos.d]# yum install -y container*
......
## 安装docker-ce
[root@localhost yum.repos.d]# yum install -y docker-ce

## 查看版本
[root@localhost yum.repos.d]# docker -v
Docker version 20.10.1, build 831ebea

Docker镜像加速

国内从 DockerHub 拉取镜像有时会遇到困难,此时可以配置镜像加速器。Docker 官方和国内很多云服务商都提供了国内加速器服务。

例如:

Docker镜像加速配置

## 开启docker守护进程
root@localhost etc]# systemctl enable --now docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
[root@localhost etc]# cd /etc/docker/
[root@localhost docker]# ls
key.json
##  创建加速器的配置文件
[root@localhost docker]# vim daemon.json 
{
    "registry-mirrors": ["https://registry.docker-cn.com"] ### 中括号内填写国内加速器服务商地址 
}

## 重启docker
[root@localhost docker]# systemctl restart docker

## 查看是否生效

[root@localhost docker]# docker info
......
 Registry Mirrors:
  https://registry.docker-cn.com/   ## 可以看到已经有国内网址,表示已经生效
 Live Restore Enabled: false

WARNING: No blkio weight support
WARNING: No blkio weight_device support

Docker常用命令

镜像仓库相关的命令

  • Docker search:从Docker Hub查找镜像
    • 语法:
    docker search [OPTIONS] TERM
    
    • 实例:
[root@localhost docker]# docker search nginx
NAME                               DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                              Official build of Nginx.                        14242     [OK]       
jwilder/nginx-proxy                Automated Nginx reverse proxy for docker con…   1933                 [OK]
richarvey/nginx-php-fpm            Container running Nginx + PHP-FPM capable of…   798                  [OK]
linuxserver/nginx                  An Nginx container, brought to you by LinuxS…   138                  
jc21/nginx-proxy-manager           Docker container for managing Nginx proxy ho…   127                  
tiangolo/nginx-rtmp                Docker image with Nginx using the nginx-rtmp…   108                  [OK]
bitnami/nginx                      Bitnami nginx Docker Image                      93                   [OK]
alfg/nginx-rtmp                    NGINX, nginx-rtmp-module and FFmpeg from sou…   86                   [OK]
jlesage/nginx-proxy-manager        Docker container for Nginx Proxy Manager        77                   [OK]
nginxdemos/hello                   NGINX webserver that serves a simple page co…   65                   [OK]
nginx/nginx-ingress                NGINX Ingress Controller for Kubernetes         46                   
privatebin/nginx-fpm-alpine        PrivateBin running on an Nginx, php-fpm & Al…   44                   [OK]
nginxinc/nginx-unprivileged        Unprivileged NGINX Dockerfiles                  27                   
schmunk42/nginx-redirect           A very simple container to redirect HTTP tra…   19                   [OK]
staticfloat/nginx-certbot          Opinionated setup for automatic TLS certs lo…   16                   [OK]
nginx/nginx-prometheus-exporter    NGINX Prometheus Exporter                       15                   
centos/nginx-112-centos7           Platform for running nginx 1.12 or building …   15                   
raulr/nginx-wordpress              Nginx front-end for the official wordpress:f…   13                   [OK]
centos/nginx-18-centos7            Platform for running nginx 1.8 or building n…   13                   
mailu/nginx                        Mailu nginx frontend                            8                    [OK]
sophos/nginx-vts-exporter          Simple server that scrapes Nginx vts stats a…   7                    [OK]
bitnami/nginx-ingress-controller   Bitnami Docker Image for NGINX Ingress Contr…   7                    [OK]
bitwarden/nginx                    The Bitwarden nginx web server acting as a r…   7                    
ansibleplaybookbundle/nginx-apb    An APB to deploy NGINX                          1                    [OK]
wodby/nginx                        Generic nginx                                   1                    [OK]
  • Docker pull:从镜像仓库中拉取或者更新指定镜像
    • 语法:
    docker pull [OPTIONS] NAME[:TAG|@DIGEST]
    
    • 实例:
[root@localhost ~]# docker pull nginx:stable  ## 拉取nginx的稳定版本到本地
stable: Pulling from library/nginx
6ec7b7d162b2: Already exists 
43876acb2da3: Pull complete 
7a79edd1e27b: Pull complete 
eea03077c87e: Pull complete 
eba7631b45c5: Pull complete 
Digest: sha256:2eea9f5d6fff078ad6cc6c961ab11b8314efd91fb8480b5d054c7057a619e0c3
Status: Downloaded newer image for nginx:stable
docker.io/library/nginx:stable
  • Docker images:列出本地镜像
    • 语法:
    docker images [OPTIONS] [REPOSITORY[:TAG]]
    
    • 实例:
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
httpd        latest    dd85cdbb9987   3 weeks ago   138MB
nginx        stable    05f64a802c26   3 weeks ago   133MB

容器操作相关的命令

  • Docker create:创建一个新的容器但不启动它
    • 语法:
    docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
    
    • 实例:
[root@localhost ~]# docker create httpd
705e89e8ebab019be4336b5af19eb0683527185aa163f6afb443b7db345cd47e
[root@localhost ~]# docker ps -a   ## 状态为created
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS    PORTS     NAMES
705e89e8ebab   httpd     "httpd-foreground"   2 minutes ago   Created             epic_lederberg
  • Docker start/stop/restart:开始/停止/重启容器
    • 语法:
    docker start/stop/restart [OPTIONS] CONTAINER [CONTAINER...]
    
    • 实例:
## 开始httpd容器
[root@localhost ~]# docker start 705e89e8ebab
705e89e8ebab
## 正常访问
[root@localhost ~]# curl 172.17.0.2
<html><body><h1>It works!</h1></body></html>
## 查看容器状态为up
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS          PORTS     NAMES
705e89e8ebab   httpd     "httpd-foreground"   5 minutes ago   Up 31 seconds   80/tcp    epic_lederberg

## 停止此前开启的容器
[root@localhost ~]# docker stop 705e89e8ebab
705e89e8ebab

[root@localhost ~]# docker ps -a  ## 状态变为exited
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS                     PORTS     NAMES
705e89e8ebab   httpd     "httpd-foreground"   7 minutes ago   Exited (0) 6 seconds ago             epic_lederberg

## 重新启动容器
[root@localhost ~]# docker restart 705e89e8ebab
705e89e8ebab
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS         PORTS     NAMES
705e89e8ebab   httpd     "httpd-foreground"   9 minutes ago   Up 5 seconds   80/tcp    epic_lederberg
  • Docker run:创建一个新的容器并运行一个命令
    • 语法:
    docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
    常用参数:
    -d: 后台运行容器,并返回容器ID;
    -i: 以交互模式运行容器,通常与 -t 同时使用;
    -t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
    
    • 实例:
[root@localhost ~]# docker run -d nginx:stable ##后台运行nginx容器
a47bb5d9f44daddb1ea5719de7a2b8b26feb6a09b15620de5bc608e4e3de8237
## 访问nginx容器
[root@localhost ~]# curl 172.17.0.3
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
  • Docker ps:列出容器
    • 语法:
    docker ps [OPTIONS]
    常用参数:
    -a :显示所有的容器,包括未运行的。
    -l :显示最近创建的容器。
    -q :静默模式,只显示容器编号。
    
    • 实例:
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS          PORTS     NAMES
705e89e8ebab   httpd     "httpd-foreground"   24 minutes ago   Up 15 minutes   80/tcp    epic_lederberg

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                        PORTS     NAMES
aca09d0142b8   nginx:stable   "/docker-entrypoint.…"   3 minutes ago    Exited (0) 3 minutes ago                charming_thompson
e5f2a0f99122   nginx:stable   "/docker-entrypoint.…"   5 minutes ago    Exited (0) 4 minutes ago                friendly_heyrovsky
edf3aafd9a9f   nginx:stable   "/docker-entrypoint.…"   10 minutes ago   Exited (127) 10 minutes ago             laughing_albattani
705e89e8ebab   httpd          "httpd-foreground"       24 minutes ago   Up 15 minutes                 80/tcp    epic_lederberg

容器的7种状态

状态名 含义
created 已创建
restarting 重启中
running 运行中
removing 迁移中
paused 暂停
exited 停止
dead 死亡
  • Docker logs:获取容器的日志
    • 语法:
    docker logs [OPTIONS] CONTAINER
    常用参数:
    --tail :仅列出最新N条容器日志
    
    • 实例
[root@localhost ~]# docker logs 705e89e8ebab --tail 5
[Mon Jan 04 06:50:32.141881 2021] [mpm_event:notice] [pid 1:tid 140353423152256] AH00492: caught SIGWINCH, shutting down gracefully
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Mon Jan 04 06:52:22.997484 2021] [mpm_event:notice] [pid 1:tid 140406613255296] AH00489: Apache/2.4.46 (Unix) configured -- resuming normal operations
[Mon Jan 04 06:52:22.997600 2021] [core:notice] [pid 1:tid 140406613255296] AH00094: Command line: 'httpd -D FOREGROUND'

  • Docker kill:杀掉一个运行种的容器
    • 语法:
    docker kill [OPTIONS] CONTAINER [CONTAINER...]
    
    • 实例:
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS          PORTS     NAMES
705e89e8ebab   httpd     "httpd-foreground"   33 minutes ago   Up 24 minutes   80/tcp    epic_lederberg

[root@localhost ~]# docker kill 705e89e8ebab
705e89e8ebab

[root@localhost ~]# docker ps  ## 已杀掉的容器状态为退出
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

  • Docker rm:删除一个或多个容器
    • 语法:
    docker rm [OPTIONS] CONTAINER [CONTAINER...]
    常用参数:
    -f :通过 SIGKILL 信号强制删除一个运行中的容器。
    -l :移除容器间的网络连接,而非容器本身。
    
    • 实例:
## 删除在运行中的容器
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS              PORTS     NAMES
705e89e8ebab   httpd     "httpd-foreground"   39 minutes ago   Up About a minute   80/tcp    epic_lederberg
[root@localhost ~]# docker rm -f 705e89e8ebab
705e89e8ebab

## 删除所有已经停止的容器
[root@localhost ~]# docker rm `docker ps -a -q`
e5f2a0f99122
edf3aafd9a9f
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

Docker状态/查询相关

  • Docker info:显示 Docker 系统信息,包括镜像和容器数
    • 语法:
    docker info [OPTIONS]
    
    • 实例:
[root@localhost ~]# docker info
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Build with BuildKit (Docker Inc., v0.5.0-docker)
......
Server:
  • Docker inspect:获取容器/镜像的元数据
    • 语法:
    docker inspect [OPTIONS] NAME|ID [NAME|ID...]
    
    • 实例:
## 查询httpd镜像的元数据
[root@localhost ~]# docker inspect httpd
[
    {
        "Id": "sha256:dd85cdbb99877b73f0de2053f225af590ab188d79469eebdb23ec2d26d0d10e8",
        "RepoTags": [
            "httpd:latest"
        ],
        "RepoDigests": [
            "httpd@sha256:a3a2886ec250194804974932eaf4a4ba2b77c4e7d551ddb63b01068bf70f4120"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2020-12-11T12:20:05.280213478Z",
        "Container": "cea1cf0dc1cf9e5b07e0412d57dea4c8bdf640ad6739a97aa6b27ded003ae059",
        "ContainerConfig": {
            "Hostname": "cea1cf0dc1cf",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
......
## 查询指定容器的元数据
[root@localhost ~]# docker inspect 0e412ad2ccfc
......
"Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }

docker命令图表

posted @ 2021-01-03 22:36  阿不思布丁  阅读(745)  评论(0编辑  收藏  举报