In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to build Dockerfile with Docker image". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to build Dockerfile by Docker image".
Dockerfile common instruction FROM
Syntax: FROM:
Indicates which base image the new image is built from. If tag is not selected, the default value is latest.
FROM centos:7
If it is not based on any image, it is written as: FROM scratch. Official note: scratch image is an empty image, which can be used to build ultra-small images such as busybox. It can be said that you can really build your own image from scratch.
MAINTAINER (deprecated)
Syntax: MAINTAINER
Indicates the image maintainer and its contact information (usually an email address). Official instructions are out of date and LABEL is recommended.
MAINTAINER mrhelloworld LABEL
Syntax: LABEL =...
The function is to specify a label for the image. You can also use LABEL to specify the mirror author.
LABEL maintainer= "mrhelloworld.com" RUN
Syntax: RUN
The Shell command that runs when building the image, for example, in the new image we want to create a java directory under the / usr/local directory.
RUN mkdir-p / usr/local/javaADD
Syntax: ADD...
Copy a file or directory into the mirror. Src can be a local file or a local compressed file, and the compressed file will be decompressed automatically. It can also be a url. If you write src as a url, ADD is similar to the wget command, and then automatically downloads and decompresses it.
ADD jdk-11.0.6_linux-x64_bin.tar.gz / usr/local/javaCOPY
Syntax: COPY...
Copy a file or directory into the mirror. The usage is the same as ADD, except that automatic download and decompression are not supported.
COPY jdk-11.0.6_linux-x64_bin.tar.gz / usr/local/javaEXPOSE
Syntax: EXPOSE [/...]
Expose the listening port of the container to the outside world. You can specify whether the port is listening to TCP or UDP. If no protocol is specified, it defaults to TCP.
EXPOSE 80 443 8080/tcp
If you want to map the container to the port of the host, you must add the-P parameter when the container starts.
ENV
Syntax: ENV adds a single, ENV =. Add multiple.
Set the environment variables within the container.
ENV JAVA_HOME / usr/local/java/jdk-11.0.6/CMD
Syntax:
CMD ["executable", "param1", "param2"], for example: CMD ["/ usr/local/tomcat/bin/catalina.sh", "start"]
CMD ["param1", "param2"], for example: CMD ["echo", "$JAVA_HOME"]
CMD command param1 param2, for example: CMD echo $JAVA_HOME
The Shell command that is executed when the container is started. There can be only one CMD instruction in Dockerfile. If more than one CMD is set, only the last CMD will take effect.
CMD ehco $JAVA_HOME
If a command is specified when the container is created, the CMD command is replaced. If the image is called centos:7, the command when creating the container is: docker run-it-- name centos7 centos:7 echo "helloworld" or docker run-it-- name centos7 centos:7 / bin/bash, the environment variable information of $JAVA_HOME will not be output, because the CMD command is overridden by echo "helloworld" and / bin/bash.
ENTRYPOINT
Syntax:
ENTRYPOINT ["executable", "param1", "param2"], for example: ENTRYPOINT ["/ usr/local/tomcat/bin/catalina.sh", "start"]
ENTRYPOINT command param1 param2, for example: ENTRYPOINT ehco $JAVA_HOME
The Shell command executed when the container is started, similar to CMD, is not overridden by the parameters specified by the docker run command line. There can be only one ENTRYPOINT instruction in Dockerfile. If more than one ENTRYPOINT is set, only the last ENTRYPOINT will take effect.
ENTRYPOINT ehco $JAVA_HOME
If both ENTRYPOINT and CMD are written in Dockerfile, and the CMD instruction is not a complete executable command, then the content specified by CMD will be used as an argument to ENTRYPOINT
If both ENTRYPOINT and CMD are written in Dockerfile, and CMD is a complete instruction, then the two will overwrite each other, and who will take effect at the end.
WORKDIR
Syntax: WORKDIR / path/to/workdir
Set working directories for RUN, CMD, ENTRYPOINT, and COPY and AND.
WORKDIR / usr/localVOLUME
Specify the container mount point to the directory or other container automatically generated by the host. The general usage scenario is when persistent storage of data is required.
# the / var/lib/mysql directory of the container is automatically mounted as an anonymous volume at run time, and the anonymous volume is VOLUME under the / var/lib/docker/volumes directory of the host ["/ var/lib/mysql"]
It is not commonly used in Dockerfile, but it is more common to specify data volumes with-v in docker run.
Build an image
After the Dockerfile file is written, you need to use the docker build command to actually build the image.
The docker build command is used to create a mirror using Dockerfile.
# create an image docker build-t mycentos:7 using the Dockerfile of the current directory. # create an image docker build-f / usr/local/dockerfile/Dockerfile-t mycentos:7 through the location of the-f Dockerfile file.
-f: specify the Dockerfile path to be used
-- tag,-t: the name and tag of the image. You can set multiple tags for one image at a time.
About. Understand
When we use the docker build command to build an image, we often see one at the end of the command. No. What on earth does it mean?
Many people think that it is used to specify the location of the Dockerfile file, but in fact, the-f parameter is used to specify the path of the Dockerfile. What on earth is the number for?
Docker is divided into Docker engine (server daemon) and client tools at runtime. We use various docker commands every day, in fact, we are using client tools to interact with Docker engine.
When we use the docker build command to build the image, the build process is actually done in the Docker engine, not in the native environment. If you use some ADD and other instructions in Dockerfile to manipulate files, how do you get the Docker engine to get these files?
Here is the concept of image construction context. When building, the user specifies the context path when building the image, and docker build will package and upload all the files under this path to the Docker engine. After these contents are expanded in the engine, you can get the files in the context.
For example, Chestnut: my host jdk file is in the / root directory, and the Dockerfile file is in the / usr/local/dockerfile directory. The contents of the file are as follows:
ADD jdk-11.0.6_linux-x64_bin.tar.gz / usr/local/java
Then the command when building the image should be written as follows:
Docker build-f / usr/local/dockerfile/Dockerfile-t mycentos:7 / root
For another example, Chestnut: my host jdk file and Dockerfile file are in the / usr/local/dockerfile directory, and the contents of the files are as follows:
ADD jdk-11.0.6_linux-x64_bin.tar.gz / usr/local/java
Then the command when building the image reads as follows:
Docker build-f / usr/local/dockerfile/Dockerfile-t mycentos:7 .Dockerfile practice
Next, we use the basic image centos:7, install jdk and tomcat in this image, and then make it into a new image mycentos:7.
Create a directory.
Mkdir-p / usr/local/dockerfile
Write Dockerfile files.
Vi Dockerfile
The Dockerfile file is as follows:
# indicates that the new image is from the centos:7 basic image FROM centos:7# declares the author information LABEL maintainer= "mrhelloworld.com" # set the working directory WORKDIR / usr/local# after the new image is successfully built, create the specified directory RUN mkdir-p / usr/local/java & & mkdir-p / usr/local/tomcat# copy file into the mirror and decompress the ADD jdk-11.0.6_ Linux-x64_bin.tar.gz / usr/local/javaADD apache-tomcat-9.0.37.tar.gz / usr/local/tomcat# exposes the container runtime listening port 8080 sets the JAVA_HOME environment variable ENV JAVA_HOME/ usr/local/java/jdk-11.0.6/ENV PATH $PATH:$JAVA_HOME/bin# in the container to the external tomcatCMD 808 setting ["/ usr/local/tomcat/apache-tomcat-9.0.37/bin/catalina.sh" when the container starts the container "run"]
Build an image.
[root@localhost] # docker build-f / usr/local/dockerfile/Dockerfile-t mycentos:7 / root/Sending build context to Docker daemon 191.4MBStep 1 Running in 3f18aa4f3fb2Removing intermediate container 3f18aa4f3fb2 10: FROM centos:7-- > 7e6257c9f8d8Step 2 Running in 3f18aa4f3fb2Removing intermediate container 3f18aa4f3fb2 10: LABEL maintainer= "mrhelloworld.com"-- > Running in 3f18aa4f3fb2Removing intermediate container 3f18aa4f3fb2-- > 7364f68ca4abStep 3 Running in 3f18aa4f3fb2Removing intermediate container 3f18aa4f3fb2 10: WORKDIR / usr/local-- > Running in d9889152cfc4Removing intermediate container d9889152cfc4-- > d05bd2e09fa4Step 4 pound 10: RUN mkdir-p / usr/local/java & & mkdir-p / Usr/local/tomcat-> Running in 3bcd6ef78350Removing intermediate container 3bcd6ef78350-- > 4832abf9d769Step 5 usr/local/java 10: ADD jdk-11.0.6_linux-x64_bin.tar.gz / usr/local/java-> e61474bf7a76Step 6 usr/local/tomcat 10: ADD apache-tomcat-9.0.37.tar.gz / usr/local/tomcat-> 7110cdff7438Step 7 usr/local/tomcat 10: EXPOSE 8080-> Running in a4731c1cf77dRemoving intermediate container a4731c1cf77d-- > f893cefee00cStep 8 EXPOSE 10: ENV JAVA_HOME / usr/local/java/jdk- 11.0.6 /-> Running in f0cb08f390dbRemoving intermediate container f0cb08f390db-- > ff9f6acf6844Step 9 CMD 10: ENV PATH $PATH:$JAVA_HOME/bin-> Running in eae88cf841d0Removing intermediate container eae88cf841d0-> 4b9226a23b10Step 10 CMD ["/ usr/local/tomcat/apache-tomcat-9.0.37/bin/catalina.sh" "run"]-- > Running in ccf481045906Removing intermediate container ccf481045906-- > 9ef76a16441bSuccessfully built 9ef76a16441bSuccessfully tagged mycentos:7 image construction history docker history image name: label | IDdocker history mycentos:7
Create a container using the built image # create container docker run-di-- name mycentos7-p 8080 mycentos:7# enter container docker exec-it mycentos7 / bin/bash# test java environment variable [root@dcae87df010b /] # java-versionjava version "11.0.6" 2020-01-14 LTSJava (TM) SE Runtime Environment 18.9 (build 11.0.6+8-LTS) Java HotSpot (TM) 64-Bit Server VM 18.9 (build 11.0.6+8-LTS) Mixed mode) # visit http://192.168.10.10:8080/ to see the page description environment OK!
At this point, I believe you have a deeper understanding of "how to build Dockerfile with Docker images". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.