返回总目录页

docker 存储

 

查看系统默认驱动

Storage Driver: overlay2   默认驱动,这里用的centos7系统
  Backing Filesystem: xfs     底层文件系统
 Docker Root Dir: /var/lib/docker  各层数据存放目录
 

[root@mcw1 ~]$ docker info
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Docker Buildx (Docker Inc., v0.7.1-docker)
  scan: Docker Scan (Docker Inc., v0.12.0)

Server:
 Containers: 19
  Running: 8
  Paused: 0
  Stopped: 11
 Images: 6
 Server Version: 20.10.12
 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-693.el7.x86_64
 Operating System: CentOS Linux 7 (Core)
 OSType: linux
 Architecture: x86_64
 CPUs: 1
 Total Memory: 976.3MiB
 Name: mcw1
 ID: XBZE:DN6Z:UQMS:2FCM:YSA4:XZFO:EPFV:LVET:UP3V:OJFK:WLGF:7Q64
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  10.0.0.131:5000
  127.0.0.0/8
 Registry Mirrors:
  https://hub-mirror.c.163.com/
 Live Restore Enabled: false

WARNING: API is accessible on http://0.0.0.0:2375 without encryption.
         Access to the remote API is equivalent to root access on the host. Refer
         to the 'Docker daemon attack surface' section in the documentation for
         more informatio: https://docs.docker.com/go/attack-surface/


[root@mcw1 ~]$ ls  /var/lib/docker/
buildkit  containers  image  network  overlay2  plugins  runtimes  swarm  tmp  trust  volumes

bind mount ,指定挂载源

,将host目录挂载到容器中使用,容器删除,host目录还存在,数据不被删除,可供下一次使用。还可以设置目录在容器中的读写情况,默认读写,可以设置为ro只读,如果容器中已存在文件的目录被挂载,这些文件会被隐藏起来,使用的是host挂载进来的文件

[root@mcw1 ~]$ mkdir htdocs  #创建挂载目录并创建文件
[root@mcw1 ~]$ echo machangwei>./htdocs/index.html

[root@mcw1 ~]$ docker run -d -p 10002:80 --name=mcw2 -v ~/htdocs:/usr/local/apache2/htdocs httpd  #-v挂载目录到容器
6fc2c03fb2150a37b4d96fcb40f2d2f9d3891dfc42eb1326a2b1ad5ff546a156
[root@mcw1 ~]$ curl 127.0.0.1:10002
machangwei
[root@mcw1 ~]$ docker ps |grep mcw2
6fc2c03fb215   httpd     "httpd-foreground"   32 seconds ago   Up 31 seconds   0.0.0.0:10002->80/tcp, :::10002->80/tcp   mcw2
[root@mcw1 ~]$ docker exec -it 6fc /bin/bash
root@6fc2c03fb215:/usr/local/apache2# ls
bin  build  cgi-bin  conf  error  htdocs  icons  include  logs    modules
root@6fc2c03fb215:/usr/local/apache2# cat htdocs/index.html  #查看容器中指定目录下的文件是挂载后的
machangwei
root@6fc2c03fb215:/usr/local/apache2# exit
[root@mcw1 ~]$ docker stop 6fc2 #将容器停止并删除,host中的数据不随之删除
6fc2
[root@mcw1 ~]$ docker rm 6fc2
6fc2
[root@mcw1 ~]$ cat htdocs/index.html 
machangwei

只读验证
[root@mcw1 ~]$ docker run -d -p 10003:80 --name=mcw3 -v ~/htdocs:/usr/local/apache2/htdocs:ro httpd   #添加ro只读,
e736d8c025152cf06bb1077fdac8e5e5e7a0a0a948fc4bb7523821f1be57dd40
[root@mcw1 ~]$ docker exec -it e736 /bin/bash
root@e736d8c02515:/usr/local/apache2# echo mcw >htdocs/index.html  #进入容器,无法修改只读方式挂载进来的数据
bash: htdocs/index.html: Read-only file system
root@e736d8c02515:/usr/local/apache2#  
exit
[root@mcw1 ~]$ curl 127.0.0.1:10003
machangwei
[root@mcw1 ~]$ echo mcw2>>~/htdocs/index.html #在host中修改挂载目录数据。访问容器时,数据也是修改后的host文件中数据
[root@mcw1 ~]$ curl 127.0.0.1:10003
machangwei
mcw2


