DOCKER04_详解Dockerfile基本指令、FROM、LABEL、RUN、CMD、ENTRYPOINT、ARG、ENV、VOLUME、USER(六)

简介: ⑨. VOLUME 创建数据卷挂载点

⑨. VOLUME 创建数据卷挂载点


①. 把容器的某些文件夹映射到主机外部


②. 写法:这个一般写在最后


VOLUME ["/var/log/"] #可以是JSON数组


VOLUME /var/log #可以直接写


VOLUME /var/log /var/db #可以空格分割多个


③. 注意:用VOLUME声明了卷,那么以后对于卷内容的修改会被丢弃,所以一定在volume声明之前修改内容


FROM alpine
RUN mkdir /hello && mkdir /app
RUN echo 1111 > /hello/a.txt
RUN echo 2222 > /app/b.txt
# 挂在容器内的指定文件夹,如果不存在则创建
# 指定了volume,即使启动容器没有指定-v参数,我们也会自动进行匿名卷挂载
# 这里的 hello app 都是容器里面的文件夹,请你在使用镜像启动容器的时候,自动给宿主机上挂载
VOLUME [ "/hello","/app"]
# 用VOLUME声明了卷,那么以后对于卷内容的修改会被丢弃,所以一定在volume声明之前修改内容
RUN echo 6666 > /hello/a.txt
RUN echo 8888 > /app/b.txt
CMD ping baidu.com


[root@i-id8g0yu9 ~]# docker build --no-cache -t dockerfileworkvolume:v1 -f dockerfileVOLUME .
Sending build context to Docker daemon  2.474MB
Step 1/8 : FROM alpine
 ---> 6dbb9cc54074
Step 2/8 : RUN mkdir /hello && mkdir /app
 ---> Running in 36ed19f5446d
Removing intermediate container 36ed19f5446d
 ---> 42af237e9a7e
Step 3/8 : RUN echo 1111 > /hello/a.txt
 ---> Running in 662511d7ddb7
Removing intermediate container 662511d7ddb7
 ---> f744129965ae
Step 4/8 : RUN echo 2222 > /app/b.txt
 ---> Running in a8321f4f8d98
Removing intermediate container a8321f4f8d98
 ---> 1d03ca553eb7
Step 5/8 : VOLUME [ "/hello","/app"]
 ---> Running in 1d4ec1c1e8f7
Removing intermediate container 1d4ec1c1e8f7
 ---> 4e76b09577d3
Step 6/8 : RUN echo 6666 > /hello/a.txt
 ---> Running in 912d739bb4d4
Removing intermediate container 912d739bb4d4
 ---> da35323435dd
Step 7/8 : RUN echo 8888 > /app/b.txt
 ---> Running in bab5aceb4f7a
Removing intermediate container bab5aceb4f7a
 ---> 73c45738db48
Step 8/8 : CMD ping baidu.com
 ---> Running in 05bedb917269
Removing intermediate container 05bedb917269
 ---> a01440e2a675
