美文网首页
六、Docker容器数据卷

六、Docker容器数据卷

作者: 紫荆秋雪_文 | 来源:发表于2020-02-23 15:45 被阅读0次

    一、docker容器数据卷

    • 将运用与运行的环境打包形成容器运行,运行可以伴随着容器,但是我们对数据的要求希望是持久化的
    • 容器之间希望有可能共享数据
    • Docker容器产生的数据,如果不通过docker commit生成新的镜像,使得数据做为镜像的一部分保存下来
    • 当容器删除后,数据自然也就没有了,为了能保存数据在docker中使用卷

    二、docker容器数据卷的作用

    • 卷就是目录或文件,存在于一个或多个容器中,由docker挂载到容器,但不属于联合文件系统,因此能够绕过Union File System提供一些用于持续存储或共享数据的特性
    • 卷的设计目的就是数据的持久化,完全独立于容器的生命周期,因此Docker不会在容器删除时删除其挂载的数据卷
    • 特点
      • 1、数据卷可在容器之间共享或重用数据
      • 2、卷中的更改可以直接生效
      • 3、数据卷中的更改不会包含在镜像的更新中
      • 4、数据卷的生命周期一直持续到没有容器使用它为止

    三、docker容器数据卷实践

    1、使用命令方式

    • 1、docker run -it -v /宿主机绝对路径目录:/容器内目录 镜像名
    docker run -it -v /hostData:/containerData centos:7
    
    Host共享数据文件夹.png Container共享数据卷.png
    • 2、查看数据卷是否挂载成功
    docker inspect 容器ID
    docker inspect 5f016a429e98
    
    [
        {
            "Id": "5f016a429e98d410f841601cd9aeea0ebb66f81f519d31c301d37c20a71006ae",
            "Created": "2020-02-22T08:59:53.187799839Z",
            "Path": "/bin/bash",
            "Args": [],
            "State": {
                "Status": "running",
                "Running": true,
                "Paused": false,
                "Restarting": false,
                "OOMKilled": false,
                "Dead": false,
                "Pid": 10627,
                "ExitCode": 0,
                "Error": "",
                "StartedAt": "2020-02-22T08:59:53.499628335Z",
                "FinishedAt": "0001-01-01T00:00:00Z"
            },
            "Image": "sha256:5e35e350aded98340bc8fcb0ba392d809c807bc3eb5c618d4a0674d98d88bccd",
            "ResolvConfPath": "/var/lib/docker/containers/5f016a429e98d410f841601cd9aeea0ebb66f81f519d31c301d37c20a71006ae/resolv.conf",
            "HostnamePath": "/var/lib/docker/containers/5f016a429e98d410f841601cd9aeea0ebb66f81f519d31c301d37c20a71006ae/hostname",
            "HostsPath": "/var/lib/docker/containers/5f016a429e98d410f841601cd9aeea0ebb66f81f519d31c301d37c20a71006ae/hosts",
            "LogPath": "/var/lib/docker/containers/5f016a429e98d410f841601cd9aeea0ebb66f81f519d31c301d37c20a71006ae/5f016a429e98d410f841601cd9aeea0ebb66f81f519d31c301d37c20a71006ae-json.log",
            "Name": "/sharp_bell",
            "RestartCount": 0,
            "Driver": "overlay2",
            "Platform": "linux",
            "MountLabel": "",
            "ProcessLabel": "",
            "AppArmorProfile": "",
            "ExecIDs": null,
            "HostConfig": {
                "Binds": [
                    "/hostData:/containerData"
                ],
                "ContainerIDFile": "",
                "LogConfig": {
                    "Type": "json-file",
                    "Config": {}
                },
                "NetworkMode": "default",
                "PortBindings": {},
                "RestartPolicy": {
                    "Name": "no",
                    "MaximumRetryCount": 0
                },
                "AutoRemove": false,
                "VolumeDriver": "",
                "VolumesFrom": null,
                "CapAdd": null,
                "CapDrop": null,
                "Dns": [],
                "DnsOptions": [],
                "DnsSearch": [],
                "ExtraHosts": null,
                "GroupAdd": null,
                "IpcMode": "shareable",
                "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,
                "DiskQuota": 0,
                "KernelMemory": 0,
                "MemoryReservation": 0,
                "MemorySwap": 0,
                "MemorySwappiness": null,
                "OomKillDisable": false,
                "PidsLimit": 0,
                "Ulimits": null,
                "CpuCount": 0,
                "CpuPercent": 0,
                "IOMaximumIOps": 0,
                "IOMaximumBandwidth": 0
            },
            "GraphDriver": {
                "Data": {
                    "LowerDir": "/var/lib/docker/overlay2/f316635ab02aef6cf0ffc7d89f01078c979baa92c6c92df392cfc3700a3a4a87-init/diff:/var/lib/docker/overlay2/6dfbb9efda33e6de2d8f17d045996576f0261db0476b94926ad474ab477d1de4/diff",
                    "MergedDir": "/var/lib/docker/overlay2/f316635ab02aef6cf0ffc7d89f01078c979baa92c6c92df392cfc3700a3a4a87/merged",
                    "UpperDir": "/var/lib/docker/overlay2/f316635ab02aef6cf0ffc7d89f01078c979baa92c6c92df392cfc3700a3a4a87/diff",
                    "WorkDir": "/var/lib/docker/overlay2/f316635ab02aef6cf0ffc7d89f01078c979baa92c6c92df392cfc3700a3a4a87/work"
                },
                "Name": "overlay2"
            },
            "Mounts": [
                {
                    "Type": "bind",
                    "Source": "/hostData",
                    "Destination": "/containerData",
                    "Mode": "",
                    "RW": true,
                    "Propagation": "rprivate"
                }
            ],
            "Config": {
                "Hostname": "5f016a429e98",
                "Domainname": "",
                "User": "",
                "AttachStdin": true,
                "AttachStdout": true,
                "AttachStderr": true,
                "Tty": true,
                "OpenStdin": true,
                "StdinOnce": true,
                "Env": [
                    "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
                ],
                "Cmd": [
                    "/bin/bash"
                ],
                "ArgsEscaped": true,
                "Image": "centos:7",
                "Volumes": null,
                "WorkingDir": "",
                "Entrypoint": null,
                "OnBuild": null,
                "Labels": {
                    "org.label-schema.build-date": "20191001",
                    "org.label-schema.license": "GPLv2",
                    "org.label-schema.name": "CentOS Base Image",
                    "org.label-schema.schema-version": "1.0",
                    "org.label-schema.vendor": "CentOS"
                }
            },
            "NetworkSettings": {
                "Bridge": "",
                "SandboxID": "f1fcdc37b8faff6e72b40ca1ee86d4e23ccaf4ee8504542ec34e367f5722e96e",
                "HairpinMode": false,
                "LinkLocalIPv6Address": "",
                "LinkLocalIPv6PrefixLen": 0,
                "Ports": {},
                "SandboxKey": "/var/run/docker/netns/f1fcdc37b8fa",
                "SecondaryIPAddresses": null,
                "SecondaryIPv6Addresses": null,
                "EndpointID": "f6fb0e237dc722331ea38e7383b3e707b38addd78518a756b008511131fec5f7",
                "Gateway": "172.17.0.1",
                "GlobalIPv6Address": "",
                "GlobalIPv6PrefixLen": 0,
                "IPAddress": "172.17.0.3",
                "IPPrefixLen": 16,
                "IPv6Gateway": "",
                "MacAddress": "02:42:ac:11:00:03",
                "Networks": {
                    "bridge": {
                        "IPAMConfig": null,
                        "Links": null,
                        "Aliases": null,
                        "NetworkID": "eebbaba16bf28e00b62bd9fe3a8faa5deb1a77cb315353561db856207060ad03",
                        "EndpointID": "f6fb0e237dc722331ea38e7383b3e707b38addd78518a756b008511131fec5f7",
                        "Gateway": "172.17.0.1",
                        "IPAddress": "172.17.0.3",
                        "IPPrefixLen": 16,
                        "IPv6Gateway": "",
                        "GlobalIPv6Address": "",
                        "GlobalIPv6PrefixLen": 0,
                        "MacAddress": "02:42:ac:11:00:03",
                        "DriverOpts": null
                    }
                }
            }
        }
    ]
    

    绑定成功

    "Binds": [
                    "/hostData:/containerData"
                ]
    
    • 3、在主文件夹中创建文件host.txt文件
    [root@localhost hostData]# touch host.txt
    [root@localhost hostData]# ll
    总用量 0
    -rw-r--r--. 1 root root 0 2月  22 17:16 host.txt
    
    • 4、在容器中查看containerData文件夹中是否有 host.txt文件
    [root@5f016a429e98 containerData]# pwd
    /containerData
    [root@5f016a429e98 containerData]# ll
    total 0
    -rw-r--r--. 1 root root 0 Feb 22 09:16 host.txt
    [root@5f016a429e98 containerData]#
    
    • 5、在容器中向host.txt中写入Container input
    • 6、在hostData文件夹下查看host.txt
    [root@localhost hostData]# ll
    总用量 4
    -rw-r--r--. 1 root root 16 2月  22 17:19 host.txt
    [root@localhost hostData]# cat host.txt
    Container input
    [root@localhost hostData]#
    
    • 7、容器停止退出后,主机修改后数据是否同步
    [root@localhost hostData]# vim host.txt
    [root@localhost hostData]# cat host.txt
    Container input
    Host input
    
    • 7.1 进入容器
    # 重启容器
    docker start 5f016a429e98
    # 重新进入容器
    docker attach 5f016a429e98
    
    [root@5f016a429e98 containerData]# cat host.txt
    Container input
    Host input
    

    2、单向共享数据(带权限)

    docker run -it -v /宿主机绝对路径目录:/容器内目录:ro 镜像名
    docker run -it -v /host_dir:/container_dir:ro centos:7
    
    • 2.1 在容器的container_dir文件夹中创建文件,发生报错(Read-only)
    [root@c8be36c89a37 container_dir]# ll
    total 0
    [root@c8be36c89a37 container_dir]# touch container.txt
    touch: cannot touch 'container.txt': Read-only file system
    [root@c8be36c89a37 container_dir]#
    
    • 2.2、在宿主的共享文件夹中创建文件
    [root@localhost host_dir]# touch host.txt
    [root@localhost host_dir]# ll
    总用量 0
    -rw-r--r--. 1 root root 0 2月  22 17:40 host.txt
    [root@localhost host_dir]# vim host.txt
    [root@localhost host_dir]# cat host.txt
    Host input
    [root@localhost host_dir]#
    
    • 2.3、在容器中查看文件
    [root@c8be36c89a37 container_dir]# ll
    total 4
    -rw-r--r--. 1 root root 11 Feb 22 09:41 host.txt
    [root@c8be36c89a37 container_dir]# cat host.txt
    Host input
    [root@c8be36c89a37 container_dir]#
    

    3、DockerFile方式实现容器数据卷

    • 1、在根目录下创建mydocker文件夹
    • 2、在mydocker文件夹下创建Dockerfile文件
    • 3、编辑Dockerfile文件
    FROM centos
    VOLUME ["/dataVolumeContainer1", "/dataVolumeContainer1"]
    CMD echo "Finished --- Success"
    CMD /bin/bash
    
    • 4、Dockerfile文件不同于-v命令的是,Dockerfile中没有指定与容器文件夹对应的宿主文件夹

      • 出于可移植和分享的考虑,用-v 主机目录:容器目录这种方法不能够直接在Dockerfile中实现。
        由于宿主机目录是依赖于特定宿主机的,并不能够保证在所有的宿主机上都存在这样的特定目录
    • 5、执行构建镜像命令

    docker build -t centos:1.0 .
    
    • 6、构建成功后查询
    [root@localhost mydocker]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    centos              1.0                 f714bb5ea228        16 seconds ago      237MB
    
    • 7、运行容器
    [root@localhost mydocker]# docker run -it centos:1.0
    [root@f9b903d68555 /]# ll
    bash: ll: command not found
    [root@f9b903d68555 /]# ls
    bin           dev  home  lib64       media  opt   root  sbin  sys  usr
    dataVolumeContainer1  etc  lib   lost+found  mnt    proc  run   srv   tmp  var
    [root@f9b903d68555 /]#
    
    • 8、查看对应宿主文件夹
    docker inspect f9b903d68555
    
    [
        {
            "Id": "f9b903d68555dff9ccaa5dac214ff4055376fe1fef8c0a2b03c8d1bcaa2d05c4",
            "Created": "2020-02-22T18:24:22.500708125Z",
            "Path": "/bin/sh",
            "Args": [
                "-c",
                "/bin/bash"
            ],
            "State": {
                "Status": "running",
                "Running": true,
                "Paused": false,
                "Restarting": false,
                "OOMKilled": false,
                "Dead": false,
                "Pid": 12091,
                "ExitCode": 0,
                "Error": "",
                "StartedAt": "2020-02-22T18:24:22.765690526Z",
                "FinishedAt": "0001-01-01T00:00:00Z"
            },
            "Image": "sha256:f714bb5ea228aa2d66f3b0e1700d5f3ae85c49cc8e2e7dee4cb20bff1a55de1f",
            "ResolvConfPath": "/var/lib/docker/containers/f9b903d68555dff9ccaa5dac214ff4055376fe1fef8c0a2b03c8d1bcaa2d05c4/resolv.conf",
            "HostnamePath": "/var/lib/docker/containers/f9b903d68555dff9ccaa5dac214ff4055376fe1fef8c0a2b03c8d1bcaa2d05c4/hostname",
            "HostsPath": "/var/lib/docker/containers/f9b903d68555dff9ccaa5dac214ff4055376fe1fef8c0a2b03c8d1bcaa2d05c4/hosts",
            "LogPath": "/var/lib/docker/containers/f9b903d68555dff9ccaa5dac214ff4055376fe1fef8c0a2b03c8d1bcaa2d05c4/f9b903d68555dff9ccaa5dac214ff4055376fe1fef8c0a2b03c8d1bcaa2d05c4-json.log",
            "Name": "/stupefied_blackwell",
            "RestartCount": 0,
            "Driver": "overlay2",
            "Platform": "linux",
            "MountLabel": "",
            "ProcessLabel": "",
            "AppArmorProfile": "",
            "ExecIDs": null,
            "HostConfig": {
                "Binds": null,
                "ContainerIDFile": "",
                "LogConfig": {
                    "Type": "json-file",
                    "Config": {}
                },
                "NetworkMode": "default",
                "PortBindings": {},
                "RestartPolicy": {
                    "Name": "no",
                    "MaximumRetryCount": 0
                },
                "AutoRemove": false,
                "VolumeDriver": "",
                "VolumesFrom": null,
                "CapAdd": null,
                "CapDrop": null,
                "Dns": [],
                "DnsOptions": [],
                "DnsSearch": [],
                "ExtraHosts": null,
                "GroupAdd": null,
                "IpcMode": "shareable",
                "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,
                "DiskQuota": 0,
                "KernelMemory": 0,
                "MemoryReservation": 0,
                "MemorySwap": 0,
                "MemorySwappiness": null,
                "OomKillDisable": false,
                "PidsLimit": 0,
                "Ulimits": null,
                "CpuCount": 0,
                "CpuPercent": 0,
                "IOMaximumIOps": 0,
                "IOMaximumBandwidth": 0
            },
            "GraphDriver": {
                "Data": {
                    "LowerDir": "/var/lib/docker/overlay2/fbba0702d68673a98e00f3184c45ed6c7df405fade2d76b48b3f8d9cb748c9e3-init/diff:/var/lib/docker/overlay2/ecc8e8b4c47a96038eedd2cef738b6747ee788e5b0f360ece2cf9cfd20e47d0f/diff",
                    "MergedDir": "/var/lib/docker/overlay2/fbba0702d68673a98e00f3184c45ed6c7df405fade2d76b48b3f8d9cb748c9e3/merged",
                    "UpperDir": "/var/lib/docker/overlay2/fbba0702d68673a98e00f3184c45ed6c7df405fade2d76b48b3f8d9cb748c9e3/diff",
                    "WorkDir": "/var/lib/docker/overlay2/fbba0702d68673a98e00f3184c45ed6c7df405fade2d76b48b3f8d9cb748c9e3/work"
                },
                "Name": "overlay2"
            },
            "Mounts": [
                {
                    "Type": "volume",
                    "Name": "92f1136825480b227587155797241a8b51d06ee0894624bae488c239111752c3",
                    "Source": "/var/lib/docker/volumes/92f1136825480b227587155797241a8b51d06ee0894624bae488c239111752c3/_data",
                    "Destination": "/dataVolumeContainer1",
                    "Driver": "local",
                    "Mode": "",
                    "RW": true,
                    "Propagation": ""
                }
            ],
            "Config": {
                "Hostname": "f9b903d68555",
                "Domainname": "",
                "User": "",
                "AttachStdin": true,
                "AttachStdout": true,
                "AttachStderr": true,
                "Tty": true,
                "OpenStdin": true,
                "StdinOnce": true,
                "Env": [
                    "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
                ],
                "Cmd": [
                    "/bin/sh",
                    "-c",
                    "/bin/bash"
                ],
                "ArgsEscaped": true,
                "Image": "centos:1.0",
                "Volumes": {
                    "/dataVolumeContainer1": {}
                },
                "WorkingDir": "",
                "Entrypoint": null,
                "OnBuild": null,
                "Labels": {
                    "org.label-schema.build-date": "20200114",
                    "org.label-schema.license": "GPLv2",
                    "org.label-schema.name": "CentOS Base Image",
                    "org.label-schema.schema-version": "1.0",
                    "org.label-schema.vendor": "CentOS",
                    "org.opencontainers.image.created": "2020-01-14 00:00:00-08:00",
                    "org.opencontainers.image.licenses": "GPL-2.0-only",
                    "org.opencontainers.image.title": "CentOS Base Image",
                    "org.opencontainers.image.vendor": "CentOS"
                }
            },
            "NetworkSettings": {
                "Bridge": "",
                "SandboxID": "4f31fb1fedf3c15a099f63014203b056f2c29187e5bd333dfcd01476e9339118",
                "HairpinMode": false,
                "LinkLocalIPv6Address": "",
                "LinkLocalIPv6PrefixLen": 0,
                "Ports": {},
                "SandboxKey": "/var/run/docker/netns/4f31fb1fedf3",
                "SecondaryIPAddresses": null,
                "SecondaryIPv6Addresses": null,
                "EndpointID": "52548e125940c16fc152743bcad035411c67d03a34a7585452763bd2b36c9c29",
                "Gateway": "172.17.0.1",
                "GlobalIPv6Address": "",
                "GlobalIPv6PrefixLen": 0,
                "IPAddress": "172.17.0.2",
                "IPPrefixLen": 16,
                "IPv6Gateway": "",
                "MacAddress": "02:42:ac:11:00:02",
                "Networks": {
                    "bridge": {
                        "IPAMConfig": null,
                        "Links": null,
                        "Aliases": null,
                        "NetworkID": "eebbaba16bf28e00b62bd9fe3a8faa5deb1a77cb315353561db856207060ad03",
                        "EndpointID": "52548e125940c16fc152743bcad035411c67d03a34a7585452763bd2b36c9c29",
                        "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
                    }
                }
            }
        }
    ]
    
    宿主文件夹:/var/lib/docker/volumes/92f1136825480b227587155797241a8b51d06ee0894624bae488c239111752c3/_data
    
    • 8、在容器中创建文件夹并且输入内容,在宿主文件夹下查看
    [root@localhost ~]# cd /var/lib/docker/volumes/92f1136825480b227587155797241a8b51d06ee0894624bae488c239111752c3/_data
    [root@localhost _data]# ll
    总用量 0
    [root@localhost _data]# ll
    总用量 4
    -rw-r--r--. 1 root root 16 2月  23 02:33 container.txt
    [root@localhost _data]# cat container.txt
    Container input
    [root@localhost _data]#
    
    • 9、Docker挂载主机目录Docker访问出现cannot open directory .: Permission denied
    在挂载目录后多加一个--privileged=true参数即可
    

    四、数据卷容器

    1、什么是数据卷容器

    命名的容器挂载数据卷,其它容器通过挂载这个(父容器)实现数据共享,挂载数据卷的容器,称之为数据卷容器

    2、先启动一个父容器dc1

    docker run -it --name=dc1 centos:2.0
    

    3、在容器dc1的dataVolumeContainer2文件夹中创建dc1.txt

    [root@localhost /]# docker run -it --name=dc1 centos:2.0
    [root@f5943ed65cad /]# ls
    bin  dataVolumeContainer1  dataVolumeContainer2  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
    [root@f5943ed65cad /]# cd dataVolumeContainer2
    [root@f5943ed65cad dataVolumeContainer2]# ls
    [root@f5943ed65cad dataVolumeContainer2]# touch dc1.txt
    [root@f5943ed65cad dataVolumeContainer2]# ls
    dc1.txt
    [root@f5943ed65cad dataVolumeContainer2]#
    

    4、创建dc2、dc3继承自dc1

    docker run -it --name=dc2 --volumes-from dc1 centos:2.0
    docker run -it --name=dc3 --volumes-from dc1 centos:2.0
    

    5、进入容器dc2中的文件夹dataVolumeContainer2

    [root@localhost /]# docker run -it --name=dc2 --volumes-from dc1 centos:2.0
    [root@1a90d12e1013 /]# ls
    bin  dataVolumeContainer1  dataVolumeContainer2  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
    [root@1a90d12e1013 /]# cd dataVolumeContainer2
    [root@1a90d12e1013 dataVolumeContainer2]# ls
    dc1.txt
    

    6、在容器dc2/dc3中分别创建dc2.txt/dc3.txt

    # 在容器dc2中创建dc2.txt
    [root@1a90d12e1013 dataVolumeContainer2]# touch dc2.txt
    [root@1a90d12e1013 dataVolumeContainer2]#
    
    # 在容器dc3中创建dc3.txt
    [root@94932e713bcb dataVolumeContainer2]# touch dc3.txt
    

    7、在容器dc1中查看

    [root@f5943ed65cad dataVolumeContainer2]# ls
    dc1.txt  dc2.txt  dc3.txt
    

    8、删除dc1容器,通过容器dc2来查看dc1.txt是否存在

    # 删除dc1容器
    [root@localhost /]# docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
    94932e713bcb        centos:2.0          "/bin/sh -c /bin/bash"   7 minutes ago       Up 7 minutes                            dc3
    1a90d12e1013        centos:2.0          "/bin/sh -c /bin/bash"   9 minutes ago       Up 9 minutes                            dc2
    
    #查看其它容器,dc1.txt依然存在
    [root@94932e713bcb dataVolumeContainer2]# ls
    dc1.txt  dc2.txt  dc3.txt
    

    9、新建dc4继承dc3,并且删除dc2、dc3

    # 新建dc4容器
    [root@localhost /]# docker run -it --name dc4 --volumes-from dc3 centos:2.0
    [root@b316ca93b74c /]# ls
    bin  dataVolumeContainer1  dataVolumeContainer2  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
    [root@b316ca93b74c /]# cd dataVolumeContainer2
    [root@b316ca93b74c dataVolumeContainer2]# ls
    dc1.txt  dc2.txt  dc3.txt
    [root@b316ca93b74c dataVolumeContainer2]# cat dc1.txt
    Container3 Input
    [root@b316ca93b74c dataVolumeContainer2]# vi dc1.txt
    [root@b316ca93b74c dataVolumeContainer2]# cat
    ^Z
    [1]+  Stopped                 cat
    [root@b316ca93b74c dataVolumeContainer2]# cat dc1.txt
    Container3 Input
    
    Container4 Input
    [root@b316ca93b74c dataVolumeContainer2]#
    

    10、小结

    • 容器之间配置信息的传递,数据卷的生命周期一直持续到没有容器使用它为止

    相关文章

      网友评论

          本文标题:六、Docker容器数据卷

          本文链接:https://www.haomeiwen.com/subject/kkwrqhtx.html