dockerFile
dockerfile build image
Dockerfile image construction is based on the basic image, * * dockerfile is a text file, * * content is some docker instructions written by the user, and each instruction builds a layer. Therefore, the content of each instruction is to describe how the layer should be built.
Dockerfile has 13 basic instructions: FROM, MAINTAINER, RUN, CMD, EXPOSE, ENV, ADD, COPY, ENTRYPOINT, VOLUME, USER, WORKDIR and ONBUILD
1. Dockerfile common instructions
type | command |
---|---|
Basic image information | FROM |
Maintainer information | MAINTAINER |
Mirror operation instruction | RUN, COPY, ADD, EXPOSE, WORKDIR, ONBUILD, USER, VOLUME, etc |
Execute command when container starts | CMD,ENTRYPOINT |
1.1. FROM: Specifies the basic image
The so-called customized image must be customized based on an image. Just as we ran an nginx image container before and then modified it, the basic image must be specified. And FROM is to specify the basic image, so FROM in a Dockerfile is a necessary instruction and must be the first instruction.
For example, specify centos as the basic image
FROM centos
1.2 RUN: Execute command
RUN instruction: commands executed inside the new image, such as executing certain actions, installing system software, configuring system information, etc,
There are two formats:
1) shell format: run < command >, just like the command entered directly on the command line.
For example, write "hello" in the default homepage of nginx:
RUN echo 'hello ' >/etc/nginx/html/index.html
2) exec format: RUN ["executable", "parameter 1", "parameter 2"]
To install nginx in the new image using yum:
RUN ["yum","install","nginx"] # Note: do not write multiple runs for multiline commands, because each instruction in Dockerfile will establish a layer The number of layers of images built according to the number of runs will cause the image to be bloated and multi-layered, which not only increases the time of component deployment, but also prone to errors. The line feed character in the writing of RUN is\
1.3 COPY: COPY files
The COPY command is used to COPY the files on the host machine into the image. If the destination location does not exist, Docker will automatically create it. However, the directory to be copied by the host machine must be under the statistical directory of Dockerfile file.
Format:
COPY [--chown=<user>:<group>] <from path>... <Target path> COPY [--chown=<user>:<group>] ["<Source path 1>",... "<Target path>"]
Such as package. In the host computer Copy the JSON file to the / usr/src/app / directory in the container:
COPY package.json /usr/src/app/
1.4 CMD: container start command
CMD command is used for the commands to be executed when the container is started. CMD can only appear once in Dockerfile. If there are multiple CMD commands, only the last one will be valid.
Its function is to provide a default command item when starting the container. If the user provides a command item when executing docker run, the command will be overwritten. If not, the command at the time of construction will be used.
Format:
shell Format: CMD <command> exec Format: CMD ["Executable file", "Parameter 1", "Parameter 2"...]
If bash (terminal command line) is entered when the container is started:
CMD /bin/bash
You can also use exec:
CMD ["/bin/bash"]
1.5 MAINTAINER: designated author
It is used to specify the author name and mailbox of dockerfile. Its main function is to identify the owner of the software.
Syntax:
MAINTAINER <name> <email>
For example:
MAINTAINER xiaoxuya xxxxx@qq.com
- 1.6. EXPOSE: exposed port
The export naming is applicable to setting the container port number of the external mapping of the container. For example, the port 8080 used in the tomcat container can be used to tell the outside world that the port 8081 of the container is external when building the image
Use docker run -p to set the mapping of the exposed port to the host machine port.
Syntax:
EXPOSE <Port 1> [<Port 2>...]
For example:
EXPOSE 8080
Export 8080 is actually equivalent to docker run -p 8080. When you need to map the 8080 port to a port in the host (such as 8888) for external access, you can use docker run -p 8888:8080
- 1.7. WORKDIR: configure working directory
The WORKDIR command configures the working directory for RUN, CMD and ENTRYPOINT instructions. Its effect is similar to the cd command in Linux naming. It is used for directory switching, but unlike cd: if the directory you switch to does not exist, WORKDIR will create a directory for it.
Syntax:
WORKDIR path # path specifies the working directory when entering
If necessary, create a hello. Net directory in nginx directory Txt file:
# Enter / usr/local/nginx directory WORKDIR /usr/local/nginx # Enter the html directory in / usr/local/nginx WORKDIR html # Create a hello. html directory Txt file RUN echo 'hello' > hello.txt
- 1.8. ENTRYPOINT: naming of container startup execution
ENTRYPOINT is as like as two peas in CMD, but ENTRYPOINT has 2 different things from CMD:
- CMD commands will be overwritten by docker run commands, while ENTRYPOINT will not
- When both CMD and ENTRYPOINT exist, the CMD instruction becomes the parameter of ENTRYPOINT, and the parameter provided by this CMD will be overwritten by the command after docker run
- 1.9,VOLUME
VOLUME is used to create a mount point that can be mounted from a local host or other container. For example, we know that the webapps directory of tomcat is where the web application code is placed. At this time, we should mount the webapps directory as an anonymous VOLUME, so that any heart written into webapps will not be recorded into the storage layer of the container, making the storage layer of the container stateless.
Format:
VOLUME ["path"]
For example, create a mount point of the webapps directory of tomcat
VOLUME /usr/local/tomcat/webapps
In this way, when running the container, you can also use docker run -v to mount the anonymous mount points to a directory on the host machine, such as
docker run -d -v /home/tomcat_webapps:/usr/local/tomcat/webapps
- 1.10, USER
The USER command is used to specify the currently executing USER. It should be noted that this USER must already exist, otherwise it cannot be specified. Its usage is a bit like WORKDIR, switching users.
-
1.11,ADD
-
As like as two peas, COPY is not the same as the way it is used.
-
.12,ONBUILD
ONBUILD is used to configure the currently created image as the basic image of other newly created images.
This means that after this image is created, if other images are based on this image, the ONBUILD command of this image will be executed first
- 1.13 ENV: setting environment variables
ENV naming is used to set the environment variables of the container. These variables exist in the form of "key=value" and are called by scripts or programs in the container. This variable will also be retained when the container runs.
Format:
- 1) Set one: ENV
- 2) Set multiple: ENV = =
For example, set an environment variable JAVA_HOME, this variable can be used for the following naming:
ENV JAVA_HOME /opt/jdk ENV PATH $PATH:$JAVA_HOME/bin
There are several points to note when setting environment variables with ENV:
- 1) It is transitive, that is, when the current image is used as the basic image of other images, the new image will have all the environment variables of the current basic image
- 2) ENV defined environment variables can be used in all subsequent instructions (except CMD) of dockerfile, but cannot be referenced by the command parameters of docker run
For example:
ENV tomcat_home_name tomcat_7 RUN mkdir $tomcat_home_name
3) In addition to ENV, docker run -e can also set environment variables to pass into the container.
For example:
docker run -d tomcat -e "tomcat_home_name=tomcat_7"
In this way, we can enter the container and use ENV to see tomcat_home_name is the environment variable.
2. Preparation of Dockerfile
Let's look at an example first
#Installing nginx on centos FROM centos #Indicate the name and email address of the author MAINTAINER xiaoxuya xxxx@qq.com #Test the network environment RUN ping www.baidu.com #Install some necessary software for nginx (- y answer yes to all question s) RUN yum -y install gcc make pcre-devel zlib-devel tar zlib #Copy the nginx installation package to the / usr/src / directory (Note: the dockerfile will be decompressed automatically, and the installation package and dockerfile must be in the same directory) copy nginx-1.15.8.tar.gz /usr/src/ #Switch to / usr/src/nginx-1.15.8 to compile and install nginx (the newline character in run writing is \) RUN cd /usr/src/nginx-1.15.8 \ && mkdir /usr/local/nginx \ && ./configure --prefix=/usr/local/nginx && make && make install \ && ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ \ && nginx #Delete nginx installation directory RUN rm -rf /usr/src/nginx-nginx-1.15.8 #Exposed port 80 (nginx default port) EXPOSE 80 #start nginx CMD ["nginx", "-g", "daemon off;"]
The above annotations as like as two peas are already clear, and it is not difficult to find that the above example is similar to a process of installing a nginx on centos system. So the Dockerfile building mirroring is almost exactly the same as that on Linux. Therefore, when writing dockerfile to build images, we can first think about the process of installing the software on Linux, and then convert the instructions provided by dockerfile into dockerfile.
3. Building images with Dockerfile
The core of using Dockerfile is to write Dockerfile, but after writing, we need to know how to use Dockerfile to build an image. Let's take building nginx image as an example to briefly explain the construction process
- 3.1 upload installation package
First, we need to upload the software installation package to the server. We can create a special folder in the server directory, such as: / var/nginx_build, and then download nginx-1.15.8 tar. Upload the GZ installation package to this directory.
- 3.2 writing Dockerfile
How to write the Dockerfile of nginx has been described in detail above. Now we just need to upload the written Dockerfile to / var / nginx_ Under the build directory, of course, you can also write Dockerfile directly on the server, but remember to ensure that the Dockerfile file and the installation package are in the same directory.
- 3.3 run build command
The docker build command is used to create an image using Dockerfile.
Format:
docker build [OPTIONS] PATH | URL | -
OPTIONS has many instructions. Here are some common ones:
- – build Arg = []: set the variable when creating the image;
- -f: specify the Dockerfile path to use;
- – force RM: delete intermediate containers in the process of setting mirroring;
- – rm: delete the intermediate container after setting the image successfully;
- – tag, -t: the name and label of the image, usually in the format of name:tag or name;
So we can build nginx with the following commands:
When Dockerfile is not in the same directory as the current command, we can also specify Dockerfile, such as
docker build -f /var/nginx_build/Dockerfile .
Note: there is "." at the end of the command line of
After naming, you will see that the console outputs the build content layer by layer until two successful results are output, that is, the build is successful.
4. Actual combat: use dockerFile to build tomcat image and run it
1. Prepare tomcat, jdk
2. Write dockerfile
vim Dockerfile # Write dockerfile FROM centos MAINTAINER xiaoxuya 1848734470@qq.com ADD apache-tomcat-9.0.45.tar.gz /usr/local/ ADD jdk-8u261-linux-x64.tar.gz /usr/local/ RUN yum -y install vim ENV MYPATH /usr/local WORKDIR $MYPATH ENV JAVA_HOME /usr/local/jdk1.8.0_261 ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.45 ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin EXPOSE 8080 CMD /usr/local/apache-tomcat-9.0.45/bin/startup.sh && tail -F /url/local/apache-tomcat-9.0.45/bin/logs/catalina.out
3. Build image through dockerfile
[root@VM-8-9-centos test]# docker build -f ./Dockerfile -t tomcat-image:1.0 . Sending build context to Docker daemon 154.6MB Step 1/12 : FROM centos ---> 300e315adb2f Step 2/12 : MAINTAINER xiaoxuya 1848734470@qq.com ---> Running in d0140dde4349 Removing intermediate container d0140dde4349 ---> 898209e32eda Step 3/12 : ADD apache-tomcat-9.0.45.tar.gz /usr/local/ ---> f31e888f965e Step 4/12 : ADD jdk-8u261-linux-x64.tar.gz /usr/local/ ---> d29625c0deaa Step 5/12 : RUN yum -y install vim ---> Running in 29e01b84012a CentOS Linux 8 - AppStream 126 kB/s | 7.5 MB 01:01 CentOS Linux 8 - BaseOS 2.8 MB/s | 2.6 MB 00:00 CentOS Linux 8 - Extras 16 kB/s | 9.6 kB 00:00 Dependencies resolved. ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: vim-enhanced x86_64 2:8.0.1763-15.el8 appstream 1.4 M Installing dependencies: gpm-libs x86_64 1.20.7-17.el8 appstream 39 k vim-common x86_64 2:8.0.1763-15.el8 appstream 6.3 M vim-filesystem noarch 2:8.0.1763-15.el8 appstream 48 k which x86_64 2.21-12.el8 baseos 49 k Transaction Summary ================================================================================ Install 5 Packages Total download size: 7.8 M Installed size: 30 M Downloading Packages: [MIRROR] gpm-libs-1.20.7-17.el8.x86_64.rpm: Curl error (28): Timeout was reached for http://mirrors.nju.edu.cn/centos/8.4.2105/AppStream/x86_64/os/Packages/gpm-libs-1.20.7-17.el8.x86_64.rpm [Connection timed out after 30000 milliseconds] [MIRROR] vim-common-8.0.1763-15.el8.x86_64.rpm: Curl error (28): Timeout was reached for http://mirrors.nju.edu.cn/centos/8.4.2105/AppStream/x86_64/os/Packages/vim-common-8.0.1763-15.el8.x86_64.rpm [Connection timed out after 30000 milliseconds] [MIRROR] vim-enhanced-8.0.1763-15.el8.x86_64.rpm: Curl error (28): Timeout was reached for http://mirrors.nju.edu.cn/centos/8.4.2105/AppStream/x86_64/os/Packages/vim-enhanced-8.0.1763-15.el8.x86_64.rpm [Connection timed out after 30000 milliseconds] (1/5): gpm-libs-1.20.7-17.el8.x86_64.rpm 1.3 kB/s | 39 kB 00:30 (2/5): vim-filesystem-8.0.1763-15.el8.noarch.rp 537 kB/s | 48 kB 00:00 (3/5): vim-enhanced-8.0.1763-15.el8.x86_64.rpm 46 kB/s | 1.4 MB 00:30 (4/5): which-2.21-12.el8.x86_64.rpm 287 kB/s | 49 kB 00:00 (5/5): vim-common-8.0.1763-15.el8.x86_64.rpm 211 kB/s | 6.3 MB 00:30 -------------------------------------------------------------------------------- Total 253 kB/s | 7.8 MB 00:31 warning: /var/cache/dnf/appstream-02e86d1c976ab532/packages/gpm-libs-1.20.7-17.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY CentOS Linux 8 - AppStream 1.6 MB/s | 1.6 kB 00:00 Importing GPG key 0x8483C65D: Userid : "CentOS (CentOS Official Signing Key) <security@centos.org>" Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D From : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial Key imported successfully Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Installing : which-2.21-12.el8.x86_64 1/5 Installing : vim-filesystem-2:8.0.1763-15.el8.noarch 2/5 Installing : vim-common-2:8.0.1763-15.el8.x86_64 3/5 Installing : gpm-libs-1.20.7-17.el8.x86_64 4/5 Running scriptlet: gpm-libs-1.20.7-17.el8.x86_64 4/5 Installing : vim-enhanced-2:8.0.1763-15.el8.x86_64 5/5 Running scriptlet: vim-enhanced-2:8.0.1763-15.el8.x86_64 5/5 Running scriptlet: vim-common-2:8.0.1763-15.el8.x86_64 5/5 Verifying : gpm-libs-1.20.7-17.el8.x86_64 1/5 Verifying : vim-common-2:8.0.1763-15.el8.x86_64 2/5 Verifying : vim-enhanced-2:8.0.1763-15.el8.x86_64 3/5 Verifying : vim-filesystem-2:8.0.1763-15.el8.noarch 4/5 Verifying : which-2.21-12.el8.x86_64 5/5 Installed: gpm-libs-1.20.7-17.el8.x86_64 vim-common-2:8.0.1763-15.el8.x86_64 vim-enhanced-2:8.0.1763-15.el8.x86_64 vim-filesystem-2:8.0.1763-15.el8.noarch which-2.21-12.el8.x86_64 Complete! Removing intermediate container 29e01b84012a ---> cb913102b122 Step 6/12 : ENV MYPATH /usr/local ---> Running in 8b0786ed778b Removing intermediate container 8b0786ed778b ---> 2975de7c5a12 Step 7/12 : WORKDIR $MYPATH ---> Running in 0a9cc086492e Removing intermediate container 0a9cc086492e ---> a64196298976 Step 8/12 : ENV JAVA_HOME /usr/local/jdk1.8.0_261 ---> Running in 7c240082ebb5 Removing intermediate container 7c240082ebb5 ---> aa65632b8c09 Step 9/12 : ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.45 ---> Running in 73e762542984 Removing intermediate container 73e762542984 ---> 152b8206534c Step 10/12 : ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin ---> Running in ee92e18eff17 Removing intermediate container ee92e18eff17 ---> d0d0d24be807 Step 11/12 : EXPOSE 8080 ---> Running in 54efc89413b3 Removing intermediate container 54efc89413b3 ---> 3fdcefe0c0ea Step 12/12 : CMD /usr/local/apache-tomcat-9.0.45/bin/startup.sh && tail -F /url/local/apache-tomcat-9.0.45/bin/logs/catalina.out ---> Running in 3c57a982db89 Removing intermediate container 3c57a982db89 ---> 271418e6337f Successfully built 271418e6337f Successfully tagged tomcat-image:1.0
4. Run mirror
[root@VM-8-9-centos test]# docker run -d -p 9999:8080 --name tomcat-container -v /home/test/tomcat9.0/webapps:/usr/local/apache-tomcat-9.0.45/webapps -v /home/test/tomcat9.0/tomcatLogs:/usr/local/apache-tomcat-9.0.45/logs tomcat-image:1.0