Getting Started with Docker - 4. Docker's dockerFile

Preface
——————————————————————————————————————
The basis for installing docker in this article is the Linux centos7 system, using terminal simulation Software xshell, if you need an installation tutorial, you can visit my previous blog to
download VMware, install and create a virtual machine.
VMware installs Centos7. The ultimate step-by-step detailed graphic process.
Contents of the previous section↓
Docker zero-based introduction - 3. Docker data volume and data volume container

1.Concepts and rules of dockerFile

We didn’t mention dockerfile in the previous concept article because it is relatively simple from a conceptual perspective. The difficulty lies in the writing and arrangement of its instructions. Here we have a general understanding.
Dockerfile is actually a build file used to build a docker image. It is a script composed of a series of commands and parameters.
The code tab of the corresponding container that we see on the docker hub official website contains the content of the dockerfile.
For example, for tomcat, you can directly view its dockerfile script code on the official website.
Insert image description here
Basic use of dockerfile scriptruleYes
→ 1. Each line of reserved words must be capitalized
→ 2. Followed by at least one parameter
→ 3. The instructions are executed sequentially from top to bottom
→ 4. The pound sign is a comment line
→ 5. Each instruction will create a new image layer , and submit the image


2.dockerFile instructions

There are many instructions. Let’s pick some simple and commonly used ones for explanation.

  1. The FROM instruction
    is used to specify the base image generated by the current image. It must be the first instruction. We know that the image has multiple readable layers. For example, the tomcat image is based on openJDK, because tomcat is developed in java and requires a java running environment. .
语法:
	FORM <image> 或 FORM <image>:<tag>
举例:
	FROM centos:7
表明此镜像基于centos:7镜像
  1. The MAINTAINER command
    is used to specify the maintainer's information, which is equivalent to a note. Generally, the developer's email address, name, etc. are written.
语法:
	MAINTAINER <...>
举例:
	MAINTAINER The CentOS Project <[email protected]> - ami_creator
  1. RUN command
    is a command that is automatically called when the container is built.
语法:
	RUN <command> 或 RUN ["命令", 参数1, 参数2]
举例:
	RUN mkdir -p "$CATALINA_HOME"
  1. The EXPOSE directive
    is used to specify the default port number for service calls (you can use -p or -P to specify the corresponding external access port when the container is started)
语法:
	EXPOSE <port> [<port>...]
举例:
	EXPOSE 8080
我们的tomcat就是暴露8080端口的
  1. WORKDIR command
    After the container is created, the terminal will log in to the path of the working directory by default, which is the directory where we entered the container for the first time.
语法:
	WORKDIR <path>
举例:
	WORKDIR $CATALINA_HOME
  1. The ENV directive (short for environment)
    is used to specify environment variables, which are used by subsequent commands and are maintained when the container is running.
语法:
	ENV <name> <value>
举例:
	ENV CATALINA_HOME /usr/local/tomcat
  1. The ADD instruction
    is used to copy the contents of the host to the container.
    It can be a relative path to the directory where the dockerfile is located, a URL,
    or a tar file, which will be [automatically decompressed] into a directory.
语法:
	ADD <src> <dest>
举例:
	ADD centos-7-x86_64-docker.tar.xz /
拷贝.tar到容器根目录并解压
  1. The COPY instruction
    copies files/directories from the <source path> in the build context directory to the directory <target path> of the new image container. It is
    similar to ADD, but this is just a simple copy without decompression.
语法:
	COPY src dest 或 COPY ["src", "dest"]
举例:
	COPY centos-7-x86_64-docker.tar.xz /
拷贝.tar到容器根目录(不会自动解压)
  1. The VOLUME instruction
    is used in the previous section. It is used to create data volumes. If there are multiple instructions, separate them with commas. It can be summarized as follows: Create a mount point that can be mounted from the local host or other containers. It is generally used to store databases. and the data that needs to be retained
语法:
	VOLUME ["/data" ...]
举例:
	VOLUME ["/dataVolumeContainer1", "/dataVolumeContainer2"]
  1. CMD command
    is the command executed when the container starts. If the user specifies a running command, it will overwrite the CMD command. This is the default value of the command specified when we run the container.
语法有三种格式:
	<1> CMD ["executable","参数1","参数2"...] 使用exec执行,推荐使用
	<2> CMD command 参数1 参数2... 在/bin/sh中执行,提供给需要交互的使用
	<3> CMD ["参数1","参数2"...] 提供给ENTRYPOINT的默认参数
举例:
	CMD ["catalina.sh", "run"]

The dockerfile should have only one cmd command. If multiple commands are specified, they will be overwritten and only the last one will be called.

  1. ENTRYPOINT instruction
    is a command executed after the container is started, and will not be overwritten by the parameters provided by docker run. It is also unique and is the same as CMD.
语法有两种格式:
	<1> ENTRYPOINT ["executable","参数1","参数2"...]
	<2> ENTRYPOINT command 参数1 参数2... shell中执行
举例:
	ENTRYPOINT ["curl", "-s", "xxx.com"]

For example, if you execute the following command

docker run -it xxximages:xxx -i

The parameters of CMD will be "-i"Append to the ENTRYPOINT parameter, which is:
ENTRYPOINT ["curl", "-s", "-i", "xxx.com"]

  1. ONBUILD instruction
    When the A image is used as the base image of the B image, the command recorded in the A image dockerfile is executed when the B image is built. To summarize,
    when the B image is built, the command specified after ONBUILD in the A image docker file will be executed.
语法:
	ONBUILD [INSTRUCTION]
举例:
	ONBUILD RUN echo "build form A镜像"

3. Customize dockerFile

We use the dockerfile scriptorderGenerally as follows
→ 1. Write dockerfile
→ 2. build dockerfile to obtain the image file
→ 3. docker run to generate a container through the image

Now let us define a dockerfile ourselves according to the above sequence
and directly upload the dockerfile code.

FROM centos:7
MAINTAINER cjl<[email protected]>
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD echo "clearlove77777"
CMD /bin/bash

This is a very simple dockerfile. We build and compile it into an image.

语法:docker build -f <dockerfilename> -t <image> .
注意这一行的末尾,是有一个英文句号的,表明是在当前目录
举例:docker build -f dockerfile1 -t cjl_centos:1.0 .

Insert image description here
Because there are installation commands in the dockerfile, there are too many things printed on the console when downloading. Here I only take screenshots of the beginning and end, and then
Insert image description here
use docker run to generate the container through the image.

docker run -dit image7:1.0

First check the generated image
Insert image description here
and then start the container. Previously we specified the workdir to be usr/local. Enter the container to see if it is like this.
Once you get it, something is wrong.
Insert image description here


OK, this is a brief introduction to dockerFile. Building dockerfile when we actually use it is the basic core of using docker. I hope you can continue to learn other instructions. See you in the next section.

Guess you like

Origin blog.csdn.net/cjl836735455/article/details/106450966