3. Docker learning, theoretical knowledge, the third day -- DockerFile

Keywords: Programming Docker curl CentOS yum

Docker learning

I. DockerFile analysis

1. What is DockerFile

DockerFile is a build file used to build Docker image. It is a script composed of a series of commands and parameters.

Three steps to build:
	1. Manually write a DockerFile file
	2. docker build to obtain a customized image
	3,docker run

2. Analysis of the construction process of DockerFile

Basic knowledge:
	2.1. Each reserved word instruction must be in capital letters, followed by at least one parameter.
	2.2. The well-known shall be executed from top to bottom.
	2.3 "and" means notes
	2.4. Each instruction will create a new image layer and submit the image.

General execution process:
	2.1. docker runs a container from the basic image (there must be a basic image)
	2.2. Execute an instruction and modify the container
	2.3. Perform an operation similar to docker commit to submit a new image layer
	2.4. docker runs a new container based on the image just submitted
	2.5. Execute the next instruction in the dockerfile until all the commands are completed.

① dockerfile is the raw material of the software = ========> ② docker image is the delivery of the software = === run = = > ③ docker container is a running state of the software

3. DockerFile reserved word instruction

3.1>,FROM	Basic image. The current new image is based on that image.
			FROM scratch
3.2>,MAINTAINER Image maintainer's name and email address
			MAINTAINER The CentOs Project <james_taylor@126.com>
3.3>,RUN  Commands to run when building containers
            RUN groupadd -r redis && useradd -r -g redis redis 
3.4>,EXPOSE  Port exposed by current container
            EXPOSE 6379
3.5>,WORKDIR  After creating the container, the terminal logs in the working directory by default, which is a foothold.
            WORKDIR	/data
3.6>,ENV  Used to set environment variables during image building
             ENV  MY_PATH /usr/mytest
              (This environment variable can be used in any subsequent RUN It can be used directly in other instructions, just like setting the prefix of environment variables before the command, such as WORKDIR $MY_PATH)
                
3.7>,ADD  Copy the files in the host directory into the image. ADD Command automatic processing URL Decompression tar Compressed package 
           ADD  centos-7-docker.tar.xz    
           (COPY Direct copy, ADD Copy+decompression)
3.8>,COPY  Similar ADD,Copy files and directories to the image             
           COPY src  desc  or COPY["src","desc"]
3.9>,VOLUME Container data volume for data preservation and persistence[HOST And container catalog file mapping]
			VOLUME  myFile:myFileContainer
3.10>,CMD  Specify a command to run when the container starts
            (DockerFile There can be more than one CMD Directive, but only the last one takes effect. CMD Will be docker run Parameter replacement after)
             shell Format: CMD <command>
             exec Format:  CMD ["Executable","Parameter 1","Parameter 2"]
                
          (Namely docker run -it centos /bin/bash Coverage dockerfile Document CMD Command)
3.11>,ENTRYPOINT Specify a command to run when the container starts
             (EBTRYPOINT Purpose and CMD The same is true for the specified container)
                (and CMD The difference is, CMD Will be overwritten. ENTRYPOINT Will be appended)
3.12>,ONBUILD  When building an inherited DockerFile Run the command when the parent image is inherited by the child. onbuild Triggered
                ONBUILD RUN echo "hello...world"
             (Similar to triggers, as long as a child image inherits'I',Will punish the order)
3.13>,LABEL Label

4. DockerFile user defined image myCentOS

Base image scratch, 99% of the images in DockerHub are built by installing and configuring the required software in the base image

Requirements: modifying centos Default path after login, support vim Editor, support ip addr View network configuration
	
//Code:
 4.1,Establish mycentos  DockerFile file
	FROM centos  ##Local centos image
	MAINTAINER mycentosmlee<james_taylor@126.com> ##Author introduction
	ENV  MYPATH /usr/local ##set variable
	WORKDIR $MYPATH  ##Enter tmp path after default login
	RUN yum -y install vim  ##Install vim
	RUN yum -y install net-tools ##Install network adapter
	EXPOSE 80  ##External exposure port 80
    CMD echo $MYPATH  ##Output mypath
    CMD echo "success.....ok" ##Output success... ok
	CMD /bin/bash ##Run / bin/bash
  4.2,structure  docker build -f dockerfile File path -t Name of the new image:TAG .  
        (TAG Version number.Current directory)
  4.3,docker images View newly created images
  4.4,docker run -it mycentos:1.3   
         (Test directory  ip addr  vim)
  4.5,docker history image ID      List the change history of the image

5. Command cases of CMD and ENTERPOINT of DockerFile

Both specify a command to run when the container starts

Memory:
3.10, CMD specifies a command to run when the container starts
            (there can be multiple CMD instructions in the DockerFile, but only the last one takes effect, and the CMD will be replaced by the parameters after docker run.)
             shell format: CMD < command >
             exec format: CMD ["executable", "Parameter1", "parameter2"]
                
          (that is, docker run -it centos /bin/bash will overwrite the CMD command in the dockerfile file)
3.11, ENTRYPOINT specifies a command to run when the container starts
             (the purpose of EBTRYPOINT is the same as that of CMD. It starts programs and parameters in the specified container.)
                (unlike CMD, CMD will be overwritten and ENTRYPOINT will be appended)

Case study:

CMD

In hub.docker.com, the last sentence of the tomcat dockerfile file is:
	CMD ["catalina.sh", "run"] (start tomcat)
When we test docker run -it tomcat ls -l, we find that the directory list of / usr/local/tomcat is listed
 docker ps found that tomcat was not running, proving that CMD ["catalina.sh", "run"] was not executed, but ls-l was executed.

ENTERPOINT

Test this first:
curl  http://ip.cn will output the page information of the website
curl  -s http://ip.cn
curl  -s -i http://ip.cn

1),Establish myCurlDockerFile Document, content:
	FROM centos
	RUN yum -y install curl
	CMD ["curl","-s","http://ip.cn"]
2),build File generation image
	docker build -f myCurlDockerFile -t myip .
3),run image
	docker run -it myip
4),test curl -s -i http://ip.cn
5),Add one test -i Parameters
	docker run -it myip -i
	//Similar to adding a line of cmd-i after CMD ["curl","-s","http://ip.cn]
6),take CMD Become ENTERPOINT
	FROM centos
	RUN yum -y install curl
	ENTERPOINT ["curl","-s","http://ip.cn"]
7),build File generation image
	docker build -f myCurlDockerFile2 -t myip2 .
8),run image
	docker run -it myip2
9),run image
	docker run -it myip2 -i
	//Similarly, parameters are added in entrpoint ["curl", "- s", "http://ip.cn]
	ENTERPOINT ["curl","-s","-i","http://ip.cn"]

6. The ONBUILD command case of DockerFile

3.12. ONBUILD. When building an inherited DockerFile, run the command. After the parent image is inherited by the child, the ONBUILD of the parent image is triggered.

Case study:

1,Establish myonbuild file
	FROM centos
	RUN yum -y install curl
	ENTERPOINT ["curl","-s","http://ip.cn"]
	ONBILD RUN echo "hello...world"
2,build Create a mirror and run
	docker build -f myonbuild -t myip_father .
	docker images
	docker run myip_father
3,Establish myonbuild2 file
	FROM myip_father
	RUN yum -y install curl
	CMD ["curl","-s","http://ip.cn"]
4,build Create a mirror and run
	docker build -f myonbuild -t myip_son .
	docker images
	docker run myip_son

Posted by hchsk on Thu, 24 Oct 2019 02:31:41 -0700