Docker Container Series Articles Docker file of Docker Container Technology

Keywords: Linux Tomcat Docker Apache JDK

Previously, the relevant articles are as follows:

Docker Container Series Introduction to Docker Technology (1)

Docker Container Series Introduction to Docker Technology (II)

Docker Container Series: How many of these 20 Docker Command s do you know?

A Brief Introduction to Docker file

Docker can use the content of Dockerfile to automatically build the image. Dockerfile is also a file, which has a series of commands such as creating mirrors, running instructions and so on, and only one running command per line is supported.

Docker file is divided into four parts:

  • Basic Mirror Letter
  • Maintainer Information
  • Mirror Operating Instruction
  • Execution of instructions at container startup

The dockerfile instruction ignores case and suggests capitalization, # as a comment, and supports only one instruction per line. The instruction can take multiple parameters.

The dockerfile instructions are:

  • Build instructions: Used to build images, whose specified operations will not be executed in containers running images.
  • Setting instructions: Used to set the properties of an image, the operation specified will be executed in the container running the image.

Dockerfile directive

There are the following types of Dockerfile instructions:

1,FROM
Used to specify the base image, and then by building a new image on the base image, the base image usually has a remote or local warehouse. And the FROM directives required for the first line of a Dockerfile can be used if a Dockerfile needs to create more than one image.

#The specific usage is as follows:
FROM < image_name >   #The default is the latest version
FROM <image:version>  #Specified version

2,MAINTAINER
Specify the creator information for the mirror

#The specific usage is as follows:
MAINTAINER < name >

3,RUN
Running all commands supported by the underlying mirror can also use multiple RUN instructions, which can be used to wrap lines

#The specific usage is as follows:
RUN < command >
RUN ["executable", "param1", "param2" ... ] (exec form) 

4,CMD
It can be a command or a script for a specified operation at container startup, but only once, if there is a default number, only the last one will be executed.

# The specific usage is as follows:
CMD ["executable", "Param1", "param2"] uses exec to execute, recommend 
CMD command param1 param2, executed on / bin/sh 
CMD ["Param1", "param2"] provides ENTRYPOINT with default parameters.

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

#The specific usage is as follows:
EXPOSE <port> [port1 , port2 ............]

6,ENV
The RUN command can then use the environment variable set in the mirror to view the environment variable through the docker inspect after the container is started. It can set or modify the environment variable through the docker run --env key=value.

#The specific usage is as follows:
ENV <key> <value>
ENV JAVA_HOME /usr/local/jdk

7,ADD
Copy the specified source files, directories, and URL s to the specified directory of the container. All files and folders copied to container have permissions of 0755, uid and gid of 0.

If the source is a directory, all files in that directory are added to the container, excluding directories.

If the source file is a recognizable compression format, docker will help decompress (pay attention to the compression format);

If the source is a file and the destination directory does not end with a slash, the destination directory will be treated as a file and the content of the source will be written to the destination directory.

If the source is a file and the destination directory ends with a slash, the source file is copied to the destination directory.

# The specific usage is as follows:
ADD < Source > < Objectives >

8,COPY
Copy the source of the local host (default is the directory where the Dockerfile is located) to the target in the container, which is automatically created when the target path does not exist.

#The specific usage is as follows:
COPY <source> <target>
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 the relative path of the path where Dockerfile is located

If it is a directory, only the contents of the directory will be copied, while the directory itself will not be copied.

9,ENTRYPOINT
Specifies the command to be executed after the container is started, with multiple lines executing only the last line. And it cannot be overwritten by the parameters provided by docker run.

#The specific usage is as follows:
ENTRYPOINT "command" "param1" "param2"

10,VOLUME
Create a mount point that can be mounted from a local host or other container, usually for storing data. It can also be implemented with docker run-v.

#The specific usage is as follows:
VOLUME  [directory_name]
VOLUME /docker_data

11,USER
Specify the user or UID used by the container runtime, which will be used later by RUN, CMD, and ENTRYPIONT to run commands.

#The specific usage is as follows:
USER [username/uid]

12,WORKDIR
Specify the runtime directory of the commands specified by RUN, CMD, and ENTRYPIONT. Multiple WORKDIR instructions can be used, and subsequent parameters, if relative, will be based on the path specified by the previous command. For example: WORKDIR / data WORKDIR work. The final path is / data/work. The path path path can also be an environment variable.

#The specific methods of use are as follows:
WORKDIR [path]

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

#The specific usage is as follows:
ONBUILD [INSTRUCTION]

Quickly building mirrors through Dockerfile

Next, we demonstrate how to use Dockerfile by building a Tomcat image, provided that the Docker environment is installed, and how to install the Docker environment is not discussed here. Please stamp the following text:

[root@master tomcat]# ll
//Total dosage 190504
-rw-r--r-- 1 root root   9552281 6 Month 715:07 apache-tomcat-8.5.31.tar.gz
-rw-r--r-- 1 root root        32 7 Month 309:41 index.jsp
-rw-r--r-- 1 root root 185515842 9 Month 202017 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 <Wechat Public Number:The Technological Way of Migrant Workers'Brothers>

#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#

Construction process

[root@master tomcat]# docker build -t tomcat-web . #This. Don't comment on it. People who believe in it understand it naturally.
Sending build context to Docker daemon 195.1 MB
Step 1/11 : FROM centos
 ---> 49f7960eb7e4
Step 2/11 : MAINTAINER mingongge <Wechat Public Number:The Technological Way of Migrant Workers'Brothers>
 ---> 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 by building a mirror

[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

Browser Input http://server-ip 8080, the results are as follows:

Posted by pjsteinfort on Wed, 18 Sep 2019 00:18:47 -0700