In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how Docker uses Dockerfile to build an image, I believe most people don't know much about it, so share this article for your reference. I hope you can learn a lot after reading this article, so let's learn about it!
Dockfile is a script interpreted by a Docker program. Dockerfile consists of one instruction, each corresponding to a command under the 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, similar to Makefile. The Docker program will read the Dockerfile and generate a custom image according to the instructions. An obvious script like Dockerfile is more acceptable to users than a black box like image, which clearly shows how image is created. With Dockerfile, when we need to customize our own additional requirements, we just need to add or modify instructions on Dockerfile to regenerate image, saving us the trouble of typing commands.
1. Writing rules and instruction usage of Dockerfile
Dockerfile instructions ignore case, it is recommended to use uppercase, use # as a comment, each line supports only one instruction, each instruction can carry multiple parameters.
Dockerfile instructions can be divided into two types according to their functions: build instructions and set instructions. The build directive is used to build the image, and its specified operations are not performed on the container running image; the setting directive is used to set the properties of the image, and its specified operations will be performed in the container running image.
(1) FROM (specify basic image)
Build instructions, which must be specified and need to precede other Dockerfile instructions. Subsequent instructions depend on the image specified by the instruction. The underlying image specified by the FROM directive can be either in the official remote warehouse or in the local warehouse.
The instruction is available in two formats:
FROM
Specifies that the underlying image is the last modified version of the image. Or:
FROM:
Specifies that the underlying image is a tag version of the image.
(2) MAINTAINER (used to specify the information of the image creator)
The build instruction is used to write information about the maker of the image to the image. When we execute the docker inspect command on the image, there are corresponding fields in the output to record this information.
Format:
MAINTAINER
(3) RUN (for installing software)
Build directive, RUN can run any command supported by the underlying image. If the basic image chooses ubuntu, then the software management part can only use the commands of ubuntu.
The instruction is available in two formats:
RUN (the command is run in a shell-`/ bin/sh-c`) RUN ["executable", "param1", "param2"...] (exec form)
(4) CMD (sets the action to be performed when container starts)
Set directive for the operation specified when container starts. This can be done by executing a custom script or by executing a system command. The directive can only exist once in the file, and if there is more than one, only the last one is executed.
The instruction is available in three formats:
CMD [executable "," param1 "," param2 "] (like an exec, this is the preferred form) CMD command param1 param2 (as a shell)
When Dockerfile specifies ENTRYPOINT, the following format is used:
CMD ["param1", "param2"] (as default parameters to ENTRYPOINT)
ENTRYPOINT specifies the path to an executable script or program that will be executed with param1 and param2 as parameters. So if the CMD instruction uses the above form, then there must be a matching ENTRYPOINT in the Dockerfile.
(5) ENTRYPOINT (sets the action to be performed when container starts)
The setting instruction, which specifies the command to be executed when the container starts, can be set multiple times, but only the last one is valid.
There are two formats:
ENTRYPOINT [executable "," param1 "," param2 "] (like an exec, the preferred form) ENTRYPOINT command param1 param2 (as a shell)
The use of this instruction is divided into two cases, one is used alone, and the other is used in conjunction with the CMD instruction.
When used alone, if you also use the CMD command and CMD is a complete executable command, then the CMD instruction and ENTRYPOINT will override each other and only the last CMD or ENTRYPOINT is valid.
# CMD instruction will not be executed, only ENTRYPOINT instruction will be executed CMD echo "Hello, World!" ENTRYPOINT ls-l
Another usage is used in conjunction with the CMD instruction to specify the default parameters of the ENTRYPOINT, when the CMD instruction is not a complete executable command, but only the parameter part; the ENTRYPOINT instruction can only specify the execution command in JSON mode, not the parameters.
FROM ubuntu CMD ["- l"] ENTRYPOINT ["/ usr/bin/ls"]
(6) USER (the user who sets the container container)
Set the directive to set the user who starts the container. The default is the root user.
# specify the user running memcached ENTRYPOINT ["memcached"] USER daemon or ENTRYPOINT ["memcached", "- u", "daemon"]
(7) EXPOSE (specify the port that the container needs to map to the host machine)
Sets an instruction that maps a port in the container to a port in the host machine. When you need to access the container, you can use the host machine's IP address and the mapped port instead of the container's IP address. It takes two steps to complete the whole operation. First, use EXPOSE to set the container port to be mapped in Dockerfile, and then specify the-p option plus the port set by EXPOSE when running the container, so that the port number set by EXPOSE will be randomly mapped to a port number in the host machine. You can also specify the port that needs to be mapped to the host machine, making sure that the port number on the host machine is not used. The EXPOSE instruction can set multiple port numbers at a time, and the-p option can be used multiple times when the corresponding container is run.
Format:
EXPOSE [...] # Mapping one port EXPOSE port1 # commands used by the corresponding running container docker run-p port1 image # mapping multiple ports EXPOSE port1 port2 port3 # commands used by the corresponding running container docker run-p port1-p port2-p port3 image # can also specify a port number docker run-p host_port1:port1-p host_port2:port2-p host_port3:port3 image that needs to be mapped to the host machine
Port mapping is an important function of docker because the IP address of the container cannot be specified every time we run the container, but is randomly generated within the address range of the bridged Nic. The IP address of the host machine is fixed. We can map the port of the container to a port on the host machine, eliminating the need to check the IP address of the container every time we access a service in the container. For a running container, you can use docker port plus the port that needs to be mapped in the container and the ID of the container to see the port number mapped on the host machine.
(8) ENV (used to set environment variables)
Build directive to set an environment variable in image.
Format:
ENV
Once set, subsequent RUN commands can be used, and after container starts, you can view the environment variable through docker inspect, or you can set or modify the environment variable when docker run-- env key=value.
If you have installed the JAVA program and need to set up JAVA_HOME, you can write this in Dockerfile:
ENV JAVA_HOME / path/to/java/dirent
(9) ADD (dest path for copying files from src to container)
Build directive, all files and folders copied into container have permissions of 0755 uid and gid 0; if it is a directory, all files in that directory will be added to the container, excluding directories; if the file is in a recognizable compressed format, docker will help unzip it (note the compressed format); if it is a file and ends without a slash in it, it will be regarded as a file and its contents will be written If it is a file and ends with a slash, the file is copied to the directory.
Format:
ADD
Is the relative path relative to the source directory being built, which can be the path of a file or directory, or a remote file url
Is the absolute path in container
(10) VOLUME (specify mount point)
Set the instruction so that a directory in the container can store data persistently, which can be used by the container itself or shared with other containers. We know that the container uses AUFS, which is a file system that cannot persist data, and when the container is closed, all changes are lost. This directive can be used in Dockerfile when the application in the container has a need to persist data.
Format:
VOLUME ["] FROM base VOLUME [" / tmp/data "]
Run the container that generates the image through the Dockerfile, and the data in the / tmp/data directory still exists after the container is closed. For example, another container also needs to persist data, and if you want to use the / tmp/data directory shared by the container above, you can run the following command to start a container:
Docker run-t-I-rm-volumes-from container1 image2 bash
Container1 runs the name of image for the ID,image2 of the first container for the second container.
(11) WORKDIR (switch directories)
Setting instruction, which can be switched multiple times (equivalent to the cd command), takes effect on RUN,CMD,ENTRYPOINT.
Format:
WORKDIR / path/to/workdir# executes vim a.txt WORKDIR / p1 WORKDIR p2 RUN vim a.txt under / p1/p2
(12) ONBUILD (executed in child images)
ONBUILD
The command specified by ONBUILD is not executed when the mirror is built, but in its child mirror.
two。 Create Dockerfile and build jdk+tomcat environment
Dockerfile file
# Pull base image FROM ubuntu:13.10 MAINTAINER zing wang "zing.jian.wang@gmail.com" # update source RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > / etc/apt/sources.list RUN apt-get update # Install curl RUN apt-get-y install curl # Install JDK 7 RUN cd / tmp & curl-L 'http://download.oracle.com/otn-pub/java/jdk/7u65-b17/jdk-7u65-linux-x64.tar. Gz'-H'Cookie: oraclelicense=accept-securebackup-cookie Gpw_e24=Dockerfile' | tar-xz RUN mkdir-p / usr/lib/jvm RUN mv / tmp/jdk1.7.0_65/ / usr/lib/jvm/java-7-oracle/ # Set Oracle JDK 7 as default Java RUN update-alternatives-- install / usr/bin/java java / usr/lib/jvm/java-7-oracle/bin/java 300 RUN update-alternatives-- install / usr/bin/javac javac / usr/lib/jvm/java-7-oracle/bin/javac 300 ENV JAVA_ HOME/ usr/lib/jvm/java-7-oracle/ # Install tomcat7 RUN cd / tmp & & curl-L 'http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.8/bin/apache-tomcat-7.0.8.tar.gz' | tar-xz RUN mv / tmp/apache-tomcat-7.0.8/ / opt/tomcat7/ ENV CATALINA_HOME/ opt/tomcat7 ENV PATH $PATH:$CATALINA_HOME/bin ADD tomcat7.sh / etc/init. D/tomcat7 RUN chmod 755 / etc/init.d/tomcat7 # Expose ports. EXPOSE 8080 # Define default command. ENTRYPOINT service tomcat7 start & & tail-f / opt/tomcat7/logs/catalina.out
Tomcat7.sh
Export JAVA_HOME=/usr/lib/jvm/java-7-oracle/ export TOMCAT_HOME=/opt/tomcat7 case $1 in start) sh $TOMCAT_HOME/bin/startup.sh;; stop) sh $TOMCAT_HOME/bin/shutdown.sh;; restart) sh $TOMCAT_HOME/bin/shutdown.sh sh $TOMCAT_HOME/bin/startup.sh;; esac exit 0
I have uploaded these files to Github https://github.com/agileshell/dockerfile-jdk-tomcat.git
3. Build an image
Once the script has been written, it needs to be converted into a mirror image:
Docker build-t zingdocker/jdk-tomcat. Docker run-d-p 8090 8080 zingdocker/jdk-tomcat
By default, tomcat occupies port 8080. Just now when you started container, you specified-p 8090 8080, which is mapped to the host port 8090.
Http://:8090 host is the host IP
These are all the contents of the article "how Docker uses Dockerfile to build images". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.