docker

docker


1.docker发展史

2008 年,Solomon Hykes 为了建立一个与编程语言无关的平台即服务(Platform-as-aService,PaaS)产品,创立了 dotCloud 公司。
Docker 早期的版本只是在简单封装 LXC( Linux 容器技术) 以及联合文件系统之上多加了点东西,但往后无论是发展还是被接受的速度都快得惊人。
Docker 1.0 于 2014 年 6 月发布。Docker 1.0 代表着稳定性与可靠性的飞跃——它声称已经“生产就绪”,虽然在这之前就已经有一些公司(如 Spotify 和百度)正式投入使用。同时,Docker 推出了一个名为 Docker Hub 的公共容器仓库,这标志着 Docker 从一个单纯的容器引擎开始转变为一个完整的平台。
2014 年 12 月举行的 DockerConEU 上, Docker Swarm 与 Docker Machine 同 时 面 世。Docker Swarm 是一个 Docker 集群管理工具,而 Docker Machine 是个部署 Docker 主机的命令行工具。这表明 Docker 的意图不仅仅是提供 Docker 引擎,而是提供一个完整且综合的容器运行方案。
OCI :【The Open Container Initiative】开放容器计划,是一个旨在对容器基础架构中的基础组件(如镜像格式与容器运行时)进行标准化的管理委员会。
Moby projetc:Docker开源项目,Moby 项目的目标是基于开源的方式,发展成为 Docker 上游,并将 Docker 拆分为更多的模块化组件。
Docker 1.8 版本引入了“内容信任”(content trust)特性,能够核实 Docker 镜像的完整性和发布者身份。对于建立基于 Docker 仓库镜像的可信工作流程,“内容信任”是个很重要的构成部分。

2.docker存储驱动

1.AUFS

AUFS(AnotherUnionFS)是一种Union FS,是文件级的存储驱动。AUFS是一个能透明覆盖一个或多个现有文件系统的层状文件系统,把多层合并成文件系统的单层表示。简单来说就是支持将不同目录挂载到同一个虚拟文件系统下的文件系统。这种文件系统可以一层一层地叠加修改文件。无论底下有多少层都是只读的,只有最上层的文件系统是可写的。当需要修改一个文件时,AUFS创建该文件的一个副本,使用CoW将文件从只读层复制到可写层进行修改,结果也保存在可写层。AUFS文件系统有3万行代码,但是ext4文件系统就只有4~5千行,AUFS想要合并进内核代码中,Linux觉得太臃肿了所以拒绝了他。在Docker中,底下的只读层就是image,可写层就是Container。结构如下:

2.OverlayFS

Overlay是Linux内核3.18后支持的,也是一种Union FS,和AUFS的多层不同的是Overlay只有两层:一个upper文件系统和一个lower文件系统,分别代表Docker的镜像层和容器层。当需要修改一个文件时,使用CoW将文件从只读的lower复制到可写的upper进行修改,结果也保存在upper层。在Docker中,底下的只读层就是image,可写层就是Container。目前最新的OverlayFS为Overlay2。

3.Devicemapper

Device mapper是Linux内核2.6.9后支持的,提供的一种从逻辑设备到物理设备的映射框架机制,在该机制下,用户可以很方便的根据自己的需要制定实现存储资源的管理策略。前面讲的AUFS和OverlayFS都是文件级存储,而Device mapper是块级存储,所有的操作都是直接对块进行操作,而不是文件。Device mapper驱动会先在块设备上创建一个资源池,然后在资源池上创建一个带有文件系统的基本设备,所有镜像都是这个基本设备的快照,而容器则是镜像的快照。所以在容器里看到文件系统是资源池上基本设备的文件系统的快照,并不有为容器分配空间。当要写入一个新文件时,在容器的镜像内为其分配新的块并写入数据,这个叫用时分配。当要修改已有文件时,再使用CoW为容器快照分配块空间,将要修改的数据复制到在容器快照中新的块里再进行修改。Device mapper 驱动默认会创建一个100G的文件包含镜像和容器。每一个容器被限制在10G大小的卷内

4.Btrfs