[root@mcw1 ~]$ docker run -d -p 10004:80 --name=mcw4 -v ~/htdocs/index.html:/usr/local/apache2/htdocs/index.html httpd #还可以只挂载单个文件,不过容器中必须得有这个文件,不然会作为新目录将容器中的文件都隐藏掉
8ccd479b3d57eda42004e27c3c504019a99a32c234a30f7237b9ed47000865bf
[root@mcw1 ~]$ curl 127.0.0.1:10004
machangwei
mcw2
[root@mcw1 ~]$

 

 docker managed volume 不指定挂载源

[root@mcw1 ~]$ docker run -d -p 10005:80 --name=mcw5 -v /usr/local/apache2/htdocs/ httpd 
95d57f9771c36cd1c254daae0ce2b0d3b23a0c3e45686fd8a9f01713e98656e3
[root@mcw1 ~]$ docker inspect 95d5
[
    {
        "Id": "95d57f9771c36cd1c254daae0ce2b0d3b23a0c3e45686fd8a9f01713e98656e3",
        "Created": "2022-01-01T21:34:15.559176855Z",
        "Path": "httpd-foreground",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 4758,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2022-01-01T21:34:16.275229672Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:dabbfbe0c57b6e5cd4bc089818d3f664acfad496dc741c9a501e72d15e803b34",
        "ResolvConfPath": "/var/lib/docker/containers/95d57f9771c36cd1c254daae0ce2b0d3b23a0c3e45686fd8a9f01713e98656e3/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/95d57f9771c36cd1c254daae0ce2b0d3b23a0c3e45686fd8a9f01713e98656e3/hostname",
        "HostsPath": "/var/lib/docker/containers/95d57f9771c36cd1c254daae0ce2b0d3b23a0c3e45686fd8a9f01713e98656e3/hosts",
        "LogPath": "/var/lib/docker/containers/95d57f9771c36cd1c254daae0ce2b0d3b23a0c3e45686fd8a9f01713e98656e3/95d57f9771c36cd1c254daae0ce2b0d3b23a0c3e45686fd8a9f01713e98656e3-json.log",
        "Name": "/mcw5",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {
                "80/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "10005"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "host",
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "KernelMemory": 0,
            "KernelMemoryTCP": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/8e90d3967e3c8ef0a3aa4267c0a2ffc5e0dec17be90e94716b9b5576de8ca762-init/diff:/var/lib/docker/overlay2/3f76496187fef1b5cd536812868752884233fed42fdfd1f606b748fa88001a17/diff:/var/lib/docker/overlay2/961e08da79100b71d0315b987cc723499c9dd022b43a700ef523b4f8e4f59783/diff:/var/lib/docker/overlay2/3daf3db1b8305a52235ed15e0166111ead8d04cef4125dd4cdb112a9361deb2f/diff:/var/lib/docker/overlay2/8b7ec8b3305c70a95021097e8b78d963745e5013159d7d6f50b53d77b4a13b8a/diff:/var/lib/docker/overlay2/fa380ffe836ecbf76429cde75e5ee384fe90f67ac3e4eea9ddb7aaaee7a814c6/diff",
                "MergedDir": "/var/lib/docker/overlay2/8e90d3967e3c8ef0a3aa4267c0a2ffc5e0dec17be90e94716b9b5576de8ca762/merged",
                "UpperDir": "/var/lib/docker/overlay2/8e90d3967e3c8ef0a3aa4267c0a2ffc5e0dec17be90e94716b9b5576de8ca762/diff",
                "WorkDir": "/var/lib/docker/overlay2/8e90d3967e3c8ef0a3aa4267c0a2ffc5e0dec17be90e94716b9b5576de8ca762/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "volume",
                "Name": "480c09c2beab5a9aebc191f2183692f94a82baa037924152e9edb6588371d26c",
                "Source": "/var/lib/docker/volumes/480c09c2beab5a9aebc191f2183692f94a82baa037924152e9edb6588371d26c/_data",
                "Destination": "/usr/local/apache2/htdocs",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
        "Config": {
            "Hostname": "95d57f9771c3",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/apache2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "HTTPD_PREFIX=/usr/local/apache2",
                "HTTPD_VERSION=2.4.52",
                "HTTPD_SHA256=0127f7dc497e9983e9c51474bed75e45607f2f870a7675a86dc90af6d572f5c9",
                "HTTPD_PATCHES="
            ],
            "Cmd": [
                "httpd-foreground"
            ],
            "Image": "httpd",
            "Volumes": {
                "/usr/local/apache2/htdocs/": {}
            },
            "WorkingDir": "/usr/local/apache2",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {},
            "StopSignal": "SIGWINCH"
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "3eadf66aea44697c210ffd6423b3e712c4a7476db82107604496567b90ea9d92",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "80/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "10005"
                    },
                    {
                        "HostIp": "::",
                        "HostPort": "10005"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/3eadf66aea44",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "2ad8de46a50658e84f8b6aafb6dd278c49d5e7886faddda0a643fdf3c410f7ae",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.8",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:08",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "0480c7596189fe6eb000b2b54f808dce3e121179ddea20c6e1900fac8db6757f",
                    "EndpointID": "2ad8de46a50658e84f8b6aafb6dd278c49d5e7886faddda0a643fdf3c410f7ae",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.8",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:08",
                    "DriverOpts": null
                }
            }
        }
    }
]
[root@mcw1 ~]$ 
[root@mcw1 ~]$ docker run -d -p 10005:80 --name=mcw5 -v /usr/local/apache2/htdocs/ httpd 
95d57f9771c36cd1c254daae0ce2b0d3b23a0c3e45686fd8a9f01713e98656e3
[root@mcw1 ~]$ docker inspect 95d5
[
{
 .........       },
        "Mounts": [
            {
                "Type": "volume",
                "Name": "480c09c2beab5a9aebc191f2183692f94a82baa037924152e9edb6588371d26c",
                "Source": "/var/lib/docker/volumes/480c09c2beab5a9aebc191f2183692f94a82baa037924152e9edb6588371d26c/_data",
                "Destination": "/usr/local/apache2/htdocs",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
        "Config": {
.........
    }
]
[root@mcw1 ~]$ 
[root@mcw1 ~]$ ls /var/lib/docker/volumes/480c09c2beab5a9aebc191f2183692f94a82baa037924152e9edb6588371d26c/_data/
index.html
#在这个volumes下随机生成目录,目录名是Name,源是目录下的_data数据目录,如果容器中Destination存在数据,那么就会复制过来,然后挂载到容器中
[root@mcw1 ~]$ curl 127.0.0.1:10005
<html><body><h1>It works!</h1></body></html>
[root@mcw1 ~]$ echo machangwei >/var/lib/docker/volumes/480c09c2beab5a9aebc191f2183692f94a82baa037924152e9edb6588371d26c/_data/index.html 
[root@mcw1 ~]$ curl 127.0.0.1:10005 #直接修改目录文件,就能修改访问到容器的数据
machangwei
[root@mcw1 ~]$ 

[root@mcw1 ~]$ docker volume ls #出来inspect查看volume,还可以用下面命令
DRIVER    VOLUME NAME
local     480c09c2beab5a9aebc191f2183692f94a82baa037924152e9edb6588371d26c

[root@mcw1 ~]$ docker volume inspect 480c09c2beab5a9aebc191f218 #必须得用全的,不能用短id
[]
Error: No such volume: 480c09c2beab5a9aebc191f218
[root@mcw1 ~]$ docker volume ls
DRIVER    VOLUME NAME
local     480c09c2beab5a9aebc191f2183692f94a82baa037924152e9edb6588371d26c
[root@mcw1 ~]$ docker volume inspect 480c09c2beab5a9aebc191f2183692f94a82baa037924152e9edb6588371d26c
[
    {
        "CreatedAt": "2022-01-02T05:34:15+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/480c09c2beab5a9aebc191f2183692f94a82baa037924152e9edb6588371d26c/_data",
        "Name": "480c09c2beab5a9aebc191f2183692f94a82baa037924152e9edb6588371d26c",
        "Options": null,
        "Scope": "local"
    }
]

容器的数据共享

数据共享主要有三种,可以将单个host挂载源,bind mount到多个容器;可以用managed volume,将文件从host直接cp到不指定挂载源的容器中;还可以使用volume container

 

容器与host共享数据,用bind ,或者managed volume方式


如下,可以用managed volume,将文件从host直接cp到不指定挂载源的容器目录中。

[root@mcw1 ~]$ docker run -d -p 10006:80 --name=mcw6 -v /usr/local/apache2/htdocs/ httpd 
8a2facb7dd5c9d67ae5de20a5be00f05647c9734e4b6ee37516dd4f67a3e5c48
[root@mcw1 ~]$ echo "mcw host data">~/htdocs/index.html
[root@mcw1 ~]$ curl 127.0.0.1:10006
<html><body><h1>It works!</h1></body></html>
[root@mcw1 ~]$ docker cp ~/htdocs/index.html 8a2f:/usr/local/apache2/htdocs/
[root@mcw1 ~]$ curl 127.0.0.1:10006
mcw host data

容器之间共享数据,用bind挂载到多个容器

[root@mcw1 ~]$  docker run -d -p 10007:80 --name=mcw7 -v ~/htdocs/:/usr/local/apache2/htdocs/ httpd 
ce8a2f86f553da7c3030996a53f3af628c90c39fd41d3b2b856ad83c96413624
[root@mcw1 ~]$  docker run -d -p 10008:80 --name=mcw8 -v ~/htdocs/:/usr/local/apache2/htdocs/ httpd 
2b38c3fbb35b029495807a0417a618f0e11c92062aba112cc8d7c776fbaf7999
[root@mcw1 ~]$  docker run -d -p 10009:80 --name=mcw9 -v ~/htdocs/:/usr/local/apache2/htdocs/ httpd 
bf07fd7de9830ba1ad0a217a04b3c643af9bc24bcaf2df5d5a8389d6defd7f02
[root@mcw1 ~]$ curl 127.0.0.1:10007
mcw host data
[root@mcw1 ~]$ curl 127.0.0.1:10008
mcw host data
[root@mcw1 ~]$ curl 127.0.0.1:10009
mcw host data

volume container

[root@mcw1 ~]$ docker create --name vc_data \
>                     -v ~/htdocs:/usr/local/apache2/htdocs \
>                     -v /other/useful/tools \
>                     busybox
6feed5d45bf49c846f608544270fa9f4670f41fadae656e5b356bf37d0ef3f7b
[root@mcw1 ~]$ docker inspect vc_data 
[
    {
        "Id": "6feed5d45bf49c846f608544270fa9f4670f41fadae656e5b356bf37d0ef3f7b",
        "Created": "2022-01-01T22:11:33.3294462Z",
        "Path": "sh",
        "Args": [],
        "State": {
            "Status": "created",
            "Running": false,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 0,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "0001-01-01T00:00:00Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:ffe9d497c32414b1c5cdad8178a85602ee72453082da2463f1dede592ac7d5af",
        "ResolvConfPath": "",
        "HostnamePath": "",
        "HostsPath": "",
        "LogPath": "",
        "Name": "/vc_data",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": [
                "/root/htdocs:/usr/local/apache2/htdocs"
            ],
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "host",
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "KernelMemory": 0,
            "KernelMemoryTCP": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/d81b36941eb3370ae6c4aab6e732d496a71b48b39e52d4b3b611b4015bef9130-init/diff:/var/lib/docker/overlay2/117fe33877fe8caa7455904e6ecf1c1eb2e4020f8d54a4c5d27e9cff014aa465/diff",
                "MergedDir": "/var/lib/docker/overlay2/d81b36941eb3370ae6c4aab6e732d496a71b48b39e52d4b3b611b4015bef9130/merged",
                "UpperDir": "/var/lib/docker/overlay2/d81b36941eb3370ae6c4aab6e732d496a71b48b39e52d4b3b611b4015bef9130/diff",
                "WorkDir": "/var/lib/docker/overlay2/d81b36941eb3370ae6c4aab6e732d496a71b48b39e52d4b3b611b4015bef9130/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "volume",
                "Name": "c07497348a37550e0f07c3944e05a3a9915fc7ae13163679c676ab9cb0c00908",
                "Source": "/var/lib/docker/volumes/c07497348a37550e0f07c3944e05a3a9915fc7ae13163679c676ab9cb0c00908/_data",
                "Destination": "/other/useful/tools",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            },
            {
                "Type": "bind",
                "Source": "/root/htdocs",
                "Destination": "/usr/local/apache2/htdocs",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],
        "Config": {
            "Hostname": "6feed5d45bf4",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": true,
            "AttachStderr": true,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "sh"
            ],
            "Image": "busybox",
            "Volumes": {
                "/other/useful/tools": {}
            },
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "",
                    "EndpointID": "",
                    "Gateway": "",
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "",
                    "DriverOpts": null
                }
            }
        }
    }
]
[root@mcw1 ~]$ 

