龙空技术网

Docker进阶---数据管理(使用数据卷、主机目录)

liuwei2142 301

前言:

今天小伙伴们对“docker数据卷挂载”大概比较着重,姐妹们都需要学习一些“docker数据卷挂载”的相关资讯。那么小编在网络上搜集了一些有关“docker数据卷挂载””的相关知识,希望我们能喜欢,姐妹们一起来学习一下吧!

在 Docker 内部以及容器之间管理数据主要有两种方式:

数据卷(Volumes)

挂载主机目录 (Bind mounts)

1. 数据卷

数据卷 是一个可供一个或多个容器使用的特殊目录,可以提供很多有用的特性:

数据卷 可以在容器之间共享和重用 对 数据卷 的修改会立马生效 对 数据卷 的更新,不会影响镜像 数据卷 默认会一直存在,即使容器被删除

2. 创建数据卷

[root@centos1 ~]# docker volume create my-volmy-vol

3. 查看数据卷

[root@centos1 ~]# docker volume lsDRIVER VOLUME NAMElocal 5f579e2fe794463b43dc31aff91c7cd122afc40bad7656f14a9063efca538cf7local ecfccad03ed382b4a8bee1a29158b158078bec9c5d4321c9ebb7ecb36a6b88a4local my-vol[root@centos1 ~]# docker volume inspect my-vol[ { "CreatedAt": "2019-04-29T21:02:07+08:00", "Driver": "local", "Labels": {}, "Mountpoint": "/var/lib/docker/volumes/my-vol/_data", "Name": "my-vol", "Options": {}, "Scope": "local" }]

4. 启用挂载数据卷的容器

在用 docker run 命令的时候,使用 --mount 标记来将 数据卷 挂载到容器里。在一次 docker run 中可以挂载多个 数据卷。

下面创建一个名为 web 的容器,并加载一个 数据卷 到容器的 /webapp 目录。

[root@centos1 dockfile]# cat app.py from flask import Flaskfrom redis import Redis, RedisErrorimport osimport socket# Connect to Redisredis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)app = Flask(__name__)@app.route("/")def hello(): try: visits = redis.incr("counter") except RedisError: visits = "<i>cannot connect to Redis, counter disabled</i>" html = "<h3>Hello {name}!</h3>" \  "<b>Hostname:</b> {hostname}<br/>" \  "<b>Visits:</b> {visits}" return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)if __name__ == "__main__": app.run(host='0.0.0.0', port=80)

查看镜像:

[root@centos1 dockfile]# docker image ls friendlyhelloREPOSITORY TAG IMAGE ID CREATED SIZEfriendlyhello v1.0 542981dbc30a 2 weeks ago 131MB

(1) 挂载数据卷

[root@centos1 dockfile]# docker run -d -P \> --name web \> --mount source=my-vol,target=/webapp \> friendlyhello:v1.0 \> python app.py0086a43b3ce265b2a4c0bf4e043cc0d014f50b407a1248cb149a307bf17067ec

(2)查看web容器的挂载信息

