docker series of the Seven Dockerfile

Dockerfile

A. What is Dockerfile

    Dockerfile docker in the image file is a description file, said bluntly point is the image file in the end is what constituted a step by step. For example, we buy on Taobao one item, but this product requires assembly before use, so the seller gave you a drawing, you step by step in accordance with the drawings assembled, then it becomes the way you need. So Dockerfile is this drawing, the image file is the product you need. Dockerfile can easily naming names, but do not suggest you do this, or use in accordance with specifications, the first letter capitalized. Here are the first few chapters we use to centos example, to see its Dockerfile is kind of how, as shown below:     

                               

Two. Dockerfile, mirroring, the container

    Dockerfile: The build file is a mirror, the mirror is a step by step description of how to come.

  Mirror: is made out by Dockerfile, including basic operating system files and software operating environment, which uses hierarchical storage.

  Container: the mirror is up and running, simple to understand, Docker mirror equivalent program, container equivalent process.

III. The basic syntax and implementation process of Dockerfile

The basic syntax of 3.1 Dockerfile

   . A Each word must be reserved at the beginning of each line can be uppercase lower case, but it is recommended capital;

  . B instructions in the order, from the down sequentially performed;

  . C # represents a comment;

  d. Each instruction execution will create a new image, and commit.

3.2 Dockerfile implementation process

   a.docker performed in a container from the base image; 

   . B performing an image modification instruction;

   . C execute docker commit command to submit a new mirror;

   . D based on a new container in the Mirror operation just committed;

   E. Dockerfile next instruction is executed in accordance with b, c, d are sequentially cycle continues until all of the instruction completes.

3.3 presentation to explain

FROM centos                              #指定要生成的镜像的基础镜像,开头第一句话必须也只能是FROM
MAINTAINER  zhengjingmao@163.com         #指定作者是谁
RUN yum install -y vim                   #执行 yum install -y vim 命令,安装vim
RUN yum install -y net-tools             #执行 yum install -y net-tools, 安装net-tools工具
WORKDIR /dev/ #启动容器后,如果启动交互模式,直接进入到哪个目录 CMD [
"/bin/bash"] #启动容器的时候,进入到/bin/bash这种命令行

    如上代码所示,FROM、MAINTAINER、RUN、WORKDIR、CMD均为关键字,按照标准的规范需要大写;FROM centos表示我们的基础镜像是centos,然后会启动centos这个容器,执行第二行代码又会生成新的镜像文件,然后提交,周而复始。

四. Dockerfile关键字

关键字 作用
FROM 指定基础镜像
MAINTAINER 作者的信息
RUN 执行什么命令
EXPOSE 容器对外暴露的端口
WORKDIR 进入到容器后进入到哪个目录
ENV 配置环境变量
ADD 将文件拷贝到镜像中并解压
COPY 将文件拷贝到镜像中
VOLUME 配置数据卷
CMD 容器启动时候执行的命令
ENTRYPOINT 容器启动时候执行的命令
ONBUILD  

      a.ADD指令,我们现在定义这样一个Dockerfile,代码如下所示:

FROM centos
MAINTAINER  zhengjingmao@163.com
RUN mkdir /datas
ADD jdk-8u60-linux-x64.tar.gz /datas/
WORKDIR /datas/
CMD ["/bin/bash"]

     首先必须将jdk-8u60-linux-x64.tar.gz文件拷贝到Dockerfile同级目录下,如下图所示:

                                                        

     执行命令:docker build -t mycentos .  构建mycentos镜像,如下图所示:

                                                          

      然后启动容器,进入到 /datas/目录下,会发现 jdk1.8.0_60 目录,表示add命令还执行了解压命令,如下图所示:

                                                        

  b.COPY命令,我们定义一个Dockerfile, 代码如下:

FROM centos
MAINTAINER  zhengjingmao@163.com
RUN mkdir /datas
ADD jdk-8u60-linux-x64.tar.gz /datas/
WORKDIR /datas/
CMD ["/bin/bash"]

      执行命令:docker build -t mycentos .  构建mycentos镜像。

    然后启动容器,进入到 /datas/目录下,会看到jdk-8u60-linux-x64.tar.gz文件,并没有解压,如下图所示:

                                                       

 

 

Guess you like

Origin www.cnblogs.com/miller-zou/p/11111756.html