Docker8_1.Docker容器数据卷挂载

Docker8_1.Docker容器数据卷挂载

  • 挂载为什么会出现?
  • docker volume:容器数据卷挂载命令
  • 使用数据卷
    • 直接使用命令来挂载 -v
    • docker inspect 容器id 查看挂载信息
  • 三种挂载方式

官放文档地址:https://docs.docker.com/engine/reference/commandline/volume/

挂载为什么会出现?

场景: 如果将数据都存放在容器中,那么删除容器,数据就会丢失。
以MySQL为例,进行数据的持久化,容器删了,等价于删库跑路了,那么为了防止删除容器连带数据一起删除,因此有没有办法要把数据存储在本地?

容器之间有一个数据共享的技术,Docker容器产生的数据,可以同步到本地,这就是卷技术,将容器的目录挂载到Linux服务器的磁盘上。

总结: 容器的持久化和同步操作,容器之间也是可以数据共享的,挂载是一个双向绑定的过程,即使容器停止运行,依然不影响数据共享。

docker volume:容器数据卷挂载命令

#语法:
docker volume COMMAND COMMAND
命令 描述
docker volume create 创建卷
docker volume inspect 显示一个或多个卷的详细信息。
docker volume ls 列表显示卷
docker volume prune 删除所有未使用的本地卷
docker volume rm 删除一个或多个卷

使用数据卷

直接使用命令来挂载 -v

docker run -it -v 主机目录:容器目录 -p 主机端口:容器端口

以tomcat为例,我这里将本机/home和容器内的/usr/local进行挂载

交互式进入容器/usr/local目录

[root@localhost home]# docker run -it --name tomcat03 -v /home:/usr/local tomcat:9.0 /bin/bash
root@e6c3e51c8d9d:/usr/local/tomcat# cd ..
root@e6c3e51c8d9d:/usr/local# ls
haokai  tomcat
root@e6c3e51c8d9d:/usr/local#

查看本机的挂载目录

[root@localhost /]# cd home/
[root@localhost home]# ll
total 4
drwx------. 15 haokai haokai 4096 Jan  6 02:12 haokai
drwxr-xr-x.  2 root   root      6 Jan  6 02:11 tomcat
[root@localhost home]# docker ps

docker inspect 容器id 查看挂载信息

mounts就是挂载的相关信息

