dockerfile的编写参数
Posted rdchenxi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了dockerfile的编写参数相关的知识,希望对你有一定的参考价值。
注意细节
“#”号开头是注释 ,指令不区分大小写,顺序执行
FROM 指定基础镜像;注意必须是文件里第一个非注释行 ENV name 值 设置变量,注意没有=号 变量引用 ${name:-chenxi} 假如变量为空chenxi就是变量的默认值 [root@master ~]# echo ${name:-chenxi} chenxi
FROM 语法介绍
FROM <指定docker镜像名>[:<tag>] 标签不加默认是latest FROM <指定docker镜像名>@哈希码
MAINTANIER
MAINTANIER #作者的信息
LABEL语法
LABEL 指定元数据信息 key=值 key=值
COPY 用于从docker 主机复制文件至创建的新镜像文件语法
COPY 源文件或目录 目标目录 COPY ["源文件" "目标目录"] 注意 路径中有空白符通常使用第二个方法 文件复制准则 源文件必须是build上下文中的路径,不能是其父目录的文件 如果源是目录,则其内部文件或者子目录会被递归复制,但源目录自身不会被复制 如果指定多个源文件,或者在指定源文件使用了通配符,则目标必须是个目录,且以/结尾 如果目标不存在,它则会被自动创建,这包括其父目录路径
示例
[root@master docker]# cat Dockerfile FROM busybox COPY index.html /data/index.html [root@master docker]# echo "chwxxi" > index.html [root@master docker]# docker build -t chenxi:v01 ./ Sending build context to Docker daemon 3.072kB Step 1/2 : FROM busybox ---> 020584afccce Step 2/2 : COPY index.html /data/index.html ---> a39f70f77f61 Successfully built a39f70f77f61 Successfully tagged chenxi:v01 [root@master docker]# docker run --name cx --rm chenxi:v01 cat /data/index.html chwxxi
ADD 指令类似于COPY指令,支持使用tar文件和URL路径语法
ADD 源文件 目标 ADD ["源文件" "目标文件"] 操作准则 如果源文件为ULR切目标文件不以/结尾,则源文件指定的文件将被下载并直接创建为目标文件 ;如果目标文件以/结尾,则文件名直接下载到目标/文件 如果源文件是本地文件系统上的压缩格式的tar文件,它将被展开为一个目录,其行为类似于tar -x 命令,然而通过ULR获取的tar文件将不会被展开 如果<源文件>有多个,或其间接直接使用通配符,则目标文件必须是一个以/结尾的目录路径,如果目标文件不以/结尾的,则其是普通文件。源文件的内容将被直接写入目标文件里
示例
[root@master docker]# cat Dockerfile FROM busybox COPY index.html /data/index.html ADD http://download.redis.io/releases/redis-5.0.7.tar.gz /data/ [root@master docker]# docker build -t redis:v01 ./ Sending build context to Docker daemon 1.988MB Step 1/3 : FROM busybox ---> 020584afccce Step 2/3 : COPY index.html /data/index.html ---> Using cache ---> a39f70f77f61 Step 3/3 : ADD http://download.redis.io/releases/redis-5.0.7.tar.gz /data/ Downloading [==================================================>] 1.984MB/1.984MB ---> 50adfdcc6117 Successfully built 50adfdcc6117 Successfully tagged redis:v01 [root@master docker]# docker run --name cx --rm redis:v01 ls /data/ index.html redis-5.0.7.tar.gz
示例-2
[root@master docker]# cat Dockerfile FROM busybox COPY index.html /data/index.html ADD redis-5.0.7.tar.gz /data/redis/ [root@master docker]# ls Dockerfile index.html redis-5.0.7.tar.gz [root@master docker]# docker build -t redis:v02 ./ Sending build context to Docker daemon 1.988MB Step 1/3 : FROM busybox ---> 020584afccce Step 2/3 : COPY index.html /data/index.html ---> Using cache ---> a39f70f77f61 Step 3/3 : ADD redis-5.0.7.tar.gz /data/redis/ ---> cbbee8a281ad Successfully built cbbee8a281ad Successfully tagged redis:v02 [root@master docker]# docker run --name cx --rm redis:v02 ls /data/redis redis-5.0.7
WORKDIR指定工作目录
用于设定Dockerfil中所有的RUN、CMD、ENTRYPOINT、COPY和ADD设定工作目录 WORKDIR 路径
示例
[root@master docker]# cat Dockerfile FROM busybox COPY index.html /data/index.html WORKDIR /usr/local/src/ ADD redis-5.0.7.tar.gz ./ [root@master docker]# ls Dockerfile index.html redis-5.0.7.tar.gz [root@master docker]# docker build -t redis:v03 ./ Sending build context to Docker daemon 1.988MB Step 1/4 : FROM busybox ---> 020584afccce Step 2/4 : COPY index.html /data/index.html ---> Using cache ---> a39f70f77f61 Step 3/4 : WORKDIR /usr/local/src/ ---> Running in d2fca8af31a5 Removing intermediate container d2fca8af31a5 ---> 91323c9b4a85 Step 4/4 : ADD redis-5.0.7.tar.gz ./ ---> b2a20e26e004 Successfully built b2a20e26e004 Successfully tagged redis:v03 [root@master docker]# docker run --name cx --rm redis:v03 ls /usr/local/src/ redis-5.0.7
VOLUME 创建挂载点目录
用于在镜像里创建一个挂载点目录,用于挂着Dockre主机上的卷或其他容器上的卷 VOLUME 目录 如果挂载点目录路径下此文件存在,docker run 会在卷挂载完成后将此前文件复制到新卷里
示例
[root@master docker]# cat Dockerfile FROM busybox COPY index.html /data/index.html WORKDIR /usr/local/src/ ADD redis-5.0.7.tar.gz ./ VOLUME /data/mysql/ [root@master docker]# docker build -t mysql:v01 ./ Sending build context to Docker daemon 1.988MB Step 1/5 : FROM busybox ---> 020584afccce Step 2/5 : COPY index.html /data/index.html ---> Using cache ---> a39f70f77f61 Step 3/5 : WORKDIR /usr/local/src/ ---> Using cache ---> 91323c9b4a85 Step 4/5 : ADD redis-5.0.7.tar.gz ./ ---> Using cache ---> b2a20e26e004 Step 5/5 : VOLUME /data/mysql/ ---> Running in 8e97168b8a38 Removing intermediate container 8e97168b8a38 ---> 36ae8d2ba3b7 Successfully built 36ae8d2ba3b7 Successfully tagged mysql:v01 [root@master docker]# docker run --name cx --rm mysql:v01 mount | grep /data/mysql /dev/mapper/centos-root on /data/mysql type xfs (rw,relatime,attr2,inode64,noquota) EXPOSE 指令 用于为容器打开指定要监听端口已实现与外部通信一次可以暴露多个端口 语法 EXPOSE 11211/tcp 11211/udp 示例 [root@master docker]# cat Dockerfile FROM busybox COPY index.html /data/index.html WORKDIR /usr/local/src/ ADD redis-5.0.7.tar.gz ./ VOLUME /data/mysql/ EXPOSE 80/tcp [root@master docker]# docker build -t http:v.01 ./ Sending build context to Docker daemon 1.988MB Step 1/6 : FROM busybox ---> 020584afccce Step 2/6 : COPY index.html /data/index.html ---> Using cache ---> a39f70f77f61 Step 3/6 : WORKDIR /usr/local/src/ ---> Using cache ---> 91323c9b4a85 Step 4/6 : ADD redis-5.0.7.tar.gz ./ ---> Using cache ---> b2a20e26e004 Step 5/6 : VOLUME /data/mysql/ ---> Using cache ---> 36ae8d2ba3b7 Step 6/6 : EXPOSE 80/tcp ---> Running in be421f20d0e8 Removing intermediate container be421f20d0e8 ---> 578864a06f74 Successfully built 578864a06f74 Successfully tagged http:v.01 [root@master docker]# docker run --name web --rm http:v.01 /bin/httpd -h /data -f -f是前台运行 [root@master ~]# docker inspect web [ { "Id": "c6d225297b350edc4a58102d61fcdaa823b3e08892b52f397b64acf02acb1be0", "Created": "2019-11-23T02:33:08.788432916Z", "Path": "/bin/httpd", "Args": [ "-h", "/data", "-f" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 10800, "ExitCode": 0, "Error": "", "StartedAt": "2019-11-23T02:33:09.391066632Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:578864a06f740fe342194424148a0ec1b77f942c254b6442bc7a5c8af4ae4546" , "ResolvConfPath": "/var/lib/docker/containers/c6d225297b350edc4a58102d61fcdaa823b3 e08892b52f397b64acf02acb1be0/resolv.conf", "HostnamePath": "/var/lib/docker/containers/c6d225297b350edc4a58102d61fcdaa823b3e0 8892b52f397b64acf02acb1be0/hostname", "HostsPath": "/var/lib/docker/containers/c6d225297b350edc4a58102d61fcdaa823b3e0889 2b52f397b64acf02acb1be0/hosts", "LogPath": "/var/lib/docker/containers/c6d225297b350edc4a58102d61fcdaa823b3e08892b 52f397b64acf02acb1be0/c6d225297b350edc4a58102d61fcdaa823b3e08892b52f397b64acf02acb1be0-json.log", "Name": "/web", "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": true, "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/7bf24b175acfc5ffff3ddd52aaf9b715ecd2 715da94798e3f0de3cccaf1cef2d-init/diff:/var/lib/docker/overlay2/cd4704ddb683f96a444128da1b375308e61739968544717bc0fb4cc25df3408f/diff:/var/lib/docker/overlay2/98dd3838dda54c72e44f932f27b3e6b838fe11093083391d265042c840adceed/diff:/var/lib/docker/overlay2/56bd207136154521127d96aabbd7823be2334cc15e3ed271396aca6e6ad58de0/diff:/var/lib/docker/overlay2/5cad835aa43c37ba0d434e3775feeb06fe1759f8749d4e8081f186d805b0ad8e/diff", "MergedDir": "/var/lib/docker/overlay2/7bf24b175acfc5ffff3ddd52aaf9b715ecd 2715da94798e3f0de3cccaf1cef2d/merged", "UpperDir": "/var/lib/docker/overlay2/7bf24b175acfc5ffff3ddd52aaf9b715ecd2 715da94798e3f0de3cccaf1cef2d/diff", "WorkDir": "/var/lib/docker/overlay2/7bf24b175acfc5ffff3ddd52aaf9b715ecd27 15da94798e3f0de3cccaf1cef2d/work" }, "Name": "overlay2" }, "Mounts": [ { "Type": "volume", "Name": "b855f89205224a970e2a02c5e6404aba57c1774c363256a33c88b14d8304dd35" , "Source": "/var/lib/docker/volumes/b855f89205224a970e2a02c5e6404aba57c1774 c363256a33c88b14d8304dd35/_data", "Destination": "/data/mysql", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ], "Config": { "Hostname": "c6d225297b35", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": true, "AttachStderr": true, "ExposedPorts": { "80/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "/bin/httpd", "-h", "/data", "-f" ], "Image": "http:v.01", "Volumes": { "/data/mysql/": {} }, "WorkingDir": "/usr/local/src", "Entrypoint": null, "OnBuild": null, "Labels": {} }, "NetworkSettings": { "Bridge": "", "SandboxID": "32d53eb902a2b797942ad31bf990220c0c63dc62c6397ac87beb5c1d6dae503f ", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": { "80/tcp": null }, "SandboxKey": "/var/run/docker/netns/32d53eb902a2", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "0bf9b2b05bc76b526798b2334957c4a9fb2a279c449e25a8bfef974d578fa3c 7", "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": "20670e48b3f87b53f0426998127e8fd0658e61e8e01ace3bf7cb041a ad5bd427", "EndpointID": "0bf9b2b05bc76b526798b2334957c4a9fb2a279c449e25a8bfef974 d578fa3c7", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.2", IP "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:02", "DriverOpts": null } } } } ] [root@master ~]# curl 172.17.0.2 chwxxi [root@master docker]# docker run --name web -P --rm http:v.01 /bin/httpd -h /data -f -P 随机在docker主机上暴露一个端口 [root@master docker]# docker port web 80/tcp -> 0.0.0.0:32768 [root@slave1 ~]# curl 192.168.10.5:32768 chwxxi
ENV 设置环境变量
用于为镜像定义所需的环境变量,并被Dockerfile文件中位于其后指令所调用 调用格式 $name 定义格式 ENV name 值 或者 name = 值 第一个只能设置一个变量,第二个可以同时设置多个变量
示例
[root@master docker]# cat Dockerfile FROM busybox ENV DOCKER_ROOT=/data/index.html COPY index.html $DOCKER_ROOT WORKDIR /usr/local/src/ ADD redis-5.0.7.tar.gz ./ VOLUME /data/mysql/ EXPOSE 80/tcp [root@master docker]# docker build -t http:v.02 ./ Sending build context to Docker daemon 1.988MB Step 1/7 : FROM busybox ---> 020584afccce Step 2/7 : ENV DOCKER_ROOT=/data/index.html ---> Running in c36156ec361f Removing intermediate container c36156ec361f ---> b9a06f515829 Step 3/7 : COPY index.html $DOCKER_ROOT ---> d9ddba9390da Step 4/7 : WORKDIR /usr/local/src/ ---> Running in 7c7c7843262e Removing intermediate container 7c7c7843262e ---> 5bcade248353 Step 5/7 : ADD redis-5.0.7.tar.gz ./ ---> 53d3ffc442eb Step 6/7 : VOLUME /data/mysql/ ---> Running in a0fefc923333 Removing intermediate container a0fefc923333 ---> cae7c094e37d Step 7/7 : EXPOSE 80/tcp ---> Running in 5f1721b1e675 Removing intermediate container 5f1721b1e675 ---> d15cd197fe0a Successfully built d15cd197fe0a Successfully tagged http:v.02 [root@master docker]# docker run --name web --rm http:v.02 cat /data/index.html chwxxi [root@master docker]# docker run --name web --rm http:v.02 printenv PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=0f82ee34454c DOCKER_ROOT=/data/index.html HOME=/root
RUN 是构建镜像过程是运行的命令
RUN 指令介绍 用于指定docker build过程中运行的程序,可以任何命令 RUN 命令 或 RUN [命令,参数] 第一种格式中,是shll 的命令。且以"/bash/sh -c"来运行它,这就意味着词进程在容器里PID不能为1,不能接受Unix信号,当使用docker stop 命令停止时此进程不会接受SIGTERM信号的 第二种格式中的参数是一个JSON格式的数组,命令后面跟的参数,不能使用通配符
CMD指定容器默认启动时的命令程序;
ENTRYPOINT
类似CMD指令的功能,用于为容器指定默认的运行程序,从而使容器像是一个单独的可执行的程序 与CMD不同的是,有ENTRYPOINT启动的程序不会被docker run 命令指定的参数覆盖,而且这些命令行参数会被当做参数传递给ENTRYPOINT指定的程序的;不过docker run 有--entrypoint选项参数可以覆盖ENTRYPOINT指定的程序的 格式 ENTRYPOINT 命令 docker run 命令传入的参数会覆盖CMD指令的内容并且附加到ENIRYPOINT命令最后作为参数使用 使用““””
HEALTHCHECK检查容器是否健康
HEALTHCHECK 语法 --interval=数字m 默认30秒 每隔多久检测一次 --timeout=数字m 默认30秒 检测时超时时间 --start-period=数字m 默认0秒 容器启动会多久检测 --retries=数字 检测几次失败后定为失败 CMD 检测命令 HEALTHECK --interval=数字m --timeout=数字m --retries=数字 CMD curl -f http://IP||exit 1
以上是关于dockerfile的编写参数的主要内容,如果未能解决你的问题,请参考以下文章
初识Dockerfile+进阶Dockerfile(手把手叫你如何编写Dockerfile)