dockerfile

由一系列用于根据基础镜像构建新的镜像文件的专用指令序列组成;

指令字符不区分大小写,但约定俗成使用大写字符;

FROM指令:必须是第一个非注释行;用于指定用到的基础镜像;
一个dockerfile文件中可以有多个FROM指令,不过,尽量不要在一个dokcerfile文件中使用多个FROM指令;

FROM <image>[:tag]
FROM <image>@<digest>

FROM busybox:latest
FROM centos:6.8

MAINTAINER指令:提供维护者信息。不限制其出现的位置,建议放在FROM指令之后;

COPY指令:从宿主机复制文件至正在创建的镜像中;

COPY <src> <dest>
COPY ["<src>","<dest>"]         #文件名中有空格时使用这种格式;

<src>:要复制的原文件或目录,支持使用通配符;
<dest>:目标路径;建议使用绝对路径,否则相对于WORKDIR而言;

所有新复制生成的目录文件的UID和GID均为0;

COPY server.xml /etc/tomcat/server.xml
COPY *.conf /etc/httpd/conf.d/

<src>是dockerfile所在目录的相对路径;
<src>如果是目录,会自动进行递归复制;如果有多个<src>,包括在<src>上使用了通配符的情形,则<dest>必须是目录,且必须以/结尾;
<dest>目标路径不存在时,会被自动创建,包括其父目录;

ADD指令:类似于COPY指令,额外支持复制tar文件(会自动展开),以及URL路径(下载文件);

ADD <src> <dest>
ADD ["<src>","<dest>"] 

ADD haproxy.cfg /etc/haroxy/haroxy.cfg
ADD logstash_*.cnf /etc/logstash/
ADD http://www.keithtt.com/download/nginx/conf/nginx.conf /etc/nginx/

注意:以URL格式指定的源文件,下载后其目标文件的权限为600;
宿主机上的tar文件复制后会自动展开,但是,通过URL下载的文件如果是tar格式,则不会被展开;

ENV指令:定义环境变量,这些变量可被当前dockerfile文件中的其它指令直接调用,调用格式为$variable_name或${variable_name};

ENV <key> <value>       #一次定义一个变量;
ENV <key>=<value>       #一次可定义多个变量;如果<value>中有空白字符,要使用转义符进行转义;

ENV myName="Obama Clark" myDog=Hello Dog 
  myCat=Garfield        #换行不能顶格;

等同于:
ENV myName Obama Clark
ENV myDog Hello Dog
ENV myCat Garfield

ENV定义的环境变量在镜像运行的整个过程中一直存在,可以使用inspect命令查看,还可以在docker run启动此镜像时,使用--env选项来修改变量的值;

 -e, --env value:Set environment variables (default []). 

USER指令:指定运行dockerfile文件中RUN/CMD/ENTRYPOINT指令指定的程序时的用户名或UID;

USER <uid>
USER <username>

注意:<uid>应该使用已经存在的用户的UID,否则docker run会报错;

WORKDIR指令:后续命令的执行路径,用于为dockerfile中的RUN/CMD/ENTRYPOINT/COPY/ADD指令指定工作目录;

WORKDIR <dirpath>

WORKDIR可出现多次。也可以使用相对路径,表示相对于前一个WORKDIR指令指定的路径;还可以调用ENV定义的环境变量;

WORKDIR /var/log
WORKDIR $statepath

VOLUME指令:定义可以被挂载的目录,用于挂载主机上的卷或其它容器的卷;

VOLUME <mountpoint>
VOLUME ["<mountpoint>",...]

如果mountpoint路径下事先有文件存在,docker run命令会在卷挂载完成后将文件复制到挂载的卷中(实际上是unionfs文件叠加);

RUN指令:用于指定docker build过程中要运行的命令;

RUN <command>
RUN ["<executable>","<param1>","<param2>",...]
RUN ["/bin/bash","-c","<executable>","<param1>","<param2>",...]

RUN yum install -y iproute nginx && yum clean all

CMD指令:用于指定在docker run时运行的程序;

