[Application] Docker DockerFile parsing (e)] [Application Docker containers data volume (D)

DockerFile Introduction

  DockerFile Docker is used to build the image file, the pace is a series of commands and configuration parameters

  Search on docker hub and view the centos DockerFile as follows:

    

  DockerFile Basics

  1, each instruction must be reserved words in uppercase and subtitles to follow behind at least one parameter  

  2, the instruction execution in top-down order

  3, # denotes a comment

  4, each instruction will create a new image layer, and mirror commit

  DockerFile build process

  1, docker run from a container base image

  2, and to execute an instruction to modify the container

  3, a similar operation is performed docker commit to submit a new image layer

  4, docker again based mirroring run just submitted a new container

  5, the next instruction executed dockerfile until completion of all instructions execute

  Case:

    You can refer to [Application] Docker containers data volume (D) , a data volume of the container to add two ways: using DockerFile add, compare build process of this procedure DockerFIle

DockerFile reserved words

  1、FROM

    Base image, a new image is based on the current mirror

  2、MAINTAINER

    Mirror defenders name and e-mail address

  3、RUN

    Command need to run container building

  4、EXPOSE

    External exposure to a current container port

  5、WORKDIR

    After re-create the specified container terminal logged in the default working directory

  6、ENV

    To set the environment variables in the construction process of the mirror

  7、ADD

    The host file in the directory image and copied into the ADD command processing URL and automatically extract the tar archive

  8、COPY

    Similarly file ADD, to the mirror copy of the document directory, the directory from the building context <source path> / directory to <destination path> in the new position of the mirror layer

    Use a: COPY src dest

    用法二:COPY ["src", "dest"]

  9、VOLUME

    Data volume container, for persistent data storage and work

  10、CMD

    Specify a command to run on startup container

    In Dockerfile can have multiple CMD command, but only the last one will work, CMD parameter will be replaced after the docker run

  11、ENTRYPOINT

    Specify a command to run on startup container

    ENTRYPOINT purpose and CMD, it is in the specified container and start the program parameters

  12、ONBUILD

    When building an inherited Dockerfile run the command, the father of the mirror is triggered after the quilt in his father's image of onbuild

  A case -Base mirror (scratch)

    Docker Hub 99% of the image are required by installing and configuring software in the mirror base out construct

1 FROM scratch

  Case B - Custom Mirror mycentos

    1, write DockerFile file

1  # centos mirror base image
 2  the FROM centos
 . 3  
. 4  # image defenders name and email address
 . 5 the MAINTAINER on H__D < H__D @ 163.com > 
. 6  
. 7  # environment variables
 . 8  ENV MYPATH / usr / local
 . 9  
10  # working directory
 . 11  the WORKDIR $ MYPATH
 12 is  
13 is  run Setup command vim construct #
 14  the rUN -Y yum the install vim
 15  
16  when run # build-mounted Tools NET
 . 17  the rUN yum the install -Y-NET Tools
 18 is  
. 19  # exposed outside the current container port
 20 is  eXPOSE 80
 21 is  
22 is  run command to start #
 23 CMD echo $MYPATH
24 
25 CMD echo "build success .............."
26 
27 CMD /bin/bash 

    2, building

      Format: Docker Build a new mirror -t name: TAG.

      命令:docker build -f dockerfile2 -t test/mycentos:1.2 .

      

      

    3, run

      Format: Docker RUN -it new image name: TAG

      Command: Docker Expediting IT Test RUN / mycentos: 1.2

      

    4, mirroring the change history list

      Format: Docker History mirror name: TAG

      Command: Docker History Test / mycentos: 1.2

      

  Case C -CMD / ENTRYPOINT Mirror Case

    When a container is specified command starts to run

    1, CMD: Dockerfile can have multiple CMD command, but only the last one will work, CMD parameter will be replaced after the docker run

      1) Create a dockerfile4, as follows:

1  # centos mirror base image
 2  the FROM centos
 . 3  
. 4  # running start command 
 . 5 the CMD [ "/ bin / echo", "the Hello"]
View Code

      2) By dockerfile4, constructing test / mycmd mirroring

        命令:docker build -f dockerfile4 -t test/mycmd .

      3) Testing

        Command: Docker -it the Test RUN / mycmd

        命令:docker run -it test/mycmd /bin/echo "ABC"

        

    2, ENTRYPOINT: you can append parameters

      1) Create a dockerfile3, as follows:

1  # centos mirror base image
 2  the FROM centos
 . 3  
. 4  Run # command starts 
 . 5  EntryPoint [ "/ bin / echo", "the Hello"]  
 . 6  
. 7  # Docker Expediting IT RUN [Image] After running output 
 . 8  # Hello World
 . 9  # Docker run -it [image] "command" output after running
 10 # Hello command line
View Code

      2) By dockerfile3, constructing test / myentrypoint mirroring

        命令:docker build -f dockerfile3 -t test/myentrypoint .

      3) Testing

        命令:docker run -it test/myentrypoint

        命令:docker run -it test/myentrypoint "ABC"

        

  D Case -ONBUILD

    ONBUILD: Run command when building an inherited Dockerfile, the father of the mirror is triggered after the quilt in his father's image of onbuild

    1, a new dockerfile5 file, as follows:

1  # centos basis Mirror Mirror
 2  the FROM centos
 3  
4  # mirrored quilt trigger
 5  ONBUILD RUN echo "now, Build Action IS the ok .............."
 6  
7  Run command to start #
 8 CMD [ "/ bin / bash"]

    2, by constructing dockerfile5 test / mycentos: 1.5 mirroring

      命令:docker build -f dockerfile5 -t test/mycentos:1.5 .

    3, a new file dockerfile6, the base image is a test / mycentos: 1.5, as follows:

1  # centos mirror base image
 2  the FROM Test / mycentos: for 1.5
 . 3  
. 4  Run # command starts
 . 5 the CMD [ "/ bin / the bash"] 

    4, constructed by dockerfile6 test / mycentos: 1.6 mirroring

      命令:docker build -f dockerfile6 -t test/mycentos:1.6 .

    

    Through observation, we can see the role of ONBUILD command

  E cases - custom images Tomcat9

    

 

    

 

Guess you like

Origin www.cnblogs.com/h--d/p/12571827.html