挂载里面有两个挂载
        "Mounts": [
            {
                "Type": "volume",
                "Name": "c07497348a37550e0f07c3944e05a3a9915fc7ae13163679c676ab9cb0c00908",
                "Source": "/var/lib/docker/volumes/c07497348a37550e0f07c3944e05a3a9915fc7ae13163679c676ab9cb0c00908/_data",
                "Destination": "/other/useful/tools",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            },
            {
                "Type": "bind",
                "Source": "/root/htdocs",
                "Destination": "/usr/local/apache2/htdocs",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],
创建三个容器,指定逻辑卷的容器。这样容器中有哪些挂载的文件,多个其它容器使用它后也有这些挂载
[root@mcw1 ~]$  docker run -d -p 10010:80 --name=mcw10 --volumes-from vc_data httpd 
11f761db182bf0f7691f3b3c54e93d67addb2cf30d09ce801e1def8f8b6c4988
[root@mcw1 ~]$  docker run -d -p 10011:80 --name=mcw11 --volumes-from vc_data httpd 
a29fea380748de524248a4e1f735701fd60080b873749ed97c03dd8fda610d9d
[root@mcw1 ~]$  docker run -d -p 10012:80 --name=mcw12 --volumes-from vc_data httpd 
7f4272c2b40b382476460c3c336f25857d7c421fdeb36dd5f383d071270e7b1b
[root@mcw1 ~]$ curl 127.0.0.1:10010
mcw host data
[root@mcw1 ~]$ curl 127.0.0.1:10011
mcw host data
[root@mcw1 ~]$ curl 127.0.0.1:10012
mcw host data
[root@mcw1 ~]$ cat htdocs/index.html 
mcw host data
[root@mcw1 ~]$ 

 data-packed volume container

FROM busybox:latest  #使用这个镜像
ADD htdocs /usr/local/apache2/htdocs #将本地目录htdocs添加为容器这个目录
VOLUME /usr/local/apache2/htdocs #等同于-v,不指定挂载源,让docker自己创建挂载源


[root@mcw1 ~]$ cd mcwdocker/
[root@mcw1 ~/mcwdocker]$ cp -a ../htdocs/ .
[root@mcw1 ~/mcwdocker]$ vim Dockerfile 
[root@mcw1 ~/mcwdocker]$ cat 
Dockerfile  htdocs/     
[root@mcw1 ~/mcwdocker]$ cat Dockerfile  
FROM busybox:latest  #使用这个镜像
ADD htdocs /usr/local/apache2/htdocs #将本地目录htdocs添加为容器这个目录
VOLUME /usr/local/apache2/htdocs #等同于-v,不指定挂载源,让docker自己创建挂载源
[root@mcw1 ~/mcwdocker]$ docker build -t datapacked .  #Dockerfile可以用注释。但是注释不能是放在命令后面
Sending build context to Docker daemon  3.584kB
Error response from daemon: dockerfile parse error line 1: FROM requires either one or three arguments
[root@mcw1 ~/mcwdocker]$ vim Dockerfile 
[root@mcw1 ~/mcwdocker]$ cat Dockerfile 
FROM busybox:latest
ADD htdocs /usr/local/apache2/htdocs
VOLUME /usr/local/apache2/htdocs
[root@mcw1 ~/mcwdocker]$ docker build -t datapacked .
Sending build context to Docker daemon  3.584kB
Step 1/3 : FROM busybox:latest
 ---> ffe9d497c324
Step 2/3 : ADD htdocs /usr/local/apache2/htdocs
 ---> e2b5e7a21788
Step 3/3 : VOLUME /usr/local/apache2/htdocs
 ---> Running in 7103ed392b15
Removing intermediate container 7103ed392b15
 ---> ee835e293230
Successfully built ee835e293230
Successfully tagged datapacked:latest

创建容器并不运行容器
[root@mcw1 ~/mcwdocker]$ docker create --name vc_mcwdata datapacked  #以包含需要挂载数据的镜像创建容器vc_mcwdata,并不需要运行
7a50c2670a7d55b019812907c196b0f302a9446f9653cb1965108ed010438c32
[root@mcw1 ~/mcwdocker]$ docker run -d -p 10013:80 --name mcw13 --volumes-from vc_mcwdata httpd 
7c30a69d0e337caf3114b06ed9e2a0f3dce7a26e5c7801d5422832519a2306d3 #运行容器,让容器的挂载源
#来自于vc_mcwdata容器,其它容器共享这个容器数据。,提高可移植性
[root@mcw1 ~/mcwdocker]$ curl 127.0.0.1:10013
mcw host data
[root@mcw1 ~/mcwdocker]$ docker ps |grep vc_mcwdata
[root@mcw1 ~/mcwdocker]$ docker ps -a|grep vc_mcwdata
7a50c2670a7d   datapacked   "sh"                     3 minutes ago        Created                                                                 vc_mcwdata

 Data Volume  的生命周期管理

1、备份


将本地镜像存到镜像仓库中,本地镜像都保存在host的/registry目录中,定期备份目录。下面这个本地仓库,好像存在点问题,有时间再研究

[root@mcw1 ~/mcwdocker]$ docker run -d -p 5000:5000 -v /myregistry:/var/lib/registry registry:2
98a061dffafcbab97c74375d6e0d59c2d2569494c0a03377c1bea86a4bd4a36f
[root@mcw1 ~/mcwdocker]$ ls /myregistry/
[root@mcw1 ~/mcwdocker]$ docker push datapacked
Using default tag: latest
^C
[root@mcw1 ~/mcwdocker]$ 
[root@mcw1 ~/mcwdocker]$ docker push 10.0.0.131:5000/datapacked
Using default tag: latest
The push refers to repository [10.0.0.131:5000/datapacked]
An image does not exist locally with the tag: 10.0.0.131:5000/datapacked
[root@mcw1 ~/mcwdocker]$ docker images datapacked
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
datapacked   latest    ee835e293230   33 minutes ago   1.24MB
[root@mcw1 ~/mcwdocker]$ docker push 10.0.0.131:5000/datapacked:latest
The push refers to repository [10.0.0.131:5000/datapacked]
An image does not exist locally with the tag: 10.0.0.131:5000/datapacked
[root@mcw1 ~/mcwdocker]$ ls /mcwregistry/
docker
[root@mcw1 ~/mcwdocker]$ ls /mcwregistry/docker/
registry
[root@mcw1 ~/mcwdocker]$ ls /mcwregistry/docker/registry/
v2
[root@mcw1 ~/mcwdocker]$ ls /mcwregistry/docker/registry/v2/
blobs  repositories
[root@mcw1 ~/mcwdocker]$ ls /mcwregistry/docker/registry/v2/repositories/
machangwei
[root@mcw1 ~/mcwdocker]$ ls /mcwregistry/docker/registry/v2/repositories/machangwei/
mcw-httpd01
[root@mcw1 ~/mcwdocker]$ ls /mcwregistry/docker/registry/v2/repositories/machangwei/mcw-httpd01/
_layers  _manifests  _uploads
[root@mcw1 ~/mcwdocker]$ ls /mcwregistry/docker/registry/v2/blobs/
sha256
[root@mcw1 ~/mcwdocker]$ 

2、恢复


数据损坏,直接用之前备份的数据复制到/mytegistry


3、迁移


更新Registry,涉及数据迁移。
(1)docker stop 当前Registry容器
(2)启动新版本容器并mount原有volume
docker run -d -p 5000:5000 -v /myregistry:/var/lib/registry registry:latest

 

4、销毁

 

正在使用的一般是无法删除的
[root@mcw1 ~/mcwdocker]$ docker volume ls
DRIVER    VOLUME NAME
local     7c550c0a1efdc7ab9d5cc4d3c6b9760fab2f8fde9081d7ae177456872afa4c32
local     186f29d1968cd35f00310a5dcda1c5351c0c893d99661fdfcfe4739c68a65867
local     480c09c2beab5a9aebc191f2183692f94a82baa037924152e9edb6588371d26c
local     c07497348a37550e0f07c3944e05a3a9915fc7ae13163679c676ab9cb0c00908
[root@mcw1 ~/mcwdocker]$ docker volume 
create   inspect  ls       prune    rm       
[root@mcw1 ~/mcwdocker]$ docker volume rm 7c550c0a1efdc7ab9d5cc4d3c6b9760fab2f8fde9081d7ae177456872afa4c32
Error response from daemon: remove 7c550c0a1efdc7ab9d5cc4d3c6b9760fab2f8fde9081d7ae177456872afa4c32: volume is in use - [8a2facb7dd5c9d67ae5de20a5be00f05647c9734e4b6ee37516dd4f67a3e5c48] 
[root@mcw1 ~/mcwdocker]$ docker  ps -a|grep 8a2facb  #因为volume有在使用的,所以无法删除
8a2facb7dd5c   httpd        "httpd-foreground"       2 hours ago         Up 2 hours                  0.0.0.0:10006->80/tcp, :::10006->80/tcp     mcw6


批量删除孤儿volume,慎用,删了就回不来了
docker volume rm $(docker volume ls -q)
[root@mcw1 ~/mcwdocker]$ docker  volume ls
DRIVER    VOLUME NAME
local     7c550c0a1efdc7ab9d5cc4d3c6b9760fab2f8fde9081d7ae177456872afa4c32
local     186f29d1968cd35f00310a5dcda1c5351c0c893d99661fdfcfe4739c68a65867
local     480c09c2beab5a9aebc191f2183692f94a82baa037924152e9edb6588371d26c
local     c07497348a37550e0f07c3944e05a3a9915fc7ae13163679c676ab9cb0c00908
[root@mcw1 ~/mcwdocker]$ docker volume rm $(docker volume ls -q)
7c550c0a1efdc7ab9d5cc4d3c6b9760fab2f8fde9081d7ae177456872afa4c32
186f29d1968cd35f00310a5dcda1c5351c0c893d99661fdfcfe4739c68a65867
480c09c2beab5a9aebc191f2183692f94a82baa037924152e9edb6588371d26c
c07497348a37550e0f07c3944e05a3a9915fc7ae13163679c676ab9cb0c00908
[root@mcw1 ~/mcwdocker]$ docker volume ls
DRIVER    VOLUME NAME

volume从创建到删除
[root@mcw1 ~/mcwdocker]$ docker volume ls #没有
DRIVER    VOLUME NAME
[root@mcw1 ~/mcwdocker]$ docker run --name mcw1 -v /test/data busybox #-v未指定host挂载源,创建volume
[root@mcw1 ~/mcwdocker]$ docker volume ls #
DRIVER    VOLUME NAME
local     556abb1e167191ed9aea60a37fbc2a7fc7e93a84b620e63cb13f75d371de24cf
[root@mcw1 ~/mcwdocker]$ docker volume inspect 556abb1e167191ed9aea60a37fbc2a7fc7e93a84b620e63cb13f75d371de24cf
[
    {
        "CreatedAt": "2022-01-02T08:24:36+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/556abb1e167191ed9aea60a37fbc2a7fc7e93a84b620e63cb13f75d371de24cf/_data",
        "Name": "556abb1e167191ed9aea60a37fbc2a7fc7e93a84b620e63cb13f75d371de24cf",
        "Options": null,
        "Scope": "local"
    }
]
[root@mcw1 ~/mcwdocker]$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@mcw1 ~/mcwdocker]$ 
[root@mcw1 ~/mcwdocker]$ 
[root@mcw1 ~/mcwdocker]$ docker volume rm 556abb1e167191ed9aea60a37fbc2a7fc7e93a84b620e63cb13f75d371de24cf
Error response from daemon: remove 556abb1e167191ed9aea60a37fbc2a7fc7e93a84b620e63cb13f75d371de24cf: volume is in use - [005f476f8dd474acd88f07852a026fe54106a8bcc10e8c333431459d5024f6b0]
[root@mcw1 ~/mcwdocker]$ docker rm mcw1   #删除容器未使用-v,是会遗留下来volume,如果确认需要删除,对于
mcw1
[root@mcw1 ~/mcwdocker]$ docker volume ls  #这种孤儿volume,可以docker volume rm删除
DRIVER    VOLUME NAME
local     556abb1e167191ed9aea60a37fbc2a7fc7e93a84b620e63cb13f75d371de24cf
[root@mcw1 ~/mcwdocker]$ docker volume inspect 556abb1e167191ed9aea60a37fbc2a7fc7e93a84b620e63cb13f75d371de24cf
[
    {
        "CreatedAt": "2022-01-02T08:24:36+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/556abb1e167191ed9aea60a37fbc2a7fc7e93a84b620e63cb13f75d371de24cf/_data",
        "Name": "556abb1e167191ed9aea60a37fbc2a7fc7e93a84b620e63cb13f75d371de24cf",
        "Options": null,
        "Scope": "local"
    }
]
[root@mcw1 ~/mcwdocker]$ docker volume  rm 556abb1e167191ed9aea60a37fbc2a7fc7e93a84b620e63cb13f75d371de24cf
556abb1e167191ed9aea60a37fbc2a7fc7e93a84b620e63cb13f75d371de24cf
[root@mcw1 ~/mcwdocker]$ docker volume ls
DRIVER    VOLUME NAME
[root@mcw1 ~/mcwdocker]$ 


指定-v删除volume,这个慎用,可能其它有在用的,回头验证是否就无法删除
[root@mcw1 ~/mcwdocker]$ docker volume  ls
DRIVER    VOLUME NAME
[root@mcw1 ~/mcwdocker]$ docker run -d --name mcw2  -v /test/data httpd
fa0ce00a840f10dc5712ce13448ac418b90f9a92b761a1231c2a9cc1efbc748d
[root@mcw1 ~/mcwdocker]$ docker volume  ls
DRIVER    VOLUME NAME
local     6b1d081d0fcf0bcd0b205ca3473ea37f80c65408b49d6129116c2eebb6b7d429
[root@mcw1 ~/mcwdocker]$ docker stop fa0
fa0
[root@mcw1 ~/mcwdocker]$ docker rm fa0 -v
fa0
[root@mcw1 ~/mcwdocker]$ docker volume  ls
DRIVER    VOLUME NAME
[root@mcw1 ~/mcwdocker]$ 

 

posted @ 2022-01-01 21:20  马昌伟  阅读(935)  评论(0编辑  收藏  举报
博主链接地址:https://www.cnblogs.com/machangwei-8/