docker 应用数据的管理
容器数据存储的三种方式
docker volume docker管理素质及文件系统的一部分,保存数据最佳方式
bind mounts 将宿主机的文件映射到容器里
tmpfs 存储在宿主机的内存里
查看docker 的默认存储驱动
Containers: 9 Running: 5 Paused: 0 Stopped: 4 Images: 3 Server Version: 18.09.2 Storage Driver: overlay2 默认存储驱动 Backing Filesystem: xfs Supports d_type: true Native Overlay Diff: true Logging Driver: json-file Cgroup Driver: cgroupfs Plugins: Volume: local Network: bridge host macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog Swarm: inactive Runtimes: runc Default Runtime: runc Init Binary: docker-init containerd version: 9754871865f7fe2f4e74d43e2fc7ccd237edcbce runc version: 09c8266bf2fcf9519a651b04ae54c967b9ab86ec init version: fec3683 Security Options: seccomp Profile: default Kernel Version: 3.10.0-862.el7.x86_64 Operating System: CentOS Linux 7 (Core) OSType: linux Architecture: x86_64 CPUs: 4 Total Memory: 7.622GiB Name: localhost.localdomain ID: BEO4:3VTY:XQ4A:UCJE:QLQL:OGXD:VOBQ:WMUY:V5PL:LTL5:3KPH:7YIX Docker Root Dir: /var/lib/docker Debug Mode (client): false Debug Mode (server): false Registry: https://index.docker.io/v1/ Labels: Experimental: false Insecure Registries: 127.0.0.0/8 Registry Mirrors: http://f1361db2.m.daocloud.io/ Live Restore Enabled: false Product License: Community Engine
容器数据卷的管理
查看当前系统数据卷
[root@localhost ~]# docker volume ls DRIVER VOLUME NAME
创建数据卷
[root@localhost ~]# docker volume ls DRIVER VOLUME NAME local chenxi_vol
查看数据卷的详细信息
[root@localhost ~]# docker volume inspect chenxi_vol [ { "CreatedAt": "2019-02-22T14:19:31+08:00", "Driver": "local", "Labels": {}, "Mountpoint": "/var/lib/docker/volumes/chenxi_vol/_data", 实际宿主机的目录 "Name": "chenxi_vol", "Options": {}, "Scope": "local" } ]
删除已存在的所有容器
[root@localhost ~]# docker rm -f $(docker ps -a | awk '{print $1}' ) 7638633dc06a 0771848dd42e 25aff37e7dd0 ab4b909c4e68 a3bdc2062cd7 d2394de6c3a1 53dfa07e6f2c 4279c14287c1 dc2a611dae83 Error: No such container: CONTAINER
启动容器并挂载数据卷
[root@localhost ~]# docker run -d --name web-1 -p 80:80 --mount src=chenxi_vol,dst=/usr/share/nginx/html nginxc074c3141663aa7b52b45af4d15fc8a4a13d94b334b70c99fd2f7a5d6c065d22 [root@localhost ~]# docker inspect web-1 [ { "Id": "c074c3141663aa7b52b45af4d15fc8a4a13d94b334b70c99fd2f7a5d6c065d22", "Created": "2019-02-22T06:35:20.789320917Z", "Path": "nginx", "Args": [ "-g", "daemon off;" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 22721, "ExitCode": 0, "Error": "", "StartedAt": "2019-02-22T06:35:22.374476291Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:f09fe80eb0e75e97b04b9dfb065ac3fda37a8fac0161f42fca1e6fe4d0977c80", "ResolvConfPath": "/var/lib/docker/containers/c074c3141663aa7b52b45af4d15fc8a4a13d94b334b70c99fd2f7a5d6c065d22/resolv.conf", "HostnamePath": "/var/lib/docker/containers/c074c3141663aa7b52b45af4d15fc8a4a13d94b334b70c99fd2f7a5d6c065d22/hostname", "HostsPath": "/var/lib/docker/containers/c074c3141663aa7b52b45af4d15fc8a4a13d94b334b70c99fd2f7a5d6c065d22/hosts", "LogPath": "/var/lib/docker/containers/c074c3141663aa7b52b45af4d15fc8a4a13d94b334b70c99fd2f7a5d6c065d22/c074c3141663aa7b52b45af4d15fc8a4a13d94b334b70c99fd2f7a5d6c065d22-json.log", "Name": "/web-1", "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, "Mounts": [ { "Type": "volume", "Source": "chenxi_vol", 挂载数据卷 "Target": "/usr/share/nginx/html" } ], "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/a0e351c43971e9ccab264bf25a49506959a354b635b2351a9afaedd50d78c135-init/diff:/var/lib/docker/overlay2/1491a86a89f7d4721b73639455d75fb153bc1 2c6cdaee687039d2e9317215741/diff:/var/lib/docker/overlay2/808c5a465c27497cb57ea709b083e855b6e11e2efa70887b1e3d6f2ece944ab3/diff:/var/lib/docker/overlay2/97f2bebcc1413f092415377b6d7e31dec473bd074f23afcde2d2394c65feed65/diff", "MergedDir": "/var/lib/docker/overlay2/a0e351c43971e9ccab264bf25a49506959a354b635b2351a9afaedd50d78c135/merged", "UpperDir": "/var/lib/docker/overlay2/a0e351c43971e9ccab264bf25a49506959a354b635b2351a9afaedd50d78c135/diff", "WorkDir": "/var/lib/docker/overlay2/a0e351c43971e9ccab264bf25a49506959a354b635b2351a9afaedd50d78c135/work" }, "Name": "overlay2" }, "Mounts": [ { "Type": "volume", "Name": "chenxi_vol", "Source": "/var/lib/docker/volumes/chenxi_vol/_data", "Destination": "/usr/share/nginx/html", "Driver": "local", "Mode": "z", "RW": true, "Propagation": "" } ], "Config": { "Hostname": "c074c3141663", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "80/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "NGINX_VERSION=1.15.8-1~stretch", "NJS_VERSION=1.15.8.0.2.7-1~stretch" ], "Cmd": [ "nginx", "-g", "daemon off;" ], "ArgsEscaped": true, "Image": "nginx", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": { "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>" }, "StopSignal": "SIGTERM" }, "NetworkSettings": { "Bridge": "", "SandboxID": "0439f377a7e2778a55457b8d48615adf0bba1fbe425bb72e03f4d5b95d31f0c5", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": { "80/tcp": null }, "SandboxKey": "/var/run/docker/netns/0439f377a7e2", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "79bffed969d27453e7cc9e0bd1e42f59e2279374423673731221973ade7c5e6d", "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": "3b2c8016eb022a0e3a9896ff22b7aee66d3034fef8b301b91dc791ff184eef13", "EndpointID": "79bffed969d27453e7cc9e0bd1e42f59e2279374423673731221973ade7c5e6d", "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 } } } }
]
[root@localhost ~]# ls /var/lib/docker/volumes/chenxi_vol/_data
50x.html index.html
前端访问
修改网页内容
[root@localhost ~]# echo "haha" > /var/lib/docker/volumes/chenxi_vol/_data/index.html
删除容器
[root@localhost ~]# docker rm -f web-2 web-2
重新再起一个挂载相同数据卷
[root@localhost ~]# docker run -d --name web-3 -p 80:80 --mount src=chenxi_vol,dst=/usr/share/nginx/html nginx 0a82046ce01576d2832c236c6d7cfe548ab09954a8757b2b8f32615712fdd078 [root@localhost ~]# ss -lntp State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* users:(("sshd",pid=921,fd=3))LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=1310,fd=13))LISTEN 0 128 :::80 :::* users:(("docker-proxy",pid=23126,fd=4))LISTEN 0 128 :::22 :::* users:(("sshd",pid=921,fd=4))LISTEN 0 100 ::1:25 :::*
创建容器使用另一种方式挂载数据目录
[root@localhost ~]# docker run -d --name web-4 -p 84:80 -v chenxi_vol:/usr/share/nginx/html nginx d3c1a6e2d46ed8867f08a1eb9a7f4438bdfa1960af148e066be409e8c6072c44 [root@localhost ~]# ss -lntp State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* users:(("sshd",pid=921,fd=3))LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=1310,fd=13))LISTEN 0 128 :::80 :::* users:(("docker-proxy",pid=23126,fd=4))LISTEN 0 128 :::84 :::* users:(("docker-proxy",pid=23221,fd=4))LISTEN 0 128 :::22 :::* users:(("sshd",pid=921,fd=4))LISTEN 0 100 ::1:25 :::*
草都可以从石头缝隙中长出来更可况你呢