######################################################################################node
文章中有錯或者其餘問題請聯繫小弟:guiqiu_2010@163.compython
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++mysql
爲何要使用Dockerfilesql
Dockerfile 能搭建出一抹同樣的環境來,並且操做方便,簡單,環境一致docker
注:base p_w_picpaths需統一, 能夠從靈鵲雲,數字雲,阿里雲中直接pull。直接在官方dockerhub中pull實在是太慢了,還常常失敗shell
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++centos
Dockerfile 語法以下,跟咱們熟悉的shell同樣,是順序向下執行的。bash
Dockerfile 執行與注意點ssh
docker build . docker build -t centos:7 . docker build -t centos:7 -t centos:latest . docker build -f /path/docker/file/Dockerfile .
tips:ide
1. 整個build過程是由Docker daemon 控制的,並不是CLI 2. Dockerfile 儘可能別放到node的 '/' 目錄下,不然會將整個根目錄所有send到docker daemon 3. Docker build 過程當中每一行是獨立的(有點相似Makefile),所若是前面定義RUN cd /tmp ,後面定義 RUN touch file ,這個file必定不會在/tmp下面
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Environment replacement
Dockerfile 中的變量是有ENV定義。變量比較有意思,有好多中寫法,你們研究一下看:
1. $foo , ${foo} 3. \$foo ,\${foo} 4. ${fool_name} ,$fool_name, ${fool}_cont 5. ${variable:-word} , 若是$variable 沒有設置,那麼傳遞的值就是word 6. ${variable:+word} ,若是$variable 已經設置,那麼傳遞的值就是word
並不是全部的Dockerfile指令都能support ENV ,咱們看看有哪些可以support:
ADD,COPY,ENV,EXPOSE,LABEL,USER,WORKDIR,VOLUME,STOPSIGNAL
目前就只有這麼多了
注:在Dockerfile同一行中ENV環境變量是保持不替換的,什麼意思呢?咱們看個例子就很清楚了。
ENV abc=hello ENV abc=bye def=$abc ENV ghi=$abc
結果 def=hello, ghi=bye,
由於在第二個ENV中,abc變量雖然是有被從新賦值,可是由於跟def賦值是在同一行,因此告終果就是依然是上一行的值,是否是頗有趣:)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#Comment
#INSTRUCTION arguments
FROM <p_w_picpaths:tag> ex: FROM centos:6.6
若是沒有對應的p_w_picpaths,那麼會直接從公共的dockerhub中pull(固然直接dockerhub的server就是另一回事了)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
MAINTANER <name>
ex: MAINTAINER GuiQiu<guiqiu_2010@162.com>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
RUN <command>
兩種模式,一種是shell模式,這種模式等於說是/bin/sh -c command這樣執行
ex: RUN echo "$NAME is my friend" RUN yum install -y mysql \ python RUN /bin/bash -c 'source $HOME/.bashrc ;\ echo $HOME'
另外一種模式,exec模式 ,由於這種模式將option看成JSON array來parser的,因此必定是要使用雙引號的。
RUN ["executable", "param1", "param2"] ex: RUN ["/bin/bash", "-c", "echo hello"] RUN [ "echo", "$HOME" ]
在exec模式中,若是有變量,那麼在parser的時候,是不傳值的,要記得。
tips:在測試階段,Dockerfile一直頻繁的修改,因此咱們在build的時候,有時候不但願使用上一版或上幾版的cache,可使用下面的命令避免(我的感受還蠻有做用的)
docker build --no-cache
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
CMD ["executable","param1","param2"] (exec form, this is the preferred form) CMD ["param1","param2"] (as default parameters to ENTRYPOINT) CMD command param1 param2 (shell form)
ENTRYPOINT ["executable", "param1", "param2"] (exec form, preferred) ENTRYPOINT command param1 param2 (shell form)
用此option能夠避免使用Dockerfile中定義的ENTRYPOINT值:--entrypoint=flag
tips:
1. 若是CMD 是給ENTRYPOINT 提供參數的話,那麼必須使用JSON格式,也就是必需要雙引號(不是單引號)
2. ENTRYPOINT CMD 區別是 ENTRYPOINT的指令不會被user指定的命令overwrite,而是看成參數使用;CMD定義的指令會直接被overwrite
3. CMD RUN 的區別,RUN指令會在build過程當中指令且commit,然而CMD 卻只是定義到p_w_picpaths中,不會在build的過程當中執行,只是在container啓動的時候執行
4. 當container別當成一個執行檔來用的時候,比較難建議使用ENTRYPOINT + CMD 執行一些參數
5. Dockerfile 必須定義ENTRYPOINT CMD其中之一
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ADD/COPY ADD <src>... <dest> ADD ["<src>",... "<dest>"] COPY <src>... <dest> COPY ["<src>",... "<dest>"]
tips:
1. ADD / COPY 都是複製的做用,可是ADD 能夠直接複製url而COPY則須要先download下來而後才能複製,固然若是url中有須要密碼認證的話,仍是必須經過其餘的方式來先獲得內容才能複製
2. src的路徑必須包含在Dockerfile所在的路徑下
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
USER daemon
以什麼user來運行此container
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
WORKDIR path
container 開啓以後default路徑
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
[root@guiqiu-virtualbox dockerfile]# cat Dockerfile FROM centos:test MAINTAINER GUIQIU<guiqiu_2010@163.com> ENV FILENAME DOCKERFILE ENV MYNAME=zhangsan YOURNAME=lisi RUN yum install -y xclock RUN yum install -y openssh-server \ openssh-clients RUN ["mkdir", "-p" ,"/d/test"] RUN useradd zhangsan ADD README /root/ COPY README /d/test WORKDIR /d USER zhangsan CMD /bin/bash
[root@guiqiu-virtualbox dockerfile]# cat Dockerfile1 FROM centos:test MAINTAINER GUIQIU<guiqiu_2010@163.com> ENV FILENAME DOCKERFILE ENV MYNAME=zhangsan YOURNAME=lisi RUN yum install -y xclock RUN yum install -y perl \ openssh-server \ openssh-clients RUN ["mkdir", "-p" ,"/d/test"] ADD README /root/ COPY README /d/test WORKDIR /d CMD /bin/bash
[root@guiqiu-virtualbox dockerfile]# cat Dockerfile2 FROM centos:test MAINTAINER GUIQIU<guiqiu_2010@163.com> ENV FILENAME DOCKERFILE ENV MYNAME=zhangsan YOURNAME=lisi RUN yum install -y xclock RUN yum install -y perl \ openssh-server \ openssh-clients RUN ["mkdir", "-p" ,"/d/test"] ADD README /root/ COPY README /d/test WORKDIR /d ENTRYPOINT ["/bin/bash"]
[root@guiqiu-virtualbox dockerfile]# cat Dockerfile3 FROM centos:test MAINTAINER GUIQIU<guiqiu_2010@163.com> ENV FILENAME DOCKERFILE ENV MYNAME=zhangsan YOURNAME=lisi RUN yum install -y xclock RUN yum install -y perl \ openssh-server \ openssh-clients RUN ["mkdir", "-p" ,"/d/test"] ADD README /root/ COPY README /d/test WORKDIR /d ENTRYPOINT ["/usr/bin/echo","ThisIsTest"]