######################################################################################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"]