CMD <command> param1 param2      #以shell解释器运行;
CMD ["<executable>","<param1>","<param2>",...]        #不以shell解释器运行;程序PID为1;
CMD [<param1>","<param2>",...]        #为ENTRYPOINT指令指定的程序提供默认参数;

每个dockerfile中只能有一个CMD,当指定多个时,只有最后一个生效;
如果docker run启动容器时指定了运行的命令,则会覆盖掉CMD指定的命令;

CMD ["/usr/sbin/httpd","-c","/etc/httpd/conf/httpd.conf"]  

ENTRYPOINT指令:类似于CMD指令,但不会被docker run命令行参数指定运行的程序覆盖,而且这些命令行参数会被当作参数传递给ENTRYPOINT指定的程序;

ENTRYPOINT <command> param1 param2      
ENTRYPOINT ["<executable>","<param1>","<param2>",...]       

每个dockerfile中只能有一个ENTRYPOINT,当指定多个时,只有最后一个生效;

CMD ["-c"]
ENTRYPOINT ["top","-c"]

ENTRYPOINT ["/bin/echo"]
docker run -it imageecho "this is a test"
这里会输出"this is a test"字符串;

EXPOSE指令:指定容器要暴露的端口;

EXPOSE <port>[/<protocol>]

  <protocol>为tcp或udp,默认为tcp;

EXPOSE 11211/tcp 11211/udp

在启动容器时,需要使用-P或者-p参数。

ONBUILD指令:定义触发器;

当前dockerfile构建出的镜像被用作基础镜像去构建其它镜像时,ONBUILD指令指定的操作才会被执行;

ONBUILD <指令>

注意:ONBUILD不会触发FROM和MAINTAINER指令,每个dockerfile都应该有自己的FROM和MAINTAINER指令;    

示例一:

# mkdir dockerfile
# cd dockerfile
# vim test.dockerfile

  FROM busybox:latest
  MAINTAINER keith <keithtt@163.com>
  COPY index.html /web/html/index.html
  EXPOSE 80
  CMD ["httpd","-f","-h","/web/html"]

# vim index.html
  from test dockerfile                  

# docker build --help
  Build an image from a Dockerfile.
  -f, --file:Name of the Dockerfile (Default is 'PATH/Dockerfile').
  -t, --tag=[]:Name and optionally a tag in the 'name:tag' format.    

# docker build -f test.dockerfile -t busybox:web .
# docker run -d --name test -P busybox:web
# docker port test
  80/tcp -> 0.0.0.0:32768

用浏览器访问
http://121.42.184.143:32768/ 

示例二:

# mkdir docker-httpd
# cd  docker-httpd/
# vim httpd.dockerfile

  FROM centos:latest
  MAINTAINER keithtt <329640305@qq.com>
  RUN yum install -y httpd php php-mysql php-mbstring && yum clean all && echo -e '<?php
	phpinfo();
?>' > /var/www/html/index.php
  EXPOSE 80
  CMD ["/usr/sbin/httpd","-f","/etc/httpd/conf/httpd.conf","-DFOREGROUND"]

# docker build -f httpd.dockerfile -t httpd:test .  

# docker images
# docker run -d --name httpd -p 80:80 httpd:test
# docker ps  

用浏览器访问
http://121.42.184.143/

示例三:

# vim pma.dockerfile

  FROM centos:latest
  MAINTAINER keithtt <329640305@qq.com>
  RUN yum install -y httpd php php-mysql php-mbstring && yum clean all && echo -e '<?php
	phpinfo();
?>' > /var/www/html/index.php
  EXPOSE 80
  VOLUME /var/www/html
  CMD ["/usr/sbin/httpd","-f","/etc/httpd/conf/httpd.conf","-DFOREGROUND"]

# docker build -f pma.dockerfile -t httpd:pma .

# wget https://files.phpmyadmin.net/phpMyAdmin/4.4.15.5/phpMyAdmin-4.4.15.5-all-languages.zip
# unzip phpMyAdmin-4.4.15.5-all-languages.zip
# mkdir /web/html
#mv phpMyAdmin-4.4.15.5-all-languages /web/html/pma
# cd /web/html/pma
# cp config.sample.inc.php config.inc.php

# openssl rand --help
# openssl rand -base64 20
  qW2C0TFAXu3S5FXV3TBgwSv5Cn4
# vim /web/html/pma/config.inc.php
  $cfg['blowfish_secret'] = 'qW2C0TFAXu3S5FXV3TBgwSv5Cn4';  
  $cfg['Servers'][$i]['host'] = '121.42.184.143';

# yum install -y mariadb-server
# vim /etc/my.cnf.d/server.cnf
  [mysqld]
  skip_name_resolve = on
  innodb_file_per_table = on
# systemctl start mariadb

# mysql
>grant all on *.* to 'root'@'%' identified by 'keithtt';
>flush privileges;
>exit;  

# docker run -d --name pma -p 80:80 -v /web/html:/var/www/html httpd:pma

用浏览器访问
http://121.42.184.143/pma/
原文地址:https://www.cnblogs.com/keithtt/p/6819379.html