"Mounts": [
            {
                "Type": "bind",
                "Source": "/home",
                "Destination": "/usr/local",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],

Docker8_1.Docker容器数据卷挂载_第1张图片

[root@localhost home]# docker ps
CONTAINER ID   IMAGE                 COMMAND             CREATED          STATUS          PORTS                                       NAMES
e6c3e51c8d9d   tomcat:9.0            "/bin/bash"         8 minutes ago    Up 8 minutes    8080/tcp                                    tomcat03
a16acb738a00   tomcat:9.0            "catalina.sh run"   10 minutes ago   Up 10 minutes   8080/tcp                                    tomcat01
1a5a0ffc0338   portainer/portainer   "/portainer"        25 hours ago     Up 41 minutes   0.0.0.0:8088->9000/tcp, :::8088->9000/tcp   inspiring_newton
[root@localhost home]# docker inspect e6c3e51c8d9d
[
    {
        "Id": "e6c3e51c8d9d725e2d9cb5505c6d5c5e7ba385566ebeac7b6602768ef573fae3",
        "Created": "2022-01-06T07:11:59.325202834Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 5185,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2022-01-06T07:12:00.042744181Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:76206e3ba4b1e2f4dcf8a83c0983e49f04cb57fbdf5adf0ce3738dfed5244766",
        "ResolvConfPath": "/var/lib/docker/containers/e6c3e51c8d9d725e2d9cb5505c6d5c5e7ba385566ebeac7b6602768ef573fae3/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/e6c3e51c8d9d725e2d9cb5505c6d5c5e7ba385566ebeac7b6602768ef573fae3/hostname",
        "HostsPath": "/var/lib/docker/containers/e6c3e51c8d9d725e2d9cb5505c6d5c5e7ba385566ebeac7b6602768ef573fae3/hosts",
        "LogPath": "/var/lib/docker/containers/e6c3e51c8d9d725e2d9cb5505c6d5c5e7ba385566ebeac7b6602768ef573fae3/e6c3e51c8d9d725e2d9cb5505c6d5c5e7ba385566ebeac7b6602768ef573fae3-json.log",
        "Name": "/tomcat03",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": [
                "/home:/usr/local"
            ],
            "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/695834c6abb0b3f63670213b9d4852ab7b9b9121f7b99615d36154abb09f7c16-init/diff:/var/lib/docker/overlay2/7f270bfbf9e8789ed4fcdea2cb15f8c997ea458f119d5e49c367ab4326de9564/diff:/var/lib/docker/overlay2/5af24043e0eb9b086d2c4fbd86c98e55c8b010e08dd1273a2ae3d1c7b0695a11/diff:/var/lib/docker/overlay2/2ebacc466ba7e410fda102bb0f304c0ac9c340467bbfd77c7a9754da0b8e9ddb/diff:/var/lib/docker/overlay2/20ce8849bd101acdee36b79fa6099266859494774cde3f66e6e41e295fe27e48/diff:/var/lib/docker/overlay2/c3de8d96a381ed34d032685cb6149f88ea470b2a798a8175c63726b3162ab5df/diff:/var/lib/docker/overlay2/3e4eac200f570cdc4f1da6ee9dd3d8a3836d05c2b29be2c0de43ce0ef4140073/diff:/var/lib/docker/overlay2/8118e9b590dd04446e9229833c4f51e648fe0cd94625f3a9703461736017952b/diff:/var/lib/docker/overlay2/0ba4d33256cf4a96bb9f35fe5c06f8491c9ae2aa3b4d6cfa9dbd20ada23fe488/diff:/var/lib/docker/overlay2/4ca76cc06bb1ef195f5dee5b1d914dac98116f4684892f375de4820a39e59e0c/diff:/var/lib/docker/overlay2/ffdceab11e1db7f0c6cd2c3d0f7ae6114dd6df84722d890d92f14ae31c43f379/diff",
                "MergedDir": "/var/lib/docker/overlay2/695834c6abb0b3f63670213b9d4852ab7b9b9121f7b99615d36154abb09f7c16/merged",
                "UpperDir": "/var/lib/docker/overlay2/695834c6abb0b3f63670213b9d4852ab7b9b9121f7b99615d36154abb09f7c16/diff",
                "WorkDir": "/var/lib/docker/overlay2/695834c6abb0b3f63670213b9d4852ab7b9b9121f7b99615d36154abb09f7c16/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "bind",
                "Source": "/home",
                "Destination": "/usr/local",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],
        "Config": {
            "Hostname": "e6c3e51c8d9d",
            "Domainname": "",
            "User": "",
            "AttachStdin": true,
            "AttachStdout": true,
            "AttachStderr": true,
            "ExposedPorts": {
                "8080/tcp": {}
            },
            "Tty": true,
            "OpenStdin": true,
            "StdinOnce": true,
            "Env": [
                "PATH=/usr/local/tomcat/bin:/usr/local/openjdk-11/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "JAVA_HOME=/usr/local/openjdk-11",
                "LANG=C.UTF-8",
                "JAVA_VERSION=11.0.13",
                "CATALINA_HOME=/usr/local/tomcat",
                "TOMCAT_NATIVE_LIBDIR=/usr/local/tomcat/native-jni-lib",
                "LD_LIBRARY_PATH=/usr/local/tomcat/native-jni-lib",
                "GPG_KEYS=48F8E69F6390C9F25CFEDCD268248959359E722B A9C5DF4D22E99998D9875A5110C01C5A2F6059E7 DCFD35E0BF8CA7344752DE8B6FB21E8933C60243",
                "TOMCAT_MAJOR=9",
                "TOMCAT_VERSION=9.0.55",
                "TOMCAT_SHA512=a0c480b8bba09069bda3b57f54e658450a59d799474ad587dead0ffbf5074c16ee3f9f9c13312d0ff3227c7034589dabf25941fbd672838e9baeee9661e024dc"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "Image": "tomcat:9.0",
            "Volumes": null,
            "WorkingDir": "/usr/local/tomcat",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "306f7ccc0319284edfe8ccc7834bf1cd6862fd2972c3972c51ee4fe96dafd143",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "8080/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/306f7ccc0319",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "2623e819c883a1ece4e4862b13e5515c906bfede639e3ae4305d5dc8d82efc3d",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.4",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:04",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "1f628b850dab8ed82c1b9596b224633604d8eea05744de5ce56bb240ee6346dc",
                    "EndpointID": "2623e819c883a1ece4e4862b13e5515c906bfede639e3ae4305d5dc8d82efc3d",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.4",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:04",
                    "DriverOpts": null
                }
            }
        }
    }
]
[root@localhost home]#

三种挂载方式

# 如何确定是具名挂载、匿名挂载、指定路径挂载
-v 容器内路径					匿名挂载
-v 卷名:容器内路径			具名挂载
-v /容器外路径:/容器内路径		指定路径挂载,看前面是否有/

# 通过 -v 容器内路径:ro(readonly 只读) 或 rw(默认:readwrite 可读可写) 改变读写权限

# 一旦设置了容器权限,这时容器对我们挂载出来的内容就有限定了,针对容器而言。
# ro也就是不能在容器内操作了,只能通过外部改变数据同步容器,容器内不能在进行操作

docker run -d -P --name nginx03 -v juming-nginx:/etc/nginx/:ro nginx
docker run -d -P --name nginx03 -v juming-nginx:/etc/nginx/:rw nginx

你可能感兴趣的:(docker,docker,容器,运维)