美文网首页【原创】Docker实战系列
Docker管理-容器的数据卷

Docker管理-容器的数据卷

作者: 复苏的兵马俑 | 来源:发表于2020-04-17 22:16 被阅读0次

    1、手工交互数据

    [root@Server ~]# docker image ls
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    nginx               1.17                5a8dfb2ca731        24 hours ago        127MB
    debian              10.3                378ca4b1d2fe        31 hours ago        114MB
    centos              8.1.1911            470671670cac        3 months ago        237MB
    centos              7.7.1908            08d05d1d5859        5 months ago        204MB
    
    [root@Server ~]# docker container run -d --name "nginx01" -p 80:80 nginx:1.17
    a32f8fcf3fbdd3b04c47b845cfa9d29cb4b2523352b316407d412028df6393be
    
    [root@Server ~]# docker container exec -it nginx01 /bin/bash
    
    root@a32f8fcf3fbd:/# cd /usr/share/nginx/html/
    
    root@a32f8fcf3fbd:/usr/share/nginx/html# ls
    50x.html  index.html
    
    [root@Server ~]# mkdir /opt/html
    
    [root@Server ~]# docker container cp nginx01:/usr/share/nginx/html/index.html /opt/html/
    
    [root@Server ~]# vi /opt/html/index.html
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to Kevin!</title>
    <style>
        body {
            width: 35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>Welcome to Kevin!</h1>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>
    
    <p>For online documentation and support please refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>
    
    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>
    
    复制文件之前的访问界面
    [root@Server ~]# docker container cp /opt/html/index.html nginx01:/usr/share/nginx/html/
    
    复制文件之后的访问界面

    2、数据卷目录

       作用:实现宿主机和容器之间的数据共享,数据持久化

    [root@Server ~]# docker run -d --name "nginx02" -p 81:80 -v /opt/html:/usr/share/nginx/html nginx:1.17
    56d64d7395e9faf365c2255464db561564efee48a3e0ccb6b63dfa8c65e8b19b
    
    [root@Server ~]# docker container ls -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
    56d64d7395e9        nginx:1.17          "nginx -g 'daemon of…"   3 minutes ago       Up 3 minutes        0.0.0.0:81->80/tcp   nginx02
    a32f8fcf3fbd        nginx:1.17          "nginx -g 'daemon of…"   4 hours ago         Up 4 hours          0.0.0.0:80->80/tcp   nginx01
    
    [root@Server ~]# docker container inspect nginx02
    [
        {
            "Id": "e086dfdfb77e0eb11361c5ea52edda8ce30c74c0b5a361006fc0a7b625486cd2",
            "Created": "2020-04-17T13:45:57.748879716Z",
            "Path": "nginx",
            "Args": [
                "-g",
                "daemon off;"
            ],
            "State": {
                "Status": "running",
                "Running": true,
                "Paused": false,
                "Restarting": false,
                "OOMKilled": false,
                "Dead": false,
                "Pid": 21448,
                "ExitCode": 0,
                "Error": "",
                "StartedAt": "2020-04-17T13:45:59.095618047Z",
                "FinishedAt": "0001-01-01T00:00:00Z"
            },
            "Image": "sha256:5a8dfb2ca7312ee39433331b11d92f45bb19d7809f7c0ff19e1d01a2c131e959",
            "ResolvConfPath": "/var/lib/docker/containers/e086dfdfb77e0eb11361c5ea52edda8ce30c74c0b5a361006fc0a7b625486cd2/resolv.conf",
            "HostnamePath": "/var/lib/docker/containers/e086dfdfb77e0eb11361c5ea52edda8ce30c74c0b5a361006fc0a7b625486cd2/hostname",
            "HostsPath": "/var/lib/docker/containers/e086dfdfb77e0eb11361c5ea52edda8ce30c74c0b5a361006fc0a7b625486cd2/hosts",
            "LogPath": "/var/lib/docker/containers/e086dfdfb77e0eb11361c5ea52edda8ce30c74c0b5a361006fc0a7b625486cd2/e086dfdfb77e0eb11361c5ea52edda8ce30c74c0b5a361006fc0a7b625486cd2-json.log",
            "Name": "/nginx02",
            "RestartCount": 0,
            "Driver": "overlay2",
            "Platform": "linux",
            "MountLabel": "",
            "ProcessLabel": "",
            "AppArmorProfile": "",
            "ExecIDs": null,
            "HostConfig": {
                "Binds": [
                    "/opt/html:/usr/share/nginx/html"
                ],
                "ContainerIDFile": "",
                "LogConfig": {
                    "Type": "json-file",
                    "Config": {}
                },
                "NetworkMode": "default",
                "PortBindings": {
                    "80/tcp": [
                        {
                            "HostIp": "",
                            "HostPort": "81"
                        }
                    ]
                },
                "RestartPolicy": {
                    "Name": "no",
                    "MaximumRetryCount": 0
                },
                "AutoRemove": false,
                "VolumeDriver": "",
                "VolumesFrom": null,
                "CapAdd": null,
                "CapDrop": null,
                "Capabilities": null,
                "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/d34f22469b1bf37f68b69cfd707918a2ae5ddf2b639d1f8147b7d50247d5d9a1-init/diff:/var/lib/docker/overlay2/0c22fab2a8d719ad451addd652df0d990262be5759b0b19192688f0472c497c8/diff:/var/lib/docker/overlay2/15b6f7757b927cb157fa21855214d16b47f7f35fe45f33f002c56c52a652fd21/diff:/var/lib/docker/overlay2/a0cb43e1ab97f722d9fd56439c3e69637f924b23f8402740dcd32fd5fc9be590/diff",
                    "MergedDir": "/var/lib/docker/overlay2/d34f22469b1bf37f68b69cfd707918a2ae5ddf2b639d1f8147b7d50247d5d9a1/merged",
                    "UpperDir": "/var/lib/docker/overlay2/d34f22469b1bf37f68b69cfd707918a2ae5ddf2b639d1f8147b7d50247d5d9a1/diff",
                    "WorkDir": "/var/lib/docker/overlay2/d34f22469b1bf37f68b69cfd707918a2ae5ddf2b639d1f8147b7d50247d5d9a1/work"
                },
                "Name": "overlay2"
            },
            "Mounts": [
                {
                    "Type": "bind",
                    "Source": "/opt/html",
                    "Destination": "/usr/share/nginx/html",
                    "Mode": "",
                    "RW": true,
                    "Propagation": "rprivate"
                }
            ],
            "Config": {
                "Hostname": "e086dfdfb77e",
                "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.17.9",
                    "NJS_VERSION=0.3.9",
                    "PKG_RELEASE=1~buster"
                ],
                "Cmd": [
                    "nginx",
                    "-g",
                    "daemon off;"
                ],
                "Image": "nginx:1.17",
                "Volumes": null,
                "WorkingDir": "",
                "Entrypoint": null,
                "OnBuild": null,
                "Labels": {
                    "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
                },
                "StopSignal": "SIGTERM"
            },
            "NetworkSettings": {
                "Bridge": "",
                "SandboxID": "22bad10e33a88c304e772388467d5e04d4a5b4cafad040aada09457c574712b4",
                "HairpinMode": false,
                "LinkLocalIPv6Address": "",
                "LinkLocalIPv6PrefixLen": 0,
                "Ports": {
                    "80/tcp": [
                        {
                            "HostIp": "0.0.0.0",
                            "HostPort": "81"
                        }
                    ]
                },
                "SandboxKey": "/var/run/docker/netns/22bad10e33a8",
                "SecondaryIPAddresses": null,
                "SecondaryIPv6Addresses": null,
                "EndpointID": "505af95540bcba9d314cef57ab02500105d6196e8e6ba8a67891d61de49398a7",
                "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": "6cd0c524350127caafa9b2c1394aa25333836dd8e91d3040066b763fabdc4f65",
                        "EndpointID": "505af95540bcba9d314cef57ab02500105d6196e8e6ba8a67891d61de49398a7",
                        "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@Server ~]# vi /opt/html/index.html
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to Kevin's home!</title>
    <style>
        body {
            width: 35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>Welcome to Kevin's home!</h1>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>
    
    <p>For online documentation and support please refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>
    
    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>
    
    [root@Server ~]# docker run -d --name "nginx03" -p 82:80 -v /opt/html:/usr/share/nginx/html nginx:1.17
    5c7bb2b9c5d4297ec61d4ced4681296d5bd4fcb7489dab1be1ebb9a2ebdb6d21
    
    [root@Server ~]# docker container ls -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
    5c7bb2b9c5d4        nginx:1.17          "nginx -g 'daemon of…"   4 seconds ago       Up 3 seconds        0.0.0.0:82->80/tcp   nginx03
    56d64d7395e9        nginx:1.17          "nginx -g 'daemon of…"   5 minutes ago       Up 5 minutes        0.0.0.0:81->80/tcp   nginx02
    a32f8fcf3fbd        nginx:1.17          "nginx -g 'daemon of…"   4 hours ago         Up 4 hours          0.0.0.0:80->80/tcp   nginx01
    
    [root@Server ~]# vi /opt/html/index.html
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to My home!</title>
    <style>
        body {
            width: 35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>Welcome to My home!</h1>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>
    
    <p>For online documentation and support please refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>
    
    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>
    

    3、数据卷容器

       作用:实现宿主机和容器之间的数据共享,数据持久化;在集中管理集群中,大批量的容器都需要挂载相同的多个数据卷时,可以采用数据卷容器进行统一管理。

    [root@Server ~]# mkdir /opt/config/
    
    [root@Server ~]# docker container cp nginx01:/etc/nginx /opt/config/
    
    [root@Server ~]# docker run -it --name "nginx_volumes" -v /opt/html:/usr/share/nginx/html -v /opt/config/nginx:/etc/nginx centos:8.1.1911 /bin/bash
    [root@7bc917a9b4a7 /]# 
    
    [root@1664a5e9e207 /]# ls /usr/share/nginx/html/
    index.html
    
    [root@1664a5e9e207 /]# ls /etc/nginx/
    conf.d          koi-utf  mime.types  nginx.conf   uwsgi_params
    fastcgi_params  koi-win  modules     scgi_params  win-utf
    
    [root@Server ~]# docker container ls -a
    CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
    1664a5e9e207        centos:8.1.1911     "/bin/bash"         About a minute ago   Up About a minute                       nginx_volumes
    
    [root@Server ~]# docker container run -d -p 81:80 --volumes-from nginx_volumes --name "nginx01" nginx:1.17
    9e309c49760c0e5415e8d0ff5e377484327f811d2c5a70700c62af9b942b425c
    
    [root@Server ~]# docker container run -d -p 82:80 --volumes-from nginx_volumes --name "nginx02" nginx:1.17
    1384e559267f1c904137418c36255bfd9cef48ce7188162b7393528462ea990b
    
    [root@Server ~]# vi /opt/html/index.html
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to Docker!</title>
    <style>
        body {
            width: 35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>Welcome to Docker!</h1>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>
    
    <p>For online documentation and support please refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>
    
    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>
    

    相关文章

      网友评论

        本文标题:Docker管理-容器的数据卷

        本文链接:https://www.haomeiwen.com/subject/tbgyvhtx.html