Btrfs被称为下一代写时复制文件系统,并入Linux内核,也是文件级级存储,但可以像Device mapper一直接操作底层设备。Btrfs把文件系统的一部分配置为一个完整的子文件系统,称之为subvolume 。那么采用 subvolume,一个大的文件系统可以被划分为多个子文件系统,这些子文件系统共享底层的设备空间,在需要磁盘空间时便从底层设备中分配,类似应用程序调用 malloc()分配内存一样。为了灵活利用设备空间,Btrfs 将磁盘空间划分为多个chunk 。每个chunk可以使用不同的磁盘空间分配策略。比如某些chunk只存放metadata,某些chunk只存放数据。这种模型有很多优点,比如Btrfs支持动态添加设备。用户在系统中增加新的磁盘之后,可以使用Btrfs的命令将该设备添加到文件系统中。Btrfs把一个大的文件系统当成一个资源池,配置成多个完整的子文件系统,还可以往资源池里加新的子文件系统,而基础镜像则是子文件系统的快照,每个子镜像和容器都有自己的快照,这些快照则都是subvolume的快照

3.docker基本用法

//搜索镜像
[root@hwf ~]# docker search httpd
NAME                                 DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
httpd                                The Apache HTTP Server Project                  4106      [OK]       
centos/httpd-24-centos7              Platform for running Apache httpd 2.4 or bui…   44                   
centos/httpd                                                                         35                   [OK]
clearlinux/httpd                     httpd HyperText Transfer Protocol (HTTP) ser…   2                    
hypoport/httpd-cgi                   httpd-cgi                                       2                    [OK]
solsson/httpd-openidc                mod_auth_openidc on official httpd image, ve…   2                    [OK]
dockerpinata/httpd                                                                   1                    
nnasaki/httpd-ssi                    SSI enabled Apache 2.4 on Alpine Linux          1                    
lead4good/httpd-fpm                  httpd server which connects via fcgi proxy h…   1                    [OK]
inanimate/httpd-ssl                  A play container with httpd, ssl enabled, an…   1                    [OK]
publici/httpd                        httpd:latest                                    1                    [OK]
dariko/httpd-rproxy-ldap             Apache httpd reverse proxy with LDAP authent…   1                    [OK]
manageiq/httpd                       Container with httpd, built on CentOS for Ma…   1                    [OK]
centos/httpd-24-centos8                                                              1                    
manasip/httpd                                                                        0                    
amd64/httpd                          The Apache HTTP Server Project                  0                    
patrickha/httpd-err                                                                  0                    
manageiq/httpd_configmap_generator   Httpd Configmap Generator                       0                    [OK]
paketobuildpacks/httpd                                                               0                    
httpdss/archerysec                   ArcherySec repository                           0                    [OK]
httpdocker/kubia                                                                     0                    
e2eteam/httpd                                                                        0                    
sandeep1988/httpd-new                httpd-new                                       0                    
19022021/httpd-connection_test       This httpd image will test the connectivity …   0                    
sherazahmedvaival/httpd-php-fpm74                                                    0                    

