03-docker_使用dockerfile添加数据卷

2022-05-02  本文已影响0人  cjxz
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的功能,我们后面再说

上一篇下一篇

猜你喜欢

热点阅读