dockerfile指令說明

  • 第一條指令必須指定基礎(chǔ)鏡像
    FROM imageA:tag

  • RUN 指定運行的命令
    一般是基于基礎(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 parames

  • LABEL 添加鏡像標(biāo)簽
    LABEL key=value key2=value2

  • MAINTAINER指定作者
    MAINTAINER author

  • EXPOSE 對外端口
    EXPOSE端口指定的是外部訪問該容器的端口,與主機(jī)端口無關(guān),如需要添加映射關(guān)系,使用-P參數(shù)
    EXPOSE 7171

  • 設(shè)置環(huán)境變量
    ENV key=value key2=value2

  • ADD 添加文件到鏡像中
    可以使用容器內(nèi)的絕對路徑,也可以使用相對路徑;file的格式原則上是不限制的,使用過程中可以根據(jù)實際情況評估
    ADD filename/url path

  • COPY 拷貝文件
    COPY filename path

  • ENTERPOINT
    啟動時的默認(rèn)命令,用法與RUN和CMD一樣
    如果同時寫了ENTERPOINT和CMD,二者可能會相互覆蓋,,?

  • VOLUME掛載
    一般多設(shè)置于需要持久化存儲

  • USER容器啟動用戶
    指定運行命令的層,執(zhí)行命令的身份
    USER user_name

  • WORKDIR 工作目錄
    如果不存在會創(chuàng)建;存在以后多設(shè)置幾次也ok
    WORKDIR path

  • ARG設(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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容