//拉取镜像
[root@hwf ~]# docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
a2abf6c4d29d: Pull complete 
dcc4698797c8: Pull complete 
41c22baa66ec: Pull complete 
67283bbdd4a0: Pull complete 
d982c879c57e: Pull complete 
Digest: sha256:0954cc1af252d824860b2c5dc0a10720af2b7a3d3435581ca788dff8480c7b32
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest
[root@hwf ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
httpd        latest    dabbfbe0c57b   7 months ago    144MB
centos       latest    5d0da3dc9764   10 months ago   231MB

//列出有哪些镜像
[root@hwf ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
httpd        latest    dabbfbe0c57b   7 months ago    144MB
centos       latest    5d0da3dc9764   10 months ago   231MB

//创建容器
[root@hwf ~]# docker create --name web httpd	//创建一个以httpd镜像的名为web容器
0340327ea4998ed7f937cba53b1d2efe45a81de40d313eb5be124cb76f0d5581
[root@hwf ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS    PORTS     NAMES
0340327ea499   httpd     "httpd-foreground"   4 seconds ago   Created             web

//开启容器
[root@hwf ~]# docker start web
web
[root@hwf ~]# docker ps			//查看有哪些开启的容器
CONTAINER ID   IMAGE     COMMAND              CREATED              STATUS         PORTS     NAMES
0340327ea499   httpd     "httpd-foreground"   About a minute ago   Up 4 seconds   80/tcp    web

//在创建容器的时候就运行
[root@hwf ~]# docker run --name centos01 -it centos /bin/bash
[root@5005cf999bdc /]# ls
bin  etc   lib	  lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr
[root@5005cf999bdc /]# exit

//列出有哪些运行的容器
[root@hwf ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS         PORTS     NAMES
0340327ea499   httpd     "httpd-foreground"   9 minutes ago   Up 8 minutes   80/tcp    web

//查看容器日志
[root@hwf ~]# docker logs web 
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
[Thu Aug 04 12:48:51.839408 2022] [mpm_event:notice] [pid 1:tid 140279670541632] AH00489: Apache/2.4.52 (Unix) configured -- resuming normal operations
[Thu Aug 04 12:48:51.839530 2022] [core:notice] [pid 1:tid 140279670541632] AH00094: Command line: 'httpd -D FOREGROUND'

//重启容器
[root@hwf ~]# docker restart web
web
[root@hwf ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS                     PORTS     NAMES
5005cf999bdc   centos    "/bin/bash"          2 minutes ago    Exited (0) 2 minutes ago             centos01
0340327ea499   httpd     "httpd-foreground"   10 minutes ago   Up 6 seconds               80/tcp    web

//停止容器
[root@hwf ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS          PORTS     NAMES
0340327ea499   httpd     "httpd-foreground"   11 minutes ago   Up 35 seconds   80/tcp    web
[root@hwf ~]# docker stop web 
web
[root@hwf ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

//强制终止容器
[root@hwf ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS         PORTS     NAMES
0340327ea499   httpd     "httpd-foreground"   12 minutes ago   Up 7 seconds   80/tcp    web
[root@hwf ~]# docker kill web
web
[root@hwf ~]# docker ps -a			//(137)显示就是异常退出
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS                       PORTS     NAMES
5005cf999bdc   centos    "/bin/bash"          4 minutes ago    Exited (0) 3 minutes ago               centos01
0340327ea499   httpd     "httpd-foreground"   12 minutes ago   Exited (137) 5 seconds ago             web

//删除容器
[root@hwf ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS                        PORTS     NAMES
5005cf999bdc   centos    "/bin/bash"          4 minutes ago    Exited (0) 4 minutes ago                centos01
0340327ea499   httpd     "httpd-foreground"   13 minutes ago   Exited (137) 50 seconds ago             web
[root@hwf ~]# docker rm centos01 
centos01
[root@hwf ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS                        PORTS     NAMES
0340327ea499   httpd     "httpd-foreground"   13 minutes ago   Exited (137) 57 seconds ago             web

//显示系统范围的信息
[root@hwf ~]# docker info 
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Docker Buildx (Docker Inc., v0.8.2-docker)
  scan: Docker Scan (Docker Inc., v0.17.0)

Server:
 Containers: 1
  Running: 1
  Paused: 0
  Stopped: 0
 Images: 2
 Server Version: 20.10.17
 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: runc io.containerd.runc.v2 io.containerd.runtime.v1.linux
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 10c12954828e7c7c9b6e0ea9b0c02b01407d3ae1
 runc version: v1.1.2-0-ga916309
 init version: de40ad0
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 4.18.0-348.el8.x86_64
 Operating System: CentOS Linux 8
 OSType: linux
 Architecture: x86_64
 CPUs: 4
 Total Memory: 3.623GiB
 Name: hwf
 ID: GB2G:GSXY:QJML:XUF2:ZDIR:QYQU:ZYOU:JK5I:L23J:OTUE:AQ42:2VWM
 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
 Registry Mirrors:
  https://2kz7i887.mirror.aliyuncs.com/
 Live Restore Enabled: false
 
//返回有关 Docker 对象的低级信息
[root@hwf ~]# docker inspect dabbfbe0c57b
[
    {
        "Id": "sha256:dabbfbe0c57b6e5cd4bc089818d3f664acfad496dc741c9a501e72d15e803b34",
        "RepoTags": [
            "httpd:latest"
        ],
        "RepoDigests": [
            "httpd@sha256:0954cc1af252d824860b2c5dc0a10720af2b7a3d3435581ca788dff8480c7b32"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2021-12-21T01:36:12.729599368Z",
        "Container": "2e3825874c832964f871068b149d74ac708e0968257683b2177adc127faf9b4e",
        "ContainerConfig": {
            "Hostname": "2e3825874c83",

//docker exec		使用exec命令打开一个shell进入到容器中,使用exit退出,容器也不会关闭
[root@hwf ~]# docker exec -it web /bin/bash
root@0340327ea499:/usr/local/apache2# exit
exit
[root@hwf ~]# 
9b4e",
        "ContainerConfig": {
            "Hostname": "2e3825874c83",

//docker exec		使用exec命令打开一个shell进入到容器中,使用exit退出,容器也不会关闭
[root@hwf ~]# docker exec -it web /bin/bash
root@0340327ea499:/usr/local/apache2# exit
exit
[root@hwf ~]# 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1we11

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值