Successfully built a01440e2a675
Successfully tagged dockerfileworkvolume:v1
[root@i-id8g0yu9 ~]# docker run -d dockerfileworkvolume:v1
56ef53c95a6c7f96a8cf308be8393ee53fb70a4802f7c43e4519cf5fab438abc
[root@i-id8g0yu9 ~]# docker ps
CONTAINER ID   IMAGE                     COMMAND                  CREATED          STATUS          PORTS     NAMES
56ef53c95a6c   dockerfileworkvolume:v1   "/bin/sh -c 'ping ba…"   4 seconds ago    Up 3 seconds              stupefied_ptolemy
b4d8744b6a92   dockerfileworkdir:v1      "/bin/sh -c 'ping ba…"   49 minutes ago   Up 49 minutes             myworkdir
[root@i-id8g0yu9 ~]# docker  inspect 56ef53c95a6c
[
    {
        "Id": "56ef53c95a6c7f96a8cf308be8393ee53fb70a4802f7c43e4519cf5fab438abc",
        "Created": "2021-04-18T14:45:15.782034507Z",
        "Path": "/bin/sh",
        "Args": [
            "-c",
            "ping baidu.com"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 8131,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-04-18T14:45:16.143344962Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:a01440e2a675dab49c9fb9c788e890c81913e7f15de3a4071fec51a5a5475aea",
        "ResolvConfPath": "/var/lib/docker/containers/56ef53c95a6c7f96a8cf308be8393ee53fb70a4802f7c43e4519cf5fab438abc/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/56ef53c95a6c7f96a8cf308be8393ee53fb70a4802f7c43e4519cf5fab438abc/hostname",
        "HostsPath": "/var/lib/docker/containers/56ef53c95a6c7f96a8cf308be8393ee53fb70a4802f7c43e4519cf5fab438abc/hosts",
        "LogPath": "/var/lib/docker/containers/56ef53c95a6c7f96a8cf308be8393ee53fb70a4802f7c43e4519cf5fab438abc/56ef53c95a6c7f96a8cf308be8393ee53fb70a4802f7c43e4519cf5fab438abc-json.log",
        "Name": "/stupefied_ptolemy",
        "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/bca4693743c24765c88e89c05892e9b8f3f0b39918c5a400d58c9b9db5ec450c-init/diff:/var/lib/docker/overlay2/8623d3705ceea6e0f9349ac0645977e1fa8c45cc9ea6337115098c03d259a200/diff:/var/lib/docker/overlay2/960ab6c1f64fd999f44c90be57256371c1e5dd43f115e1d2ff11037408a8ffc8/diff:/var/lib/docker/overlay2/871b7ffab75435d24006f3f7bff690c903328b273764a7e4ab1922bece5b8ad7/diff:/var/lib/docker/overlay2/43d78cac3804ed1dc62283f315a185e8136796e5779d9ee8717fa9a2d99d6edd/diff",
                "MergedDir": "/var/lib/docker/overlay2/bca4693743c24765c88e89c05892e9b8f3f0b39918c5a400d58c9b9db5ec450c/merged",
                "UpperDir": "/var/lib/docker/overlay2/bca4693743c24765c88e89c05892e9b8f3f0b39918c5a400d58c9b9db5ec450c/diff",
                "WorkDir": "/var/lib/docker/overlay2/bca4693743c24765c88e89c05892e9b8f3f0b39918c5a400d58c9b9db5ec450c/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "volume",
                "Name": "65adbd1e4997c8df05a05b30ab1a2c3df970e19b0dd38f579f428ec8442810b9",
                "Source": "/var/lib/docker/volumes/65adbd1e4997c8df05a05b30ab1a2c3df970e19b0dd38f579f428ec8442810b9/_data",
                "Destination": "/app",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            },
            {
                "Type": "volume",
                "Name": "1c3739d31bd487d847d0101ac2d48bbdb661cc9cbe97799de86181c2f50d98db",
                "Source": "/var/lib/docker/volumes/1c3739d31bd487d847d0101ac2d48bbdb661cc9cbe97799de86181c2f50d98db/_data",
                "Destination": "/hello",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
        "Config": {
            "Hostname": "56ef53c95a6c",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "ping baidu.com"
            ],
            "Image": "dockerfileworkvolume:v1",
            "Volumes": {
                "/app": {},
                "/hello": {}
            },
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "d4490ef3887d03e22812b820c963c82a8edfe45c5f3141676a03df8d7aecfc51",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/d4490ef3887d",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "671b069ea364448c75dbbb4d9365c60b46abf392bdcca26bc9713cf9051baa2e",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.3",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:03",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "24c026c0a26cfb78a82f033887c3669c4ccaf63060e02fefe51e958cb5037c32",
                    "EndpointID": "671b069ea364448c75dbbb4d9365c60b46abf392bdcca26bc9713cf9051baa2e",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.3",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:03",
                    "DriverOpts": null
                }
            }
        }
    }
]
[root@i-id8g0yu9 ~]# cd /var/lib/docker/volumes/65adbd1e4997c8df05a05b30ab1a2c3df970e19b0dd38f579f428ec8442810b9/_data
[root@i-id8g0yu9 _data]# ls
b.txt
[root@i-id8g0yu9 _data]# cat b.txt  # 注意这里还有追加6666或者8888
2222
[root@i-id8g0yu9 _data]# 
相关文章
|
18天前
|
JavaScript 数据库 Docker
一个有用的docker entrypoint脚本的范例
通过这个 Entrypoint 脚本,您可以确保在 Docker 容器启动时执行必要的初始化步骤,并且容器可以根据不同的运行时环境进行相应的配置和操作。这个范例展示了如何编写一个灵活且功能强大的 Entrypoint 脚本,使您的 Docker 容器更加可靠和易于管理。
26 3
|
2月前
|
存储 关系型数据库 MySQL
|
2月前
|
Shell 应用服务中间件 nginx
docker学习--最详细的docker run 各子命令解释与应用
`docker run` 是 Docker 中用于启动容器的基本命令。常用子命令包括 `-i`(交互模式)、`-t`(分配终端)、`-d`(后台运行)、`-p`(端口映射)、`--name`(指定容器名)。例如,`docker run -it nginx:1.20 /bin/bash` 可以创建并进入交互式容器。使用 `-d` 可在后台运行容器,`-p` 可将容器端口映射到主机端口,`--name` 则用于自定义容器名称以便管理。
280 0
|
3月前
|
Docker 容器
docker run
【9月更文挑战第08天】
168 1
|
3月前
|
关系型数据库 MySQL 数据库
docker启动mysql多实例连接报错Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’
docker启动mysql多实例连接报错Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’
220 0
|
4月前
|
Docker 容器
在Docker中,Dockerfile有哪些常见指令?
在Docker中,Dockerfile有哪些常见指令?
|
4月前
|
安全 Ubuntu Linux
在Docker中,镜像内没有curl,kill,ipconfig等指令如何添加?
在Docker中,镜像内没有curl,kill,ipconfig等指令如何添加?
|
7月前
|
Linux Docker 容器
Docker Dockerfile 语法与指令
Docker Dockerfile 语法与指令
80 0
|
Linux Docker 容器
Docker Dockerfile 语法与指令
Docker Dockerfile 语法与指令
151 0
|
应用服务中间件 Linux Shell
Docker进阶 dockerfile指令构建docker镜像
Docker进阶 Dockerfile指令,编译dockerfile文件构建镜像,dockerfile常用指令,实操训练:Dockerfile构建Nginx镜像: FROM #指定基础镜像,一切从这里开始构建 MAINTAINER #镜像是谁写的,姓名+邮箱 RUN #镜像构建的时候需要运行的命令 ADD #步骤:tomcat镜像,这个tomcat压缩包!添加内容 WORKDIR #镜像的工作目录 VOLUME #挂载的目录 EXPOST #暴露端口配置
Docker进阶 dockerfile指令构建docker镜像