Docker series articles:

This article is the fifth article in the Docker series. You must follow the demo I made and type it out. The impression will be even more profound. Come on!

What is DockerFile

Dockfile is a script interpreted by the Docker program. The Dockerfile consists of instructions one by one, and each instruction corresponds to a command under Linux. The Docker program translates these Dockerfile instructions into real Linux commands. Dockerfile has its own writing format and supported commands, and the Docker program solves the dependencies between these commands. The Docker program will read the Dockerfile and generate a customized image (Image) according to the instructions. Compared with the black box of mirroring, the obvious script of Dockerfile is easier to be accepted by users. It clarifies how the mirroring is generated. With Dockerfile, when we need to customize our own additional requirements, we only need to add or modify instructions on the Dockerfile to regenerate the image. This saves the trouble of typing commands. Each time adding a command is equivalent to the basis of the image. Add a layer.

Dockerfile basic structure

Dockerfile is generally divided into four parts: basic image information, maintainer information, image operation instructions and instructions to be executed when the container is started. Dockerfile instructions are case-ignoring. It is recommended to use uppercase and use # as a comment. Each line only supports one instruction. Each instruction can carry multiple parameters.

Introduction to Dockerfile instructions

Regarding each core instruction, I will execute the instruction when I am studying. You can watch it patiently. You can copy the instruction and execute it. The insight will be more profound.

FROM (Specify the base image)

Specify the basic image, which must be the first command; the subsequent commands all depend on the image specified by the command. The basic image specified by the FROM instruction can be in an official remote warehouse or in a local warehouse.

# Using
the FROM <Image>
the FROM <Image> : <tag>
the FROM  MySQL: 5.7
# explanation
tag is optional, if not used these two values, will use the latest version of the base image
copy the code
MAINTAINER (used to specify the information of the image creator)

The build instruction is used to write information related to the image maker into the image. When we execute the docker inspect command on the image, there are corresponding fields in the output to record the information.

# Using
the MAINTAINER on  < name >
the MAINTAINER on  WTZ  WTZ @ 163 . COM
duplicated code
RUN (command executed when building the image)

RUN is used to execute commands in the mirror container, and it has the following two command execution methods:

#shellExecute Method
RUN  <command>
RUN yum install wget
RUN wget -O redis.tar.gz  ""
RUN tar redis.tar.gz -xvf 
#exec implementation
# use
RUN [ "Executable""the param1""param2" ]
RUN [ "/etc/the execfile""arg1""arg1" ]
# Notes
RUN command The created intermediate image will be cached and used in the next build. If you do not want to use the cache mirroring, you can be specified when building - NO -cache parameters, such as: Docker Build - NO -cache
duplicated code
ADD (Add file)

Add local files to the container, tar type files will be automatically decompressed (network compressed resources will not be decompressed), you can access network resources, similar to wget;

# Using
the ADD <the src> ...  <dest>
the ADD [ "<the src>" , ...  "<dest>" ] is used to support a path comprising a space
the ADD test.txt/test/     # test file to add/test/
Copy code
COPY (copy files)

Copying files will not automatically decompress files, nor can you access network resources;

CMD (called after the container is built)

Specify the command to be executed when the container is started. Each Dockerfile can only have one CMD command. If multiple commands are specified, only the last one will be executed. If the user specifies a command to run when starting the container, the command specified by CMD will be overwritten. You must pay attention to the latter two points. I have made some corresponding demos, and you can refer to them.

