云原生之Docker容器的存储管理

Posted 江湖有缘

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了云原生之Docker容器的存储管理相关的知识,希望对你有一定的参考价值。

一、检查本地docker环境

1.检查宿主机系统版本

[root@docker ~]# cat /etc/centos-release
CentOS Linux release 7.6.1810 (Core) 

2.检查docker版本

[root@docker ~]# docker -v
Docker version 20.10.18, build b40c2f6

二、创建一个测试httpd容器

1.创建测试目录

[root@docker ~]# mkdir -p /data/test
[root@docker ~]# cd /data/test/
[root@docker test]# 

2.创建httpd容器

docker run -d  --name web01 -p 82:80 -v /data/test/htdocs/:/usr/local/apache2/htdocs/ httpd

3.查看容器状态

[root@docker test]# docker ps
CONTAINER ID   IMAGE                         COMMAND                  CREATED              STATUS              PORTS                                                 NAMES
05d35ba971ad   httpd                         "httpd-foreground"       About a minute ago   Up About a minute   0.0.0.0:82->80/tcp, :::82->80/tcp                     web01

4.查看容器详细信息

[root@docker test]# docker inspect web01 
[
    
        "Id": "05d35ba971ad78179a7ab7a93dfb4c59faeb5872152f03a897144f4e5040b7ee",
        "Created": "2022-10-24T09:15:39.545943014Z",
        "Path": "httpd-foreground",
        "Args": [],
        "State": 
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 113766,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2022-10-24T09:15:39.826230552Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        ,
        "Image": "sha256:dabbfbe0c57b6e5cd4bc089818d3f664acfad496dc741c9a501e72d15e803b34",
        "ResolvConfPath": "/var/lib/docker/containers/05d35ba971ad78179a7ab7a93dfb4c59faeb5872152f03a897144f4e5040b7ee/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/05d35ba971ad78179a7ab7a93dfb4c59faeb5872152f03a897144f4e5040b7ee/hostname",
        "HostsPath": "/var/lib/docker/containers/05d35ba971ad78179a7ab7a93dfb4c59faeb5872152f03a897144f4e5040b7ee/hosts",
        "LogPath": "/var/lib/docker/containers/05d35ba971ad78179a7ab7a93dfb4c59faeb5872152f03a897144f4e5040b7ee/05d35ba971ad78179a7ab7a93dfb4c59faeb5872152f03a897144f4e5040b7ee-json.log",
        "Name": "/web01",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": 
            "Binds": [
                "/data/test/htdocs/:/usr/local/apache2/htdocs/"
            ],
            "ContainerIDFile": "",
            "LogConfig": 
                "Type": "json-file",
                "Config": 
            ,
            "NetworkMode": "default",
            "PortBindings": 
                "80/tcp": [
                    
                        "HostIp": "",
                        "HostPort": "82"
                    
                ]
            ,
            "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/97d6b86a78f355c15fb43d539fc284b60f464c0ee768d87a8e5cbe62e3a62626-init/diff:/var/lib/docker/overlay2/76281b2c0491c38ccd64dc2c9164c58eefd014a7294117c4962515bf75419681/diff:/var/lib/docker/overlay2/1f4f0f5f0c604cca28ad955db0b19c04f93026468448ca98f43f4eeaa69b4186/diff:/var/lib/docker/overlay2/47f840069670e495b72b41d3aba5e0ed52346af2da30c4b9accc0b9866d9e3f9/diff:/var/lib/docker/overlay2/e1042193e6b8e7fefe86fc987757ee0a9c7d1f297db466e08af143ead1095138/diff:/var/lib/docker/overlay2/4649e6ac724d477b2fe8db2da934d572eaaaa946a847936794b506edf8b0c87d/diff",
                "MergedDir": "/var/lib/docker/overlay2/97d6b86a78f355c15fb43d539fc284b60f464c0ee768d87a8e5cbe62e3a62626/merged",
                "UpperDir": "/var/lib/docker/overlay2/97d6b86a78f355c15fb43d539fc284b60f464c0ee768d87a8e5cbe62e3a62626/diff",
                "WorkDir": "/var/lib/docker/overlay2/97d6b86a78f355c15fb43d539fc284b60f464c0ee768d87a8e5cbe62e3a62626/work"
            ,
            "Name": "overlay2"
        ,
        "Mounts": [
            
                "Type": "bind",
                "Source": "/data/test/htdocs",
                "Destination": "/usr/local/apache2/htdocs",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            
        ],
        "Config": 
            "Hostname": "05d35ba971ad",
            "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": null,
            "WorkingDir": "/usr/local/apache2",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": ,
            "StopSignal": "SIGWINCH"
        ,
        "NetworkSettings": 
            "Bridge": "",
            "SandboxID": "99696b43e61670fc9f98c0d84d6da281fe4db3ec52dc5210895014dbe4ca69a4",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": 
                "80/tcp": [
                    
                        "HostIp": "0.0.0.0",
                        "HostPort": "82"
                    ,
                    
                        "HostIp": "::",
                        "HostPort": "82"
                    
                ]
            ,
            "SandboxKey": "/var/run/docker/netns/99696b43e616",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "276ea4df3a145005646c1f457c57ad47ab3e367cc6a2c15ce78bc2b0a198b25d",
            "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": "49542796523d70c8ccea7b297c45542ee4b5f30be2d9e1420cdf9ea57c5864fe",
                    "EndpointID": "276ea4df3a145005646c1f457c57ad47ab3e367cc6a2c15ce78bc2b0a198b25d",
                    "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
                
            
        
    
]

