Docker基本原理

目录

前言

一、Docker 概述

1.虚拟化三种模式

2.虚拟化功能

3.三个层面

4.Docker三要素

二、Docker容器概述

1.底层原理

2.Docker和虚拟化的区别

3.使用docker有什么意义

4.docker的应用场景

5.Docker引擎( Docker Engine)

三、部署20版Docker

1.关闭防火墙策略

2.安装依赖包

3.安装docker-ce社区版

4.网络优化

5.相关命令–查看

四、Docker镜像操作

1.运行镜像

2.镜像工作流程

3.获取镜像

4.查看镜像列表

5.获取镜像信息

6.添加镜像标签

7.删除镜像

8.镜像导出/导入

五、容器操作

1.查询容器

2.创建容器

3.启动容器

4.停止容器

5.进入/退出容器

6.容器的导入/导出

7.删除容器


前言

Docker 是在 Linux 容器里运行应用的一种开源工具,是一种轻量级的虚拟机。把容积化技术做成了标准化平台。

一、Docker 概述

Dcoker是基于容器技术的轻量级虛拟化解决方案,docker是由容器引擎,把linux的cgroup、namespaces等容器底层技术进行完美的封装、并抽象为用户提供创建和管理容器的便捷界面(命令行Cli、api等) C/s

1.虚拟化三种模式

全虚拟化(50%)

半虚拟化(软、硬件结合的方式)

直通(以全硬件的方式实现虚拟化的功能)

2.虚拟化功能

在一个操作系统内,模拟多个操作系统

以软件的方式模拟物理设备的功能

3.三个层面

操作系统层

抽象层

内核层

4.Docker三要素

镜像:模板。组资源集合,包含了应用程序软件包、应用程序相关的依赖包、运行应用程序所需要的基础环境(泛指操作系统环境),可以理解为容器的模板

容器:基于镜像的一种运行时状态

仓库:存放image镜像模板;仓库分类: 公共仓库一》docker hub,私有仓库registry harbor

二、Docker容器概述

1.底层原理

(1)名称空间( Namespaces):提供容器的隔离工作区的技术

容器完美的实现了6个名称空问隔离(namespace资源隔离-用容器化技术封装)

(2)控制组(Control groups):资源管理功能,将应用程序限制为一组特定的资源

控制组允许Docker Engine将可用的硬件资源共享给容器,并有选择地实施限制和约束

mount文件系统,挂载点
user操作进程的用户和用户组
pid进程编号
uts主机名和主机域
ipc信号量、消息队列、共享内存(不同的应用调用内存资源的时候应该使用不同的内存空间)
net网络设备、网络协议栈、端口等

2.Docker和虚拟化的区别

特性Docker 容器虚拟机虚拟化
启动速度秒级分钟级
运行性能接近原生(直接在内核中运行)10%-20%50%左右损失
磁盘占用50-100MB3-5G
数量成百上千,每个进程可控制一个容器几十个
隔离性进程级别操作系统(更彻底)
操作系统主要支持Linux几乎所有
封装程度只封装目标代码和依赖关系,共享宿主机内核完整的操作系统,与宿主机隔离

3.使用docker有什么意义

相同版本的docker引擎

打包成镜像包,拖到另一个操作系统中(把引擎放在镜像中,带着镜像到处跑)

利用引擎把这个镜像再去运行为之前的相同的一模一样的容器了

镜像——>封装的某一时刻的服务/应用状态

容器——>应用跑起来的状态(正常提供服务的状态—运行时running)

4.docker的应用场景

打包应用程序简单部署。

可脱离底层硬件任意迁移(实现了应用的隔离,将应用拆分并进行解耦)例如:需要把服务器从腾讯云迁移到阿里云,如果采用的是 Docker 容器技术,整个迁移的过程只需要在新的服务器上启动我们需要的容器就可以了。

5.Docker引擎( Docker Engine)

