03-docker_使用dockerfile添加数据卷

d2cae3ddaa9a58e5a26cfb22c03dadf1.jpeg

在宿主机常见dockerfile文件,内容如下:

FROM centos
VOLUME ["/dataVolumeContainer1","/dataVolumeContainer2"]
CMD echo "finished,--------success1"
CMD /bin/bash

根据上面的dockerfile创建一个镜像

docker build -f dockerFile1 -t cjxz/centos .

命令描述:
-f:表示dockerfile文件的路径
-t:标签
. : 表示使用当前路径
使用docker images查看生成的镜像

[root@VM-0-4-centos _data]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED          SIZE
cjxz/centos   latest    12acdfb72421   42 minutes ago   231MB

使用docker run -it cjxz/centos启动容器。然后在容器下面指定的文件创建一个文件。操作如下

docker run  cjxz/centos
docker exec -it   容器id /bin/bash
cd dataVolumeContainer1
echo '1234567' > 1.txt
exit

我们刚才创建的1.txt就会出现在dataVolumeContainer1下面,那么容器里面dataVolumeContainer1和dataVolumeContainer2在宿主机的什么地方呢?我们可以使用docker inspect 容器id就能够看到容器的详细配置情况

 [
    {
        "Id": "3b2cf0472efe5be37e47efe842a35ccd1b1f0662bd8b43f11c4737205e6ee44c",
        "Created": "2022-04-29T08:58:26.576698568Z",
        "Path": "/bin/sh",
        "Args": [
            "-c",
            "/bin/bash"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 20600,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2022-04-29T09:07:32.049652599Z",
            "FinishedAt": "2022-04-29T09:07:03.125381894Z"
        },
        "Image": "sha256:12acdfb724210565489b728cb8f3497221913b20d2642374b56e818e7c99e700",
        "ResolvConfPath": "/var/lib/docker/containers/3b2cf0472efe5be37e47efe842a35ccd1b1f0662bd8b43f11c4737205e6ee44c/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/3b2cf0472efe5be37e47efe842a35ccd1b1f0662bd8b43f11c4737205e6ee44c/hostname",
        "HostsPath": "/var/lib/docker/containers/3b2cf0472efe5be37e47efe842a35ccd1b1f0662bd8b43f11c4737205e6ee44c/hosts",
        "LogPath": "/var/lib/docker/containers/3b2cf0472efe5be37e47efe842a35ccd1b1f0662bd8b43f11c4737205e6ee44c/3b2cf0472efe5be37e47efe842a35ccd1b1f0662bd8b43f11c4737205e6ee44c-json.log",
        "Name": "/compassionate_bhabha",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "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/4a59ea89f28a7fdf64b7a73700be3815902d613984812a5510c7ed082cca713b-init/diff:/var/lib/docker/overlay2/e48984b9b0e8671ffa4e5f09fe9bafeb94602fca6b187498b51698dc8506c1c0/diff",
                "MergedDir": "/var/lib/docker/overlay2/4a59ea89f28a7fdf64b7a73700be3815902d613984812a5510c7ed082cca713b/merged",
                "UpperDir": "/var/lib/docker/overlay2/4a59ea89f28a7fdf64b7a73700be3815902d613984812a5510c7ed082cca713b/diff",
                "WorkDir": "/var/lib/docker/overlay2/4a59ea89f28a7fdf64b7a73700be3815902d613984812a5510c7ed082cca713b/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "volume",
                "Name": "51256c6cca173c230b155a0bea5b2426e44858f67c5eb4ebd6e7f0768381d371",
                "Source": "/var/lib/docker/volumes/51256c6cca173c230b155a0bea5b2426e44858f67c5eb4ebd6e7f0768381d371/_data",
                "Destination": "/dataVolumeContainer1",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            },
            {
                "Type": "volume",
                "Name": "462a7ce975600a006426c1d7c6677c55a575aaf13e8954f5f82d19411d071798",
                "Source": "/var/lib/docker/volumes/462a7ce975600a006426c1d7c6677c55a575aaf13e8954f5f82d19411d071798/_data",
                "Destination": "/dataVolumeContainer2",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
        "Config": {
            "Hostname": "3b2cf0472efe",
            "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/sh",
                "-c",
                "/bin/bash"
            ],
            "Image": "cjxz/centos",
            "Volumes": {
                "/dataVolumeContainer1": {},
                "/dataVolumeContainer2": {}
            },
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20210915",
                "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": "859fe47d8ae07eeb0d5de5a25a465a00aa8a70fe000705475c8f993adf5c5676",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/859fe47d8ae0",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "d70a10daa20d5e357a1a668e4380ee5724513c39b944d81cd9fd8eee5f629038",
            "Gateway": "172.18.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.18.0.4",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:12:00:04",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "c992944e87220f17d7475ac7a643a4fc61bc85aa89ca91d0418c41d12a1f8b47",
                    "EndpointID": "d70a10daa20d5e357a1a668e4380ee5724513c39b944d81cd9fd8eee5f629038",
                    "Gateway": "172.18.0.1",
                    "IPAddress": "172.18.0.4",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:12:00:04",
                    "DriverOpts": null
                }
            }
        }
    }
]
[root@VM-0-4-centos _data]# 

找到Mounts节点,就能够看到容器中目录挂载到宿主机上面的哪个目录。我们主机挂载到了/var/lib/docker/volumes/51256c6cca173c230b155a0bea5b2426e44858f67c5eb4ebd6e7f0768381d371/_data下面。现在使用cd命令,查看之前创建的1.txt是否已经在该目录下面。

关于dockerfile的功能,我们后面再说

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,362评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,330评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,247评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,560评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,580评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,569评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,929评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,587评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,840评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,596评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,678评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,366评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,945评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,929评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,165评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,271评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,403评论 2 342

推荐阅读更多精彩内容