三、宿主机目录内更新文件,验证容器内读取

1.进入容器宿主机挂载目录

[root@docker test]# pwd
/data/test
[root@docker test]# cd htdocs/
[root@docker htdocs]# ls
[root@docker htdocs]# 

2.新建index.html文件

echo "Docker technology plays an important role in today's society." > index.html

3.查看容器内index.html

[root@docker htdocs]# docker exec -it web01 /bin/bash
root@05d35ba971ad:/usr/local/apache2# ls
bin  build  cgi-bin  conf  error  htdocs  icons  include  logs	modules
root@05d35ba971ad:/usr/local/apache2# cd htdocs/
root@05d35ba971ad:/usr/local/apache2/htdocs# ls
index.html
root@05d35ba971ad:/usr/local/apache2/htdocs# cat index.html 
Docker technology plays an important role in today's society.

四、删除测试容器,查看宿主机index.heml

[root@docker htdocs]# docker stop web01 
web01
[root@docker htdocs]# docker rm  web01 
web01
[root@docker htdocs]# ls
index.html
[root@docker htdocs]# cat index.html 
Docker technology plays an important role in today's society.

五、容器的数据卷管理

1.再次运行测试容器

[root@docker htdocs]# docker run -d  --name web02 -p 85:80 -v /usr/local/apache2/htdocs/  httpd
f028c9fbcddb186dd616a5bf7990378abc9f492e9275f0f5b587ef60a30145e3

2.查看容器默认挂载的数据卷