Docker Engine是具有以下主要组件的C/S客户端—服务器应用程序

server端:服务器是一种长期运行的程序,称为守护程序进程(dockerd命令)

CLIENT端: REST API,它指定程序可以用来与守护程序进行通信并指示其操作的接口
通过client客户端传入命令,比如以下:
docker run:运行
docker start:开启
docker rm:删除
与sever端进行交互,控制server端进行应命令的操作

三、部署20版Docker

1.关闭防火墙策略

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# setenforce 0

2.安装依赖包

[root@localhost ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
#device-mapper-persistent-data:存储驱动
#lvm2:控制工具

#设置阿里云镜像源
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
已加载插件:fastestmirror, langpacks
adding repo from: https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo

3.安装docker-ce社区版

[root@localhost ~]# yum install -y docker-ce

[root@localhost ~]# systemctl start docker         #直接开启docker
[root@localhost ~]# systemctl enable docker

[root@localhost ~]# vim /etc/docker/daemon.json    #镜像加速
{
  "registry-mirrors": ["https://zydiol88.mirror.aliyuncs.com"]
}

4.网络优化

[root@localhost ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward=1                       #开启路由转发
[root@localhost ~]# sysctl -p
net.ipv4.ip_forward = 1

[root@localhost ~]# systemctl restart network
[root@localhost ~]# systemctl restart docker
[root@localhost ~]# ifconfig 
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:a3:31:c3:f4  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
...
[root@localhost ~]# docker images          #查询镜像列表
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

5.相关命令–查看

查看docker版本

[root@localhost ~]# docker -v                #查看docker版本
Docker version 20.10.11, build dea9396
[root@localhost ~]# docker version
Client: Docker Engine - Community            #客户端
 Version:           20.10.11                 #引擎
 API version:       1.41                     #引擎版本
 Go version:        go1.16.9                 #go语言版本
 Git commit:        dea9396                  #git工具
 Built:             Thu Nov 18 00:38:53 2021 #创建时间
 OS/Arch:           linux/amd64              #操作系统
 Context:           default                  #连接方式
 Experimental:      true

Server: Docker Engine - Community            #服务端
 Engine:
  Version:          20.10.11
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.9
  Git commit:       847da18
  Built:            Thu Nov 18 00:37:17 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:                                 #容器版本
  Version:          1.4.12
  GitCommit:        7b11cfaabd73bb80907dd23182b9347b4245eb5d
 runc:                                       #运行容器版本
  Version:          1.0.2
  GitCommit:        v1.0.2-0-g52b36a2
 docker-init:                                #初始化
  Version:          0.19.0
  GitCommit:        de40ad0

用于显示 docker 的系统级信息,比如内核,镜像数,容器数等

[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.6.3-docker)
  scan: Docker Scan (Docker Inc., v0.9.0)

Server:                       #服务端
 Containers: 0                #容器
  Running: 0                  #运行个数
  Paused: 0                   #基础个数
  Stopped: 0                  #停止个数
 Images: 0                    #镜像
 Server Version: 20.10.11     #版本
 Storage Driver: overlay2     #存储引擎
  Backing Filesystem: xfs     #文件系统
  Supports d_type: true       #支持的类型
  Native Overlay Diff: true   #外接的连接性文件
  userxattr: false            
 Logging Driver: json-file    #加载驱动
 Cgroup Driver: cgroupfs      
 Cgroup Version: 1            #版本
 Plugins:
  Volume: local               #本地卷
  Network: bridge host ipvlan macvlan null overlay      #插件支持的网络类型
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runtime.v1.linux runc io.containerd.runc.v2  #运行环境
 Default Runtime: runc        #默认的运行时环境:运行时容器
 Init Binary: docker-init     #镜像基础对应的数据
 containerd version: 7b11cfaabd73bb80907dd23182b9347b4245eb5d         #容器版本
 runc version: v1.0.2-0-g52b36a2     #运行时容器
 init version: de40ad0
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 3.10.0-957.el7.x86_64
 Operating System: CentOS Linux 7 (Core)
 OSType: linux
 Architecture: x86_64
 CPUs: 8
 Total Memory: 3.683GiB
 Name: localhost.localdomain
 ID: HJ3R:CJW2:5CGM:FBT7:APLB:OXJS:JBSZ:XOX7:MH7G:AHM2:7ZNB:HPTD
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:              #私有地址网段
  127.0.0.0/8
 Live Restore Enabled: false

WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled

四、Docker镜像操作

1.运行镜像

[root@localhost docker]# docker run hello-world   #run的形式运行hello-world镜像
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:cc15c5b292d8525effc0f89cb299f1804f3a725c8d05e158653a563f15e4f685
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/

2.镜像工作流程

(1)docker client 客户端连接到服务端(服务端是以一个守护进程的形式跑在操作系统里面的)restfulapi 典型的C/S架构

(2)由docker 服务端的守护进程从docker hub 上下载了镜像(ps:服务端会先检查本地系统是否有这个镜像)

(3)服务端创建了一个新的容器,然后从拉去的这个镜像启动了一个容器,容器执行了脚本/可执行程序让我们可以查看/使用(client)

(4)docker 服务端把这些信息流(传递)返回到客户端并展示出来(展示在终端上)

3.获取镜像

docker search 镜像名      (搜索镜像)      
docker pull 镜像:版本号  (获取固定版本镜像)
docker pull 镜像名      (不加版本号默认为获取最新版本)
[root@localhost docker]# docker pull centos:7    #默认下载centos7
7: Pulling from library/centos
2d473b07cdd5: Pull complete 
Digest: sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987
Status: Downloaded newer image for centos:7
docker.io/library/centos:7
[root@localhost docker]# docker pull nginx       #默认下载nginx的最新版
Using default tag: latest
latest: Pulling from library/nginx              
eff15d958d66: Pull complete 
1e5351450a59: Pull complete 
2df63e6ce2be: Pull complete 
9171c7ae368c: Pull complete 
020f975acd28: Pull complete 
266f639b35ad: Pull complete 
Digest: sha256:097c3a0913d7e3a5b01b6c685a60c03632fc7a2b50bc8e35bcaa3691d788226e
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

4.查看镜像列表

[root@localhost docker]# docker images           #查看有几个镜像
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
nginx         latest    ea335eea17ab   7 days ago     141MB
hello-world   latest    feb5d9fea6a5   2 months ago   13.3kB
centos        7         eeb6ee3f44bd   2 months ago   204MB

5.获取镜像信息

docker inspect 镜像ID
docker image ls -q   #只获取镜像的id号

6.添加镜像标签

[root@localhost docker]# docker tag nginx:latest nginx:lnmp
[root@localhost docker]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
nginx         latest    ea335eea17ab   7 days ago     141MB
nginx         lnmp      ea335eea17ab   7 days ago     141MB     #新的标签,info查不到
hello-world   latest    feb5d9fea6a5   2 months ago   13.3kB
centos        7         eeb6ee3f44bd   2 months ago   204MB

7.删除镜像

[root@localhost docker]# docker rmi nginx:lnmp
Untagged: nginx:lnmp
[root@localhost docker]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
nginx         latest    ea335eea17ab   7 days ago     141MB
hello-world   latest    feb5d9fea6a5   2 months ago   13.3kB
centos        7         eeb6ee3f44bd   2 months ago   204MB

docker rmi `docker images -aq`      #删除所有的镜像

8.镜像导出/导入

[root@localhost docker]# cd /opt/           
[root@localhost opt]# docker save -o nginx_v1 nginx:latest   #镜像导出
[root@localhost opt]# ls
containerd  nginx_v1  rh

[root@localhost opt]# docker load < nginx_v1                 #镜像导入
e1bbcf243d0e: Loading layer [==================================================>]  83.88MB/83.88MB
37380c5830fe: Loading layer [==================================================>]  61.99MB/61.99MB
ff4c72779430: Loading layer [==================================================>]  3.072kB/3.072kB
49eeddd2150f: Loading layer [==================================================>]  4.096kB/4.096kB
1e8ad06c81b6: Loading layer [==================================================>]  3.584kB/3.584kB
8525cde30b22: Loading layer [==================================================>]  7.168kB/7.168kB
Loaded image: nginx:latest

五、容器操作

一般分为两种容器种类:交互式和守护式容器

交互式容器:一般用于测试、开发、临时性任务等

守护式容器:一般用来跑服务

1.查询容器

docker ps -a      #查看容器

2.创建容器

格式:docker run [选项] 镜像 [命令] [变量]

-i:让容器的标准输入保持打开

-t:分配一个伪终端

-d:后台守护进程的方式运行

[root@localhost opt]# docker create -it nginx:latest /bin/bash
b7b3fc63703ec1ee322cae249d167741981e5bf92d52aa406d323f46e402389d
[root@localhost opt]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS    PORTS     NAMES
b7b3fc63703e   nginx:latest   "/docker-entrypoint.…"   56 seconds ago   Created             elated_swirles

3.启动容器

[root@localhost opt]# docker start b7b3fc63703e
b7b3fc63703e
[root@localhost opt]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS     NAMES
b7b3fc63703e   nginx:latest   "/docker-entrypoint.…"   2 minutes ago   Up 4 seconds   80/tcp    elated_swirles

启动一次性运行容器

持续性运行浪费资源,那么一次性执行如下操作

docker run centos:7 /usr/bin/bash -c ls /
[root@localhost opt]# docker run centos:7 /usr/bin/bash -c ls /   #-c:传递命令参数
anaconda-post.log
bin
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var

持续后台运行

docker run -d centos:7 /usr/bin/bash -c "while true;do echo hello; done"  #后台运行centos:7的容器
4873059a924b7a9b5ca422457b8ece4c5b24187d6930c374f52c34011419ddad
[root@localhost opt]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                     PORTS     NAMES
4873059a924b   centos:7       "/usr/bin/bash -c 'w…"   45 seconds ago   Up 45 seconds                        kind_kepler
f113fa048b8e   centos:7       "/usr/bin/bash -c ls…"   4 minutes ago    Exited (0) 3 minutes ago             sad_carson
b7b3fc63703e   nginx:latest   "/docker-entrypoint.…"   35 minutes ago   Up 32 minutes              80/tcp    elated_swirles

4.停止容器

docker stop 容器ID				##停止容器
docker stop b054125b9481		##停止容器

5.进入/退出容器

使用exec进入的容器状态一定要是运行状态,否则会报错。

docker exec -it 容器ID /bin/bash
exit    #退出容器

或者使用run:docker run -it nginx:latest /bin/bash

注意:

docker run -it 会创建前台进程,但是会在输入exit后终止进程
docker attach 会通过连接stdin,连接到容器内输入输出流,会在输入exit后终止容器进程
docker exec -it 会连接到容器,可以像SSH已有进入容器内部,进行操作,可以通过exit退出容器,但是不影响容器运行状态

6.容器的导入/导出

#容器导出
docker export 容器ID > 备份文件名

#容器导入(会生成镜像,而不会创建容器)
cat 备份文件名 | docker import - 仓库名:镜像名

7.删除容器

#删除容器
docker rm 容器ID

#强制删除容器(正在运行的容器)
docker rm -f 容器ID

#批量删除容器(正则表达)
docker ps -a | awk '{print "docker rm"$1}' | bash

#删除非up状态下的
docker rm `docker ps -q`

#批量删除exit状态下的容器
for i in `docker ps -a | grep -i exit | awk '{print $1}'`; do docker rm -f $i;done created
  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值