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

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
容器服务 Serverless 版 ACK Serverless,317元额度 多规格
容器服务 Serverless 版 ACK Serverless,952元额度 多规格
简介: 云原生之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

image.png

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 
相关文章
|
9天前
|
Kubernetes Cloud Native Docker
云原生时代的容器化实践:Docker和Kubernetes入门
【10月更文挑战第37天】在数字化转型的浪潮中,云原生技术成为企业提升敏捷性和效率的关键。本篇文章将引导读者了解如何利用Docker进行容器化打包及部署,以及Kubernetes集群管理的基础操作,帮助初学者快速入门云原生的世界。通过实际案例分析,我们将深入探讨这些技术在现代IT架构中的应用与影响。
38 2
|
7天前
|
运维 Cloud Native 虚拟化
一文吃透云原生 Docker 容器,建议收藏!
本文深入解析云原生Docker容器技术,涵盖容器与Docker的概念、优势、架构设计及应用场景等,建议收藏。关注【mikechen的互联网架构】,10年+BAT架构经验倾囊相授。
一文吃透云原生 Docker 容器,建议收藏!
|
9天前
|
运维 Kubernetes Cloud Native
云原生技术:容器化与微服务架构的完美结合
【10月更文挑战第37天】在数字化转型的浪潮中,云原生技术以其灵活性和高效性成为企业的新宠。本文将深入探讨云原生的核心概念,包括容器化技术和微服务架构,以及它们如何共同推动现代应用的发展。我们将通过实际代码示例,展示如何在Kubernetes集群上部署一个简单的微服务,揭示云原生技术的强大能力和未来潜力。
|
7天前
|
Cloud Native API 持续交付
云原生之旅:从容器到微服务的演进之路
【10月更文挑战第39天】在这篇文章中,我们将一起探索云原生技术的奥秘。通过浅显易懂的语言和生动的比喻,我们将了解云原生技术如何改变软件开发的世界。文章将带领读者从容器的基本概念出发,逐步深入到微服务架构的实践,揭示这些技术如何助力现代应用的快速迭代与可靠部署。准备好,让我们启程进入云原生的精彩世界吧!
|
8天前
|
缓存 监控 开发者
掌握Docker容器化技术:提升开发效率的利器
在现代软件开发中,Docker容器化技术成为提升开发效率和应用部署灵活性的重要工具。本文介绍Docker的基本概念,并分享Dockerfile最佳实践、容器网络配置、环境变量和秘密管理、容器监控与日志管理、Docker Compose以及CI/CD集成等技巧,帮助开发者更高效地利用Docker。
|
9天前
|
监控 持续交付 Docker
Docker 容器化部署在微服务架构中的应用有哪些?
Docker 容器化部署在微服务架构中的应用有哪些?
|
9天前
|
监控 持续交付 Docker
Docker容器化部署在微服务架构中的应用
Docker容器化部署在微服务架构中的应用
|
9天前
|
安全 持续交付 Docker
微服务架构和 Docker 容器化部署的优点是什么?
微服务架构和 Docker 容器化部署的优点是什么?
|
19天前
|
Kubernetes 监控 开发者
掌握容器化:Docker与Kubernetes的最佳实践
【10月更文挑战第26天】本文深入探讨了Docker和Kubernetes的最佳实践,涵盖Dockerfile优化、数据卷管理、网络配置、Pod设计、服务发现与负载均衡、声明式更新等内容。同时介绍了容器化现有应用、自动化部署、监控与日志等开发技巧,以及Docker Compose和Helm等实用工具。旨在帮助开发者提高开发效率和系统稳定性,构建现代、高效、可扩展的应用。
|
15天前
|
关系型数据库 MySQL API