數據管理

這一章介紹如何在 Docker 內部以及容器之間管理數據,在容器中管理數據主要有兩種方式:linux

數據卷(Volumes)nginx

掛載主機目錄 (Bind mounts)web

 

數據卷redis

 

 

1.數據卷 能夠在容器之間共享和重用docker

 

2.數據卷 的修改會立馬生效json

 

3.數據卷 的更新,不會影響鏡像ubuntu

 

4.數據卷 默認會一直存在,即便容器被刪除centos

 

注意: 數據卷 的使用,相似於 Linux 下對目錄或文件進行 mount,鏡像中的被指定爲掛bash

 

載點的目錄中的文件會隱藏掉,能顯示看的是掛載的 數據卷 。app

[root@localhost ~]# docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED                  SIZE
ubuntu                         16.04               657d80a6401d        Less than a second ago   121MB
nginx                          latest              ab56bba91343        Less than a second ago   126MB
ubuntu                         v2                  a66d0fda4c36        5 hours ago              86.1MB
<none>                         <none>              986426ddabcf        5 hours ago              86.1MB
nginx                          v2                  06292a546f31        25 hours ago             126MB
commit-test                    v1                  28a5df57c0b9        45 hours ago             447MB
commit-test                    v2                  28a5df57c0b9        45 hours ago             447MB
erixero/centos7.3-base-image   latest              ae904afa83bb        2 years ago              288MB
ansible/centos7-ansible        latest              688353a31fde        2 years ago              447MB
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS              PORTS                NAMES
c7a2a75a5536        nginx                     "nginx -g 'daemon of��"   26 hours ago        Up 26 hours         0.0.0.0:80->80/tcp   webserver
ce4ccea0bde8        ansible/centos7-ansible   "bash"                   47 hours ago        Up 47 hours                              web3
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# docker run -itd -P -v ghd_volume:/data centos7.3-base-image bash
Unable to find image 'centos7.3-base-image:latest' locally
^C
[root@localhost ~]# docker run -itd -P -v ghd_volume:/data erixero/centos7.3-base-image  bash
f6bbea44368060f520a27915e5446ad0cde67ebb4cad99c39370b6b6aa7fa8ce
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE                          COMMAND                  CREATED             STATUS              PORTS                NAMES
f6bbea443680        erixero/centos7.3-base-image   "bash"                   6 seconds ago       Up 5 seconds                             vibrant_lalande
c7a2a75a5536        nginx                          "nginx -g 'daemon of��"   26 hours ago        Up 26 hours         0.0.0.0:80->80/tcp   webserver
ce4ccea0bde8        ansible/centos7-ansible        "bash"                   47 hours ago        Up 47 hours                              web3
[root@localhost ~]# docker inspect f6bbea443680
[
    {
        "Id": "f6bbea44368060f520a27915e5446ad0cde67ebb4cad99c39370b6b6aa7fa8ce",
        "Created": "2019-08-31T08:53:49.082498253Z",
        "Path": "bash",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 87155,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2019-08-31T08:53:50.080729254Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:ae904afa83bbbb1eb542c356fb42a15f39ce96ce0dba9aa49a8fe51f18a44116",
        "ResolvConfPath": "/var/lib/docker/containers/f6bbea44368060f520a27915e5446ad0cde67ebb4cad99c39370b6b6aa7fa8ce/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/f6bbea44368060f520a27915e5446ad0cde67ebb4cad99c39370b6b6aa7fa8ce/hostname",
        "HostsPath": "/var/lib/docker/containers/f6bbea44368060f520a27915e5446ad0cde67ebb4cad99c39370b6b6aa7fa8ce/hosts",
        "LogPath": "/var/lib/docker/containers/f6bbea44368060f520a27915e5446ad0cde67ebb4cad99c39370b6b6aa7fa8ce/f6bbea44368060f520a27915e5446ad0cde67ebb4cad99c39370b6b6aa7fa8ce-json.log",
        "Name": "/vibrant_lalande",
        "RestartCount": 0,
        "Driver": "devicemapper",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": [
                "ghd_volume:/data"
            ],
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "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": true,
            "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": {
                "DeviceId": "93",
                "DeviceName": "docker-253:0-51547343-1e12217a5354101071e294c14c2d50853608f06644729fc76a240de07c2276d5",
                "DeviceSize": "10737418240"
            },
            "Name": "devicemapper"
        },
        "Mounts": [
            {
                "Type": "volume",
                "Name": "ghd_volume",
                "Source": "/var/lib/docker/volumes/ghd_volume/_data",
                "Destination": "/data",
                "Driver": "local",
                "Mode": "z",
                "RW": true,
                "Propagation": ""
            }
        ],
View Code

刪除數據卷

[root@localhost ~]# docker volume rm ghd_volume

 

掛載主機目錄

--mount和-v的區別,-v若是容器中沒有目標目錄會自動建立,使用--mount若是目標目錄不存在會報錯,爲了不掛錯的狀況發生,仍是建議使用--mount

docker run -it --mount type=bind,source=/opt,target=/opt erixero/centos7.3-base-image bash
bash-4.2# cd /opt/
bash-4.2# ls
bbb.txt  containerd  dockerfile  ghd  redis  ubuntu.tar.gz
bash-4.2# exit
[root@localhost ~]# cd /opt/
[root@localhost opt]# ls
bbb.txt  containerd  dockerfile  ghd  redis  ubuntu.tar.gz

#能夠看到/opt目錄已掛在到容器中
相關文章
相關標籤/搜索