Docker容器数据卷

容器数据卷

概念

docker的理念
将应用和环境打包成一个镜像!
如果数据都在容器中, 那么我们容器删除, 数据就会丢失!需求:数据可以持久化
容器之间可以有一个数据共享的技术!Docker容器中产生的数据, 同步到本地!这就是卷技术!目录的挂载,将我们容器内的目录, 挂载到Linux上面!
总结:容器的持久化和同步操作!容器间也是可以数据共享的!

使用数据卷

方式一:直接使用命令来挂载, -v

docker run -it -v 主机目录:容器目录
# 测试
docker run -it -v /home/ceshi:/home centos /bin/bash

# 启动后我们可以通过docker inspect 容器id 查看容器的详细信息
docker inspect 605792482b78 
[
    {
        "Id": "605792482b78ec465aeb1bc3435072c1d15ef5bb6c121b5435a2d54ccb6abd0f",
        "Created": "2021-07-07T04:05:06.1318939Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 4399,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-07-07T04:05:06.5403594Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
        "ResolvConfPath": "/var/lib/docker/containers/605792482b78ec465aeb1bc3435072c1d15ef5bb6c121b5435a2d54ccb6abd0f/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/605792482b78ec465aeb1bc3435072c1d15ef5bb6c121b5435a2d54ccb6abd0f/hostname",
        "HostsPath": "/var/lib/docker/containers/605792482b78ec465aeb1bc3435072c1d15ef5bb6c121b5435a2d54ccb6abd0f/hosts",
        "LogPath": "/var/lib/docker/containers/605792482b78ec465aeb1bc3435072c1d15ef5bb6c121b5435a2d54ccb6abd0f/605792482b78ec465aeb1bc3435072c1d15ef5bb6c121b5435a2d54ccb6abd0f-json.log",
        "Name": "/elastic_sinoussi",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": [
                "/Users/dm/ceshi:/home"
            ],
            "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/4bd0730f1688f7084071236f77ed381561be8a1fd0ae3aa335fe3f4fb97401f6-init/diff:/var/lib/docker/overlay2/44e554333aeb2e37bb18ba3ae2e6e8ee83f4e6eeade745b7ac39ded0c88b3f3f/diff",
                "MergedDir": "/var/lib/docker/overlay2/4bd0730f1688f7084071236f77ed381561be8a1fd0ae3aa335fe3f4fb97401f6/merged",
                "UpperDir": "/var/lib/docker/overlay2/4bd0730f1688f7084071236f77ed381561be8a1fd0ae3aa335fe3f4fb97401f6/diff",
                "WorkDir": "/var/lib/docker/overlay2/4bd0730f1688f7084071236f77ed381561be8a1fd0ae3aa335fe3f4fb97401f6/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "bind",
                "Source": "/Users/dm/ceshi",
                "Destination": "/home",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],
        "Config": {
            "Hostname": "605792482b78",
            "Domainname": "",
            "User": "",
            "AttachStdin": true,
            "AttachStdout": true,
            "AttachStderr": true,
            "Tty": true,
            "OpenStdin": true,
            "StdinOnce": true,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "Image": "centos",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20201204",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "fe488aec7aaa3a6bf80de5c7b26f5b88ac3f2bc0daf99a88d16d98958aa6256f",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/fe488aec7aaa",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "5681466c86468c0787979446e3e62ab7b914841208325b3b93bead8e02919cc1",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.5",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:05",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "231fbbf145a9be8f27f4d0de98f3950dc170218f64b3a24ab416b43a491a27cd",
                    "EndpointID": "5681466c86468c0787979446e3e62ab7b914841208325b3b93bead8e02919cc1",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.5",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:05",
                    "DriverOpts": null
                }
            }
        }
    }
]

# 可以同时在容器内和宿主机修改数据同步映射

数据卷的好处

修改只需要在本地修改即可, 容器内会自动同步!

Dockerfile

Dockerfile是用来构建docker镜像的构建文件,命令文件, 通过这个脚本可以生成镜像,镜像是一层的, 脚步是一个一个的命令, 每个命令都是一层

方式二、Dockerfile构建

# 在一个目录下创建一个dockerfile文件,名字可以随意,建议Dockerfile。编写如下命令
FROM centos

VOLUME ["volume01", "volume02"]

CMD echo "----end----"
CMD /bin/bash

# 运行dockerfile文件(mac)
docker build -f dockerfile1 -t test/centos .
[+] Building 0.1s (5/5) FINISHED                                                
 => [internal] load build definition from dockerfile1                      0.0s
 => => transferring dockerfile: 124B                                       0.0s
 => [internal] load .dockerignore                                          0.0s
 => => transferring context: 2B                                            0.0s
 => [internal] load metadata for docker.io/library/centos:latest           0.0s
 => [1/1] FROM docker.io/library/centos                                    0.0s
 => exporting to image                                                     0.0s
 => => exporting layers                                                    0.0s
 => => writing image sha256:02c2769725446694db5e95968ba34f1ce7e2ef38daa6f  0.0s
 => => naming to docker.io/test/centos  

# 查看打包的镜像
docker images

# 运行镜像
dockder run -it 镜像id

# 测试查看容器卷挂载
docker ps # 查看运行中的镜像
docker inspect 容器id # 查看容器挂载
"Mounts": [
            {
                "Type": "volume",
                "Name": "8b647cb0e98a96de1cc4d726e81df009962269239599eb8bc250885c39412777",
                "Source": "/var/lib/docker/volumes/8b647cb0e98a96de1cc4d726e81df009962269239599eb8bc250885c39412777/_data",
                "Destination": "/home/volume01",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            },
            {
                "Type": "volume",
                "Name": "f70ee35a1feafccd88b4957843d04248086174751ff5db8d30fe0d026f95c641",
                "Source": "/var/lib/docker/volumes/f70ee35a1feafccd88b4957843d04248086174751ff5db8d30fe0d026f95c641/_data",
                "Destination": "/home/volume02",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ]

实现多个mysql数据共享

docker run -d -p 3306:3306 -v /etc/mysql/conf.d -v /var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -name mysql01 mysql:5.7

docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -name mysql02 --volumes-from mysql01 mysql:5.7
# 此时可以实现两个容器数据同步

结论

容器之间配置信息的传递, 数据卷容器的生命周期一直持续到没有容器使用为止。但是一旦你持久化道本地,这个时候,本地的数据是不会删除的!

posted @ 2021-07-07 13:27  phper-liunian  阅读(79)  评论(0编辑  收藏  举报