[root@docker htdocs]# docker inspect web02  |grep Mounts -A5
        "Mounts": [
            
                "Type": "volume",
                "Name": "3b1eab4eb072a43375068c8ff534028de2a7f6dd6aeceb60c0d1584c4bc046a6",
                "Source": "/var/lib/docker/volumes/3b1eab4eb072a43375068c8ff534028de2a7f6dd6aeceb60c0d1584c4bc046a6/_data",
                "Destination": "/usr/local/apache2/htdocs",

3.进入容器内编辑index.html内容

[root@docker htdocs]# docker exec -it web02  /bin/bash
root@f028c9fbcddb:/usr/local/apache2# ls
bin  build  cgi-bin  conf  error  htdocs  icons  include  logs	modules
root@f028c9fbcddb:/usr/local/apache2# cd htdocs/
root@f028c9fbcddb:/usr/local/apache2/htdocs# echo "Docker technology plays an important role in today's society." > index.html
root@f028c9fbcddb:/usr/local/apache2/htdocs# 

4.宿主机访问内容

[root@docker htdocs]# curl 127.0.0.1:85
Docker technology plays an important role in today's society.

5.在宿主机默认挂载卷检查index.html

[root@docker htdocs]# docker inspect web02  |grep Mounts -A5
        "Mounts": [
            
                "Type": "volume",
                "Name": "3b1eab4eb072a43375068c8ff534028de2a7f6dd6aeceb60c0d1584c4bc046a6",
                "Source": "/var/lib/docker/volumes/3b1eab4eb072a43375068c8ff534028de2a7f6dd6aeceb60c0d1584c4bc046a6/_data",
                "Destination": "/usr/local/apache2/htdocs",
[root@docker htdocs]# cd /var/lib/docker/volumes/3b1eab4eb072a43375068c8ff534028de2a7f6dd6aeceb60c0d1584c4bc046a6/_data
[root@docker _data]# ls
index.html
[root@docker _data]# cat index.html 
Docker technology plays an important role in today's society.

6.删除容器,验证数据持久化

[root@docker _data]# docker stop web02 
web02
[root@docker _data]# docker rm  web02 
web02
[root@docker _data]# ls
index.html
[root@docker _data]# pwd
/var/lib/docker/volumes/3b1eab4eb072a43375068c8ff534028de2a7f6dd6aeceb60c0d1584c4bc046a6/_data
[root@docker _data]# cat index.html 
Docker technology plays an important role in today's society.

六、创建数据卷容器

1.创建一个数据卷容器test03

[root@docker htdocs]#  docker create --name test03 -v /data/test/htdocs:/usr/local/apache2/htdocs -v /other/tools/ busybox 
3aab455cee0bccd7776ed43bee1d083a4fb61275e7cfd8b63a3e0de9a9b6b9c5


2.查看数据卷容器详细信息

[root@docker htdocs]# docker inspect test03 |grep Mounts -A16
        "Mounts": [
            
                "Type": "volume",
                "Name": "795b0038d6092015781b524d85e95252326787f9e5db45b703666f3ca587c1db",
                "Source": "/var/lib/docker/volumes/795b0038d6092015781b524d85e95252326787f9e5db45b703666f3ca587c1db/_data",
                "Destination": "/other/tools",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            ,
            
                "Type": "bind",
                "Source": "/data/test/htdocs",
                "Destination": "/usr/local/apache2/htdocs",
                "Mode": "",
                "RW": true,
[root@docker htdocs]# 

七、创建test04测试容器

1.创建test04容器

[root@docker htdocs]#  docker run --name test04 -td -p 86:80 --volumes-from test03  busybox
cfde4db1ff755695a7b2f4c10e863c9fdf8e9a0f7f4a132d2fdd2fb0e4c497f8


2.检查test04容器的volume部分

[root@docker htdocs]# docker inspect test04 |grep Mounts
        "Mounts": [
[root@docker htdocs]# docker inspect test04 |grep Mounts -A16
        "Mounts": [
            
                "Type": "bind",
                "Source": "/data/test/htdocs",
                "Destination": "/usr/local/apache2/htdocs",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            ,
            
                "Type": "volume",
                "Name": "795b0038d6092015781b524d85e95252326787f9e5db45b703666f3ca587c1db",
                "Source": "/var/lib/docker/volumes/795b0038d6092015781b524d85e95252326787f9e5db45b703666f3ca587c1db/_data",
                "Destination": "/other/tools",
                "Driver": "local",
                "Mode": "",
                "RW": true,


3.检查test04容器中index.html

[root@docker htdocs]# docker exec -it test04 /bin/bash
OCI runtime exec failed: exec failed: unable to start container process: exec: "/bin/bash": stat /bin/bash: no such file or directory: unknown
[root@docker htdocs]# docker exec -it test04 /bin/sh
/ # cat /usr/local/apache2/htdocs/index.html
Docker technology plays an important role in today's society.
/ # 


4.在test04容器挂载路径创建文件,验证宿主机路径信息

/ # cd /other/tools/
/other/tools # ls
/other/tools # echo "aa-test-out " > test.out
/other/tools # 

5.检查数据卷中的文件

[root@docker htdocs]# docker inspect test04 |grep Mounts
        "Mounts": [
[root@docker htdocs]# docker inspect test04 |grep Mounts -A16
        "Mounts": [
            
                "Type": "bind",
                "Source": "/data/test/htdocs",
                "Destination": "/usr/local/apache2/htdocs",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            ,
            
                "Type": "volume",
                "Name": "795b0038d6092015781b524d85e95252326787f9e5db45b703666f3ca587c1db",
                "Source": "/var/lib/docker/volumes/795b0038d6092015781b524d85e95252326787f9e5db45b703666f3ca587c1db/_data",
                "Destination": "/other/tools",
                "Driver": "local",
                "Mode": "",
                "RW": true,
[root@docker htdocs]# cd /var/lib/docker/volumes/795b0038d6092015781b524d85e95252326787f9e5db45b703666f3ca587c1db/_data
[root@docker _data]# ls
test.out
[root@docker _data]# cat test.out 
aa-test-out 

以上是关于云原生之Docker容器的存储管理的主要内容,如果未能解决你的问题,请参考以下文章

云原生之Docker实战部署轻量级容器云管理平台Humpback

[云原生之DockerDocker容器的存储与迁移

云原生之Docker实战使用Docker部署MinIO对象存储

云原生之Docker实战使用docker部署Wiznote私人笔记系统

云原生之Docker使用docker部署talebook个人图书管理平台

云原生之使用Docker部署Dailynotes个人笔记管理工具