In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge about "how to use Dockerfile to customize Java Web mirror". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
I. Foreword
How to use Docker to build Java Web runtime environment
1. Start the container:
docker run -i: indicates running the container in "interactive mode"-t: indicates that the container will enter its command line after starting-v: indicates which local directory needs to be mounted to the container, format: -v :
Enter container, configure environment, exit
2. View all containers:
docker container ls -a or docker ps -a
The syntax of docker commit is:
docker commit [options] [[:]]
--author "wwx" \--message "Modified default web page" \
docker commit 57c312bbaad1 huangyong/javaweb:0.1
4. Start the container:
docker run
Docker commit is used with caution, and it is composed of commit images, which means that all operations on images are black box operations, and the generated images are also called black box images. If you use docker commit to create an image and modify it later, each modification will make the image more bloated, and the deleted upper layer will not be lost, and will always follow the image, even if it is not accessible at all. This will make mirroring more bloated.
Dockerfile is a text file containing instructions, each instruction builds a layer, so the content of each instruction describes how the layer should be built.②Dockerfile command detailed explanation #Specify the base image FROM Dockerfile must be the first command FROM scratch. Not based on any image, the next command will exist as the first layer of the image #Specify maintainer information MAINTAINER format: MAINTAINER #Execute command line command RUN Define how each layer should be built (not writing Shell script) Each RUN = Start a container, execute command, and then submit storage layer file Change execution environment of two-line RUN command Different formats: 1) Shell format: RUN #Similar to command line input 2) exec format: RUN ["executable file", "parameter 1", "parameter 2"] #Similar function calls end of line\change line beginning #comments && command concatenation #copy file COPY format: 1) COPY... 2) COPY ["",... ""] can be multiple or even wildcard #The relative path of the context path can be either an absolute path within the container or a relative path to the working directory (The working directory can be specified with WORKDIR command)#Higher-level copy file ADD can be a URL, if it is gzip , bzip2 and xz case, ADD command will automatically decompress this compressed file to go to all file copy using COPY command, only in the case of automatic decompression use ADD#container start command CMD container is the process. Since it is a process, you need to specify the program and parameters to run when starting. CMD command is used to specify the default container main process startup command For containers, the startup program is the container application process, containers exist for the main process, the main process exits, the container loses the meaning of existence, thus exiting, other auxiliary processes are not what it needs to care about. Format: 1) Shell format: CMD 2) exec format: CMD ["executable file", "parameter 1", "parameter 2"...] Exec format is generally recommended. This format will be parsed into JSON array when parsing, so be sure to use double quotes "instead of single quotes CMD echo $HOME. In actual execution, it will be changed to: CMD [ "sh", "-c", "echo $HOME" ]#Entry point ENTRYPOINT is the same as CMD. Both are specified. When the container startup program and parameters are actually executed, they will become: "" #At startup, executable files can be passed ENTRYPOINT ["docker-entrypoint.sh"] #Apply pre-run preparation, specify ENTRYPOINT as docker-entrypoint.sh script, and can pass parameters to service script at image startup #Set environment variable ENV format: 1) ENV 2) ENV = =...# The difference between ARG and ENV is that ARG sets environment variables for the build environment that will not exist when the container runs in the future. Format: ARG [=] You can use--build-arg = in the docker build command to overwrite #Define anonymous volume VOLUME In order to prevent users from forgetting to mount directories saved by dynamic files as volumes at runtime, specify that some directories are mounted as anonymous volumes, so that if users do not specify mounts at runtime, their applications can run normally without writing a large amount of data to the container storage layer. Format: 1) VOLUME 2) VOLUME ["", "...] VOLUME /data The/data directory here will be automatically mounted as an anonymous volume at runtime, and any information written to/data will not be recorded in the container storage layer-v mydata:/datamydata This named volume is mounted to/data, replacing the anonymous volume defined in Dockerfile Mount configuration #Declare port EXPOSE Declare runtime container Provide service port #Specify working directory WORKDIR Change the working directory format of each layer in the future: WORKDIR is equivalent to cd... WORKDIR /aWORKDIR bWORKDIR cRUN pwd The final path is/a/b/c#Specify the current user USER command is similar to WORKDIR, both change the environment state and affect the future layers USER only helps you switch to the specified user, this user must be established in advance, otherwise it cannot be switched.# HEALTHCHECK Format: 1) HEALTHCHECK [Options] CMD #Set commands to check container health 2) HEALTHCHECK NONE #If the base image has a health check command, use this line to block its health check command HEALTHCHECK supports the following options: --interval=: interval between two health checks, default is 30 seconds;--timeout=: timeout time for health check command to run, if this time exceeds, this health check is considered failed, default is 30 seconds;--retraces =: When the specified number of consecutive failures occurs, the container status is considered unhealthy, default 3 times. As with CMD , ENTRYPOINT, HEALTHCHECK can occur only once, and if more than one is written, only the last one takes effect. HEALTHCHECK --interval=5s --timeout=3s \ CMD curl -fs http://localhost/ ||exit 1#ONBUILD will only be executed when the current mirror is used as the base mirror to build the next level mirror Format: ONBUILD makes a base image, the base image updates, each project does not need to synchronize the changes of Dockerfile, after rebuilding, it inherits the updates of the base image ③ Build image #Build image docker build [option] The image is not built locally, but on the server side, that is, the image is built in Docker engine. So in this client/server architecture, how do you get local files to the server? When building, the user will specify the path to build the image context. After the docker build command knows this path, it will package all the contents under the path and upload them to the Docker engine. Once Docker engine receives this context package, it expands to get everything it needs to build the image. Beginners often ask why COPY../ package. json/app or COPY /opt/xxxx /app doesn't work because these paths are out of context and Docker engine can't get files at these locations. For example: COPY ./ package. json/app/is the package.json in the replication context directory #COPY The path of the source file in such directives is the relative path of the context path-f../ Dockerfile.php parameter specifies a file as Dockerfile Usage of other docker builds Build directly with Git repo:Docker will go to git clone the project, switch to the specified branch, and enter the specified directory to start building with the given tar package Build: Docker engine will download this package, and automatically decompress, using it as a context to start building Dockerfile
1.0.0 Dockerfile
#VERSION INFORMATION FROM CENTOS:7MAINTAINER wuweixiang #SETUP WORKDIR /var/#ADD jdk, tomcatADD jdk-8u191-linux-x64.tar.gz .# ADD http://mirrors.hust.edu.cn/apache/tomcat/tomcat-8/v8.5.35/bin/apache-tomcat-8.5.35.tar.gz .ADD apache-tomcat-8.5.35.tar.gz .# Set environment variable ENV JAVA_HOME /var/jdk1.8.0_191ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/binENV TIME_ZONE Asia/Shanghai#Change Time Zone RUN set -x \&& echo "${TIME_ZONE}" > /etc/timezone \&& ln -sf /usr/share/zoneinfo/${TIME_ZONE} ONE} /etc/localtime#Open internal service port EXPOSE 8080#Start tomcat server CMD ["/var/apache-tomcat-8.5.35/bin/catalina.sh","run"] && tail -f /var/apache-tomcat-8.5.35/logs/catalina.out build method (mirror is pushed, this step can be ignored)
Install Linux with git
sudo yum install git
② Clone project source code
git clone https://gitee.com/wuweixiang/javaweb-docker.git
③ Construct mirror image
Go to/javaweb-docker/dockerfile-java8-tomcat8 directory:
docker build -t [[:]] . #Create image repository name: often appears in the form of a two-segment path, such as wuweixiang/javaweb:1.0.0, the former Docker account username, the latter is often the corresponding software name. Label: Specify which version of mirror is required, default latest. use
1. Install docker on the newly purchased server and execute the following instructions:
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
2. Create the/var/webapps/directory and put the war package in that directory.
3. Run the following command to achieve deployment.
docker run -d -p 80:8080 \-v /var/webapps/:/var/apache-tomcat-8.5.35/webapps/ \--name \wuweixiang/javaweb:1.0.0
Note:
mount path/var/webapps/upload location for current war
uses examples
1. Download a war package to the mount path/var/webapps/:
It can be seen that the war package is automatically decompressed.
2. Visit 112.74.185.172/finder-web-2.4.9
summary
This deployment mode is different from the previous deployment mode, omitting the configuration process of jdk and tomcat environment, as long as the war package is uploaded.
"How to use Dockerfile custom Java Web mirror" content introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
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.