第一條指令必須指定基礎(chǔ)鏡像
FROM imageA:tagRUN 指定運行的命令
一般是基于基礎(chǔ)容器,搭建基礎(chǔ)環(huán)境的操作,屬于build image的時候操作的;使用RUN指令的時候,最好不要寫多層RUN,使用下面一種形式,\分隔換行繼續(xù)寫就行了,不然層級增加,不利于鏡像可讀性和簡約性
RUN direct-command
RUN ["executed-file","param1","param2"]CMD 指令只出容器啟動時需要的命令
與RUN指令的使用實際不同;這里使用可執(zhí)行文件傳參數(shù)的時候,要注意使用雙引號,參數(shù)傳遞以后,docker會解析成json串
CMD ["executed-file","param1","param2"]
CMD direct-command paramesLABEL 添加鏡像標(biāo)簽
LABEL key=value key2=value2MAINTAINER指定作者
MAINTAINER authorEXPOSE 對外端口
EXPOSE端口指定的是外部訪問該容器的端口,與主機(jī)端口無關(guān),如需要添加映射關(guān)系,使用-P參數(shù)
EXPOSE 7171設(shè)置環(huán)境變量
ENV key=value key2=value2ADD 添加文件到鏡像中
可以使用容器內(nèi)的絕對路徑,也可以使用相對路徑;file的格式原則上是不限制的,使用過程中可以根據(jù)實際情況評估
ADD filename/url pathCOPY 拷貝文件
COPY filename pathENTERPOINT
啟動時的默認(rèn)命令,用法與RUN和CMD一樣
如果同時寫了ENTERPOINT和CMD,二者可能會相互覆蓋,,?VOLUME掛載
一般多設(shè)置于需要持久化存儲USER容器啟動用戶
指定運行命令的層,執(zhí)行命令的身份
USER user_nameWORKDIR 工作目錄
如果不存在會創(chuàng)建;存在以后多設(shè)置幾次也ok
WORKDIR pathARG設(shè)置變量
ONBUILD 基于當(dāng)前鏡像生成的子鏡像才會有的動作
例如ONBUILD CMD direct-command parames這種STOPSIGNAL 容器退出時的系統(tǒng)指令
HEALTHCHECK 健康檢查
>> 示例<<
FROM centos:7.1
MAINTAINER M_G
LABEL im_name="my_centos7"\
im_desc="centos7.1byM_G"\
im_version="1.0"
RUN /bin/echo "Testing-RUN1"\
/bin/echo "Testing-RUN2"\
/bin/echo "Testing-RUN3"\
/bin/echo "The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile"
RUN ["/bin/echo", "-c", "OtherMeth"]
EXPOSE 7788
CMD /bin/echo "CMD"
>> 示例運行結(jié)果 <<
docker build -t runtest/centos:7.1 .
Sending build context to Docker daemon 2.048kB
Step 1/7 : FROM centos:7.2.1511
---> 9aec5c5fe4ba
Step 2/7 : MAINTAINER M_G
---> Running in fd9773ab5d04
Removing intermediate container fd9773ab5d04
---> 5d049cf44ade
Step 3/7 : LABEL im_name="my_centos7" im_desc="centos7.1byM_G" im_version="1.0"
---> Running in dbe78be6937a
Removing intermediate container dbe78be6937a
---> 835de35557c0
Step 4/7 : RUN /bin/echo "Testing-RUN1" /bin/echo "Testing-RUN2" /bin/echo "Testing-RUN3" /bin/echo "The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile"
---> Running in 200e155ce261
Testing-RUN1 /bin/echo Testing-RUN2 /bin/echo Testing-RUN3 /bin/echo The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile
Removing intermediate container 200e155ce261
---> 6b20860bc949
Step 5/7 : RUN ["/bin/echo", "-c", "OtherMeth"]
---> Running in 39064900f238
-c OtherMeth
Removing intermediate container 39064900f238
---> 47e32cba5448
Step 6/7 : EXPOSE 7788
---> Running in c658c7ba76ea
Removing intermediate container c658c7ba76ea
---> e76255a7950f
Step 7/7 : CMD /bin/echo "CMD"
---> Running in 9ab1ff69c36d
Removing intermediate container 9ab1ff69c36d
---> 91f66f06b528
Successfully built 91f66f06b528
Successfully tagged runtest/centos:7.1
完成以上指令,docker images已經(jīng)創(chuàng)建成功,測試一下:
docker run -t -i runtest/centos:7.1 /bin/echo "TEST"
# output
TEST