[root@centos1 dockfile]# docker inspect web[ { "Id": "0086a43b3ce265b2a4c0bf4e043cc0d014f50b407a1248cb149a307bf17067ec", "Created": "2019-05-01T05:53:05.71075631Z", "Path": "python", "Args": [ "app.py" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 34074, "ExitCode": 0, "Error": "", "StartedAt": "2019-05-01T05:53:06.970434009Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:542981dbc30a508d965555f1952cb5419d562393339ce70a2d905b039be607d9", "ResolvConfPath": "/var/lib/docker/containers/0086a43b3ce265b2a4c0bf4e043cc0d014f50b407a1248cb149a307bf17067ec/resolv.conf", "HostnamePath": "/var/lib/docker/containers/0086a43b3ce265b2a4c0bf4e043cc0d014f50b407a1248cb149a307bf17067ec/hostname", "HostsPath": "/var/lib/docker/containers/0086a43b3ce265b2a4c0bf4e043cc0d014f50b407a1248cb149a307bf17067ec/hosts", "LogPath": "/var/lib/docker/containers/0086a43b3ce265b2a4c0bf4e043cc0d014f50b407a1248cb149a307bf17067ec/0086a43b3ce265b2a4c0bf4e043cc0d014f50b407a1248cb149a307bf17067ec-json.log", "Name": "/web", "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": true, "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, "Mounts": [ { "Type": "volume", "Source": "my-vol", "Target": "/webapp" } ], "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/a566caa8f2260e29caacb5de8f79bd5f0192cfe2c0fba7e5dfa2a18e46bc626b-init/diff:/var/lib/docker/overlay2/c69d5a766136213b43f91380ef3a72df22f845bc81953cef853a73eda1ab518f/diff:/var/lib/docker/overlay2/6c68184b75cc8dd21339aead8cd4a566ca0a672cf1899b98bb792305c30c08dc/diff:/var/lib/docker/overlay2/1209bc7d087ccbcec5401dd85a82dc9a25071bf037185cfec56274736b747688/diff:/var/lib/docker/overlay2/010cb9681ef5597006a0a41736ffd55664799d5cf733be18d1b59eb9bb6bcc4a/diff:/var/lib/docker/overlay2/aa2c16521cc328da4ae763bb035f93b6c1116a38b96d2ae39da104c2b4729eaa/diff:/var/lib/docker/overlay2/22e11d4d3001ea0e9bc6739a1806fae647f72b352118a3ca1cc257f686d39379/diff:/var/lib/docker/overlay2/5d458786728d39e54ba19d2053caf498c55038962dc8fb6fcf199112e7185fdd/diff", "MergedDir": "/var/lib/docker/overlay2/a566caa8f2260e29caacb5de8f79bd5f0192cfe2c0fba7e5dfa2a18e46bc626b/merged", "UpperDir": "/var/lib/docker/overlay2/a566caa8f2260e29caacb5de8f79bd5f0192cfe2c0fba7e5dfa2a18e46bc626b/diff", "WorkDir": "/var/lib/docker/overlay2/a566caa8f2260e29caacb5de8f79bd5f0192cfe2c0fba7e5dfa2a18e46bc626b/work" }, "Name": "overlay2" }, "Mounts": [ { "Type": "volume", "Name": "my-vol", "Source": "/var/lib/docker/volumes/my-vol/_data", "Destination": "/webapp", "Driver": "local", "Mode": "z", "RW": true, "Propagation": "" } ], "Config": { "Hostname": "0086a43b3ce2", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "80/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "LANG=C.UTF-8", "PYTHONIOENCODING=UTF-8", "GPG_KEY=C01E1CAD5EA2C4F0B8E3571504C367C218ADD4FF", "PYTHON_VERSION=2.7.16", "PYTHON_PIP_VERSION=19.0.3", "NAME=World" ], "Cmd": [ "python", "app.py" ], "Image": "friendlyhello:v1.0", "Volumes": null, "WorkingDir": "/tool/docker/app", "Entrypoint": null, "OnBuild": null, "Labels": {} }, "NetworkSettings": { "Bridge": "", "SandboxID": "ec541a75fc9e0d523788ca10377831554368516f958d4a6e26eef43b68fd7bc5", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": { "80/tcp": [ { "HostIp": "0.0.0.0", "HostPort": "32768" } ] }, "SandboxKey": "/var/run/docker/netns/ec541a75fc9e", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "b76493e207b8b3221d6e0067648d23fdef22f7a419971d3ffd9eccacf0eca779", "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": "86b6f2174773ee5ce7b4407a5ca74884ce128a4f4cc2c9f1330491865d499e8d", "EndpointID": "b76493e207b8b3221d6e0067648d23fdef22f7a419971d3ffd9eccacf0eca779", "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 } } } }]

5. 删除数据卷

数据卷 是被设计用来持久化数据的,它的生命周期独立于容器,Docker 不会在容器被删除后自动删除 数据卷,并且也不存在垃圾回收这样的机制来处理没有任何容器引用的 数据卷。

如果需要在删除容器的同时移除数据卷。可以在删除容器的时候使用 docker rm -v 这个命令。

(1)删除一个数据卷

[root@centos1 dockfile]# docker volume lsDRIVER VOLUME NAMElocal 5f579e2fe794463b43dc31aff91c7cd122afc40bad7656f14a9063efca538cf7local ecfccad03ed382b4a8bee1a29158b158078bec9c5d4321c9ebb7ecb36a6b88a4local my-vol[root@centos1 dockfile]# [root@centos1 dockfile]# docker volume rm my-volmy-vol[root@centos1 dockfile]# docker volume lsDRIVER VOLUME NAMElocal 5f579e2fe794463b43dc31aff91c7cd122afc40bad7656f14a9063efca538cf7local ecfccad03ed382b4a8bee1a29158b158078bec9c5d4321c9ebb7ecb36a6b88a4

(2)清理数据卷

无主的数据卷可能会占据很多空间,要清理请使用以下命令

[root@centos1 dockfile]# docker volume pruneWARNING! This will remove all local volumes not used by at least one container.Are you sure you want to continue? [y/N] yDeleted Volumes:5f579e2fe794463b43dc31aff91c7cd122afc40bad7656f14a9063efca538cf7ecfccad03ed382b4a8bee1a29158b158078bec9c5d4321c9ebb7ecb36a6b88a4Total reclaimed space: 0B

6. 挂载主机目录

[root@centos1 dockfile]# docker run -d -P --name web --mount type=bind,source=/src/webapp,target=/opt/webapp friendlyhello:v1.0 python app.pycf7120db209f5a641f42e4a2df0f73261eeba7aa30b5ee1321de8c82ad1e0f1b

上面的命令加载主机的 /src/webapp 目录到容器的 /opt/webapp目录。这个功能在进行测试的时候十分方便,比如用户可以放置一些程序到本地目录中,来查看容器是否正常工作。本地目录的路径必须是绝对路径,以前使用 -v 参数时如果本地目录不存在 Docker 会自动为你创建一个文件夹,现在使用 --mount 参数时如果本地目录不存在,Docker 会报错。

Docker 挂载主机目录的默认权限是 读写,用户也可以通过增加 readonly 指定为 只读:

docker run -d -P \ --name web \ --mount type=bind,source=/src/webapp,target=/opt/webapp,readonly \ friendlyhello:v1.0 \ python app.py [root@centos1 dockfile]# docker inspect web......"Mounts": [ { "Type": "bind", "Source": "/src/webapp", "Destination": "/opt/webapp", "Mode": "", "RW": true, "Propagation": "rprivate" } ]......

标签: #docker数据卷挂载