# Supports three formats:
# Use exec execution, the recommended way;
CMD  [ "Executable" , "param1" , "param2" ]
# execute/bin/sh, the application needs to provide interaction;
CMD  the Command param1 param2
# provided ENTRYPOINT to the default parameters;
the CMD  [ "the param1" , "param2"
copy Code
  1. CMD use demo
# Write dockerfilecmdtest
vim  dockerfilecmdtest
the FROM CentOS
CMD [ "LS" , "-a" ]
# image building
Docker Build dockerfilecmdtest -f -t centoscmdtest.
# View Mirror
Docker ImagesRF Royalty Free
# boot image
docker run 8000ab3673ac
copy the code

  1. Verify that the user specifies the command to run when starting the container, and the command specified by CMD will be overwritten;
# Boot image when executed -l see detailed contents of the file 
Docker  RUN 8000ab3673ac -l
Copy the code
# Ls -al we execute a discovery can be performed
Docker  RUN 8000ab3673ac ls -al
copy the code
  1. If CMD specifies multiple commands, only the last one will be executed;
# Edit dockerfilecmdtest
vim dockerfilecmdtest
# add a
CMD  echo  "This IS A the Test."
# Building image
Docker Build dockerfilecmdtest -f -t centoscmdtest.
# View the boot image
Docker iamges
Docker RUN 86f69e6e6cbe
copy the code
ENTRYPOINT (called after the container is built)

Similar to the CMD command, configure the command to be executed after the container is started, and it will not be overwritten by the parameters provided by docker run.
There can only be one ENTRYPOINT in each Dockerfile. When multiple entries are specified, only the last one will take effect.
It can be used with the CMD command: use CMD to change participants, where CMD is equivalent to passing parameters to ENTRYPOINT.

# Two formats are supported
# Use exec execute
EntryPoint [ "Executable""param1""param2" ]
#shell perform
EntryPoint  the Command  param1 param2
copy the code
  1. ENTRYPOINT uses the demo and verifies that the parameters provided by ENTRYPOINT are not overwritten by the parameters provided by docker run;
# Write dockerentrypointtest
vim  dockerentrypointtest
the FROM CentOS
EntryPoint [ "LS" , "-a" ]
# image building
Docker Build dockerentrypointtest -f -t dockerentrypointtest.
# Boot image
Docker ImagesRF Royalty Free
Docker RUN 1ff15d1bff30 the -l
# Remove Mirror
docker rmi 1ff15d1bff30
copy the code

  1. Pass parameters dynamically with CMD command;
# Edit dockerentrypointtest
Vim  dockerentrypointtest
the FROM Ubuntu  
the CMD [ "-a" ]  
EntryPoint [ "/usr/bin/LS"
# starting container
docker run 601d1c5b2cdd
duplicated code
ENV (set environment variable)

Set the environment variable, define the environment variable, then this environment variable can be used in subsequent instructions. After the container is started, you can view this environment variable through docker inspect, or you can set or modify the environment variable when docker run --env key=value.

Use #
ENV <Key> < value >
copy the code
EXPOSE (the port designated for external interaction)

Set the command, which will map the port in the container to a port in the host machine. When you need to access the container, you can use the IP address of the host machine and the mapped port instead of the IP address of the container. To complete the entire operation, two steps are required. 1. use EXPOSE in the Dockerfile to set the container port that needs to be mapped, and then specify the -p option and the port set by EXPOSE when running the container, so that the port number set by EXPOSE will be randomly mapped to the host A port number in the machine. You can also specify the port that needs to be mapped to the host machine. At this time, make sure that the port number on the host machine is not used. The EXPOSE instruction can set multiple port numbers at one time, and the -p option can be used multiple times when running the container accordingly.

Use #
EXPOSE  <Port>  [<Port> ...]
copy the code
VOLUME (specify the supported directory)

Create a mount point that can be mounted from the local host or other containers, generally used to store data that needs to be persisted. The Volume setting command enables a directory in the container to have the function of persistently storing data. The directory can be used by the container itself or shared with other containers;

# Using
the VOLUME  [ "<the mountpoint>" ]
Copy Code
WORKDIR (Switch directory)

Setting instructions can be switched multiple times (equivalent to the cd command);

# Format
WORKDIR  /path/to/the Workdir
copy the code
USER (specified user)

Specify the user name or UID when running the container. The default is root, and subsequent RUNs will also use the specified user. When using USER to specify a user, you can use the user name, UID or GID, or a combination of the two. When the service does not require administrator rights, this command can be used to specify the running user. And you can create the required users before;

# Using
the USER  User
the USER  User : Group
the USER  User : GID
the USER  UID : Group
copy the code

Actual combat

Create a centos mirror with tomcat;

  1. Edit DockerFile;
# Edit the file centostomcat
vim centostomcat
# base image
the FROM CentOS
# of
# files in the current directory to copy files to/usr/local/in
COPY test.txt/usr/local/test.txt
# copy of the current Files tomcat and jdk under the directory to/usr/local/and decompress
ADD jdk- 8 u141-linux-x64.tar.gz/usr/local/
ADD apache-tomcat- 9.0 . 46 .tar.gz/usr/local/
#Install vim
RUN yum- y  install vim #Life
environment variable
ENV JAVA_HOME/usr/local/jdk1.8.0_141
ENV CATALINA_HOME/usr/local/apache-tomcat- 9.0 . 46
ENV CATALINA_BASH/usr/local/tomcat- the Apache- 9.0 . 46
# 8080 port of external exposure
eXPOSE  8080
# start tomact and view the log
CMD/usr/local/apache-tomcat- 9.0 . 46/bin/ && tail -F/usr/local/apache- tomcat- 9.0 . 46/logs/catalina.out
Copy code
  1. Build a mirror;
Docker  Build centostomcat -f -t dockerfiletomcat.
Copy the code
  1. Start the mirror, visit port 8088, and find that tomact has started;
Docker  RUN  -d   Expediting IT  --name  tomcattest  -p  8088 : 8080  dockerfiletomcat
curl  localhost : 8088
copy the code
  1. Enter the mirror to check whether there is a test.txt file in the/usr/local directory and whether Java is installed;
# The mirroring
Docker  Exec  Expediting IT 141eec62a29a/bin/the bash
Java -version
duplicated code

the end

Everyone is welcome to pay attention and like it!