Docker file of Docker container technology

               


Introduced earlier:

Container Technology|Compose of Docker Three Musketeers

Container Technology|Docker-machine of Docker Three Musketeers

Create a highly compelling and visualized Docker container monitoring system platform

A brief introduction to Docker file

Docker can use the contents of the Dockerfile to automatically build the image. Dockerfile is also a file, which contains a series of commands such as creating a mirror and running instructions, and each line only supports one running command.

Docker file is divided into four parts:
  • Basic Mirror Letter

  • Maintainer information

  • Mirror operation instructions

  • Execute instructions when the container starts

The dockerfile command ignores uppercase and lowercase letters. Uppercase is recommended. #As a comment, only one command per line is supported, and the command can take multiple parameters.

The dockerfile instructions are:
  • Build instructions: used to build the image, the specified operation will not be executed in the container running the image.

  • Setting instructions: used to set the attributes of the image, and the specified operations will be executed in the container running the image.

Dockerfile instructions

There are the following types of Dockerfile instructions:

1. FROM is
used to specify the base image, and then build a new image on the base image. The base image generally has a remote or local warehouse. And the required FROM instruction in the first line of the Dockerfile file, if a Dockerfile needs to create multiple images, you can use multiple FROM instructions.

#具体使用用法如下:
FROM < image_name >   #默认是latest版本
FROM <image:version>  #指定版本

2. MAINTAINER
specifies the creator information of the mirror

#具体使用法如下:
MAINTAINER < name >

3. RUN
runs all the commands that the basic image can support. You can also use multiple RUN instructions, and you can use \ to wrap

#具体使用法如下:
RUN < command >
RUN ["executable""param1""param2" ... ] (exec form) 

4. CMD is
used for the specified operation when the container is started. It can be a command or a script, but it will only be executed once. If there are many, it will only execute the last one by default.

#具体使用法如下:
CMD [“executable” ,”Param1”, “param2”]使用exec执行,推荐 
CMD command param1 param2,在/bin/sh上执行 
CMD [“Param1”, “param2”] 提供给ENTRYPOINT做默认参数。

5. EXPOSE
specifies the port mapping of the container (container and physical machine). When running the container, add the -p parameter to specify the port set by EXPOSE. EXPOSE can set multiple port numbers, and use the -p parameter multiple times to run the container accordingly. You can refer to the host's mapped port by docker port + the port number and container ID that the container needs to map.

#具体使用法如下:
EXPOSE <port> [port1 , port2 ............]

6. ENV
is used to set environment variables in the image, and then the RUN command can use the environment variables that are set. After the container is started, you can also view the environment variables through docker inspect, which can be set by docker run --env key=value or Modify environment variables.

#具体使用法如下:
ENV <key> <value>
ENV JAVA_HOME /usr/local/jdk

7. ADD
copies the specified source files, directories, and URLs to the specified directory of the container. The permissions of all files and folders copied to the container are 0755, and the uid and gid are 0.

  • If the source is a directory, all files in the directory will be added to the container, excluding the directory;

  • If the source file is in a recognizable compression format, docker will help decompress it (note the compression format);

  • If the source is a file and the target directory does not end with a slash, the target directory will be regarded as a file, and the contents of the source will be written into the target directory;

  • If the source is a file and the target directory ends with a slash, the source file will be copied to the target directory.

#具体使用法如下:
ADD <> <目标>

8. COPY
copies the source of the local host (the directory where the Dockerfile is located by default) to the target in the container. The target path will be created automatically if it does not exist.

#具体使用法如下:
COPY <源> <目标>
COPY web/index.html  /var/web/
  • The path must be an absolute path, if it does not exist, the corresponding directory will be created automatically

  • The path must be relative to the path where the Dockerfile is located

  • If it is a directory, only the contents under the directory will be copied, but the directory itself will not be copied

9. ENTRYPOINT
specifies the command to be executed after the container is started, and only the last line is executed for multiple lines. And it cannot be overridden by the parameters provided by docker run.

#具体使用法如下:
ENTRYPOINT "command" "param1" "param2"

10. VOLUME
creates a mount point that can be mounted from the local host or other containers, generally used to store data. This function can also be achieved with docker run -v.

#具体使用法如下:
VOLUME  [directory_name]
VOLUME /docker_data

11. USER
specifies the user or UID used when the container is running. Later, RUN, CMD, and ENTRYPIONT will use this user to run commands.

#具体使用法如下:
USER [username/uid]

12. WORKDIR
specifies the running directory of the command specified by RUN, CMD, and ENTRYPIONT. Multiple WORKDIR instructions can be used. If the subsequent parameter is a relative path, it will be based on the path specified by the previous command. Such as: WORKDIR /data WORKDIR work. The final path is /data/work. The path path can also be an environment variable.

#具体使用方法如下:
WORKDIR [path]

13. ONBUILD
configures the currently created image as the base image of other newly created images, the operation instructions executed. That is, after this mirror is created, if other mirrors are based on this mirror, the ONBUILD command of this mirror will be executed first.

#具体使用法如下:
ONBUILD [INSTRUCTION]

Quickly build an image through Dockerfile

Next, we will demonstrate the use of Dockerfile by building a Tomcat image. The premise is to install the Docker environment. How to install the Docker environment will not be repeated here. Please jab the following text:

[root@master tomcat]# ll
总用量 190504
-rw-r--r-- 1 root root   9552281 6月   7 15:07 apache-tomcat-8.5.31.tar.gz
-rw-r--r-- 1 root root        32 7月   3 09:41 index.jsp
-rw-r--r-- 1 root root 185515842 9月  20 2017 jdk-8u144-linux-x64.tar.gz
[root@master tomcat]# cat index.jsp 
welcome to mingongge's web site
[root@master tomcat]# pwd
/root/docker/tomcat
[root@master tomcat]# vim Dockerfile
#config file start#
FROM centos
MAINTAINER mingongge <微信公众号:民工哥技术之路>

#add jdk and tomcat software
ADD jdk-8u144-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-8.5.31.tar.gz /usr/local/
ADD index.jsp /usr/local/apache-tomcat-8.5.31/webapps/ROOT/

#config java and tomcat ENV
ENV JAVA_HOME /usr/local/jdk1.8.0_144
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.31/
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin

#config listen port of tomcat
EXPOSE 8080

#config startup command of tomcat
CMD /usr/local/apache-tomcat-8.5.31/bin/catalina.sh run

#end of config-file#
Build process
[root@master tomcat]# docker build -t tomcat-web . #这个.不用注释了吧相信懂的人自然懂的
Sending build context to Docker daemon 195.1 MB
Step 1/11 : FROM centos
 ---> 49f7960eb7e4
Step 2/11 : MAINTAINER mingongge <微信公众号:民工哥技术之路>
 ---> Running in afac1e218299
 ---> a404621fac22
Removing intermediate container afac1e218299
Step 3/11 : ADD jdk-8u144-linux-x64.tar.gz /usr/local/
 ---> 4e22dafc2f76
Removing intermediate container b1b23c6f202a
Step 4/11 : ADD apache-tomcat-8.5.31.tar.gz /usr/local/
 ---> 1efe59301d59
Removing intermediate container aa78d5441a0a
Step 5/11 : ADD index.jsp /usr/local/apache-tomcat-8.5.31/webapps/ROOT/
 ---> f09236522370
Removing intermediate container eb54e6eb963a
Step 6/11 : ENV JAVA_HOME /usr/local/jdk1.8.0_144
 ---> Running in 3aa91b03d2d1
 ---> b497c5482fe0
Removing intermediate container 3aa91b03d2d1
Step 7/11 : ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
 ---> Running in f2649b5069be
 ---> 9cedb218a8df
Removing intermediate container f2649b5069be
Step 8/11 : ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.31/
 ---> Running in 39ef620232d9
 ---> ccab256164fe
Removing intermediate container 39ef620232d9
Step 9/11 : ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin
 ---> Running in a58944d03d4a
 ---> f57de761a759
Removing intermediate container a58944d03d4a
Step 10/11 : EXPOSE 8080
 ---> Running in 30681437d265
 ---> b906dcc26584
Removing intermediate container 30681437d265
Step 11/11 : CMD /usr/local/apache-tomcat-8.5.31/bin/catalina.sh run
 ---> Running in 437790cc642a
 ---> 95204158ee68
Removing intermediate container 437790cc642a
Successfully built 95204158ee68
Start the container from the built image
[root@master tomcat]# docker run -d -p 8080:8080 tomcat-web
b5b65bee5aedea2f48edb276c543c15c913166bf489088678c5a44fe9769ef45
[root@master tomcat]# docker ps
CONTAINER ID   IMAGE        COMMAND                  CREATED        STATUS         PORTS                    NAMES
b5b65bee5aed   tomcat-web   "/bin/sh -c '/usr/..."   5 seconds ago  Up 4 seconds   0.0.0.0:8080->8080/tcp   vigilant_heisenberg
Access container

The browser enters http://server-ip:8080, and the result is as follows:

image


Guess you like

Origin blog.51cto.com/15127557/2667618