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 > Servers >
Share
Shulou(Shulou.com)06/01 Report--
How to use Docker to redefine Java virtualization deployment, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
Today, I would like to share with you the actual case of deploying java applications in docker.
> Dockerfiles
Dockerfile contains a series of instructions telling Docker how to build an image, which specifies the base point of the image and every detail of configuring the image. The following is an example of Dockerfile, which is the Dockerfile of the CentOS image.
Listing 1. CentOS Dockerfile
```sh
FROM scratch
MAINTAINER The CentOS Project-ami_creator
ADD centos-7-20150616_1752-docker.tar.xz /
# Volumes for systemd
# VOLUME ["/ run", "/ tmp"]
# Environment for systemd
# ENV container=docker
# For systemd usage this changes to / usr/sbin/init
# Keeping it as / bin/bash for compatibility with previous
CMD ["/ bin/bash"]
`
Most of the contents are comments, with four main commands:
1. FROM scratch: all Dockerfile inherits from a base image. In this case, the CentOS image inherits from the "scratch" image, which is the root of all images. This configuration is fixed, indicating that this is one of the root images of Docker.
2. The MAINTAINER...: MAINTAINER directive indicates the owner of the mirror, in this case CentOS Project.
3. The ADD centos...tar.xz:ADD instruction tells Docker to upload the specified file to the image, and if the file is compressed, it will be decompressed to the specified path. In this example, Docker uploads a Gzip package for the CentOS operating system and unzips it to the root directory of the system.
4. CMD ["/ bin/bash"]: finally, the CMD instruction tells Docker what command to execute, and in this case, it ends up in the Bourne Again Shell (bash) terminal.
Now that you know what Docker looks like, let's take a look at Tomcat's official Dockerfile. Figure 2 shows the structure of this file.
This architecture may not be as simple as you think, but we will learn it slowly next, in fact, it is very logical. As mentioned above, the root of all Dockerfile is scratch, and then the debian:jessie image is specified. This official image is based on a standard image. Docker does not need to reinvent the wheel, but creates a new image each time. Just continue to build a new image based on a stable image. In this example, debian:jessie is an official Debian Linux image, just like the CentOS above, it has only three lines of instructions.
Listing 2. Debian:jessie Dockerfile
```sh
FROM scratch
ADD rootfs.tar.xz /
CMD ["/ bin/bash"]
`
In the figure above, we also see that two additional images, CURL and Source Code Management, are installed, and the Dockerfile of the image buildpack-deps:jessie-curl is shown in listing 3.
Listing 3. Buildpack-deps:jessie-curl Dockerfile
```sh
FROM debian:jessie
RUN apt-get update & & apt-get install- y-- no-install-recommends\
Ca-certificates\
Curl\
Wget\
& & rm-rf / var/lib/apt/lists/*
`
This Dockerfile uses apt-get to install curl and wget so that the image can download software from other servers. The RUN directive tells Docker to execute specific commands in the running instance, in this case, it updates all libraries (apt-get update), and then executes apt-get install to install curl and wget.
The Dockerfile for buildpack-deps:jessie-scp is shown in listing 4.
Listing 4. Buildpack-deps:jessie-scp Dockerfile
```sh
FROM buildpack-deps:jessie-curl
RUN apt-get update & & apt-get install- y-- no-install-recommends\
Bzr\
Git\
Mercurial\
Openssh-client\
Subversion\
& & rm-rf / var/lib/apt/lists/*
`
This [Dockerfile] installs source management tools such as Git,Mercurial and Subversion.
Java's [Dockerfile is more complex, as shown in listing 5.
Listing 5. Java Dockerfile
```sh
FROM buildpack-deps:jessie-scm
# A few problems with compiling Java from source:
# 1. Oracle. Licensing prevents us from redistributing the official JDK.
# 2. Compiling OpenJDK also requires the JDK to be installed, and it gets
# really hairy.
RUN apt-get update & & apt-get install-y unzip & & rm-rf / var/lib/apt/lists/*
RUN echo 'deb jessie-backports main' > / etc/apt/sources.list.d/jessie-backports.list
# Default to UTF-8 file.encoding
ENV LANG C.UTF-8
ENV JAVA_VERSION 8u66
ENV JAVA_DEBIAN_VERSION 8u66-b01-1~bpo8+1
# see
# and
ENV CA_CERTIFICATES_JAVA_VERSION 20140324
RUN set-x\
& & apt-get update\
& & apt-get install-y\
Openjdk-8-jdk= "$JAVA_DEBIAN_VERSION" > ca-certificates-java= "$CA_CERTIFICATES_JAVA_VERSION"\
& & rm-rf / var/lib/apt/lists/*
# see CA_CERTIFICATES_JAVA_VERSION notes above
RUN / var/lib/dpkg/info/ca-certificates-java.postinst configure
# If you're reading this and have any feedback on how this image could be
# improved, please open an issue or a pull request so we can discuss it!
`
To put it simply, the Dockerfile uses the security parameter to execute apt-get install-y openjdk-8-jdk to download and install Java, while the ENV directive configures the system's environment variables.
Finally, listing 6 is Tomcat's [Dockerfile.
Listing 6. Tomcat Dockerfile
```sh
FROM java:7-jre
ENV CATALINA_HOME / usr/local/tomcat
ENV PATH $CATALINA_HOME/bin:$PATH
RUN mkdir-p "$CATALINA_HOME"
WORKDIR $CATALINA_HOME
# see
RUN gpg-keyserver pool.sks-keyservers.net-recv-keys\
05AB33110949707C93A279E3D3EFE6B686867BA6\
07E48665A34DCAFAE522E5E6266191C37C037D42\
47309207D818FFD8DCD3F83F1931D684307A10A5\
541FBE7D8F78B25E055DDEE13C370389288584E7\
61B832AC2F1C5A90F0F9B00A1C506407564C17A3\
79F7026C690BAA50B92CD8B66A3AD3F4F22C4FED\
9BA44C2621385CB966EBA586F72C284D731FABEE\
A27677289986DB50844682F8ACB77FC2E86E29AC\
A9C5DF4D22E99998D9875A5110C01C5A2F6059E7\
DCFD35E0BF8CA7344752DE8B6FB21E8933C60243\
F3A04C595DB5B6A5F1ECA43E3B7BBB100D811BBE\
F7DA48BB64BCB84ECBA7EE6935CD23C10D498E23
ENV TOMCAT_MAJOR 8
ENV TOMCAT_VERSION 8.0.26
ENV TOMCAT_TGZ_URL
RUN set-x\
& & curl-fSL "$TOMCAT_TGZ_URL"-o tomcat.tar.gz\
& & curl-fSL "$TOMCAT_TGZ_URL.asc"-o tomcat.tar.gz.asc\
& & gpg-- verify tomcat.tar.gz.asc\
& & tar-xvf tomcat.tar.gz-strip-components=1\
& & rm bin/*.bat\
& & rm tomcat.tar.gz*
EXPOSE 8080
CMD ["catalina.sh", "run"]
`
Strictly speaking, Tomcat uses the parent Dockerfile of Java 7 (the latest Java version is 8 by default). This Dockerfile sets the CATALINA_HOME and PATH environment variables, then creates a new CATALINA_HOME directory with the mkdir command, the WORKDIR directive changes the current working path to CATALINA_HOME, and the RUN directive executes a series of commands on the same line:
1. Download the Tomcat package.
two。 Download the file check code.
3. Verify that the downloaded file is correct.
4. Extract the Tomcat package.
5. Delete all batch files (we are running on Linux).
6. Delete the archive file.
Writing these commands on the same line is a command corresponding to Docker, and finally Docker will cache the results of the execution. Docker has a strategy to detect when the image needs to be rebuilt, and to verify that the instructions in the construction process are correct. When an instruction causes the mirror to change, Docker caches the results of each step, and Docker starts the image generated by the last correct instruction.
The EXPOSE directive causes Docker to expose the specified port when it starts a container, as we did when we started it before, we need to tell Docker which physical port will be mapped to the container (the-p parameter), and EXPOSE is used to define the Docker container port. Finally, Dockerfile uses the catalina.sh script to start Tomcat.
A brief review
Building Tomcat from scratch with Dockerfile is a long process, so let's summarize the steps so far:
1. Install Debian Linux.
two。 Install curl and wget.
3. Install source management tools.
4. Download and install Java.
5. Download and install Tomcat.
6. Expose port 8080 of the Docker instance.
7. Start Tomcat with catalina.sh.
Now that you should be a Dockerfile expert, we will try to build a custom Docker image next.
> deploy custom applications to Docker
Because this guide focuses on how to deploy Java applications in Docker, rather than the application itself, I will build a simple Hello World servlet. You can get this project from [GitHub]. The source code is nothing special, just an output "Hello World!" Servlet. Even more interesting is the corresponding [Dockerfile], as shown in listing 7.
Listing 7. Dockerfile for Hello World servlet
```sh
FROM tomcat
ADD deploy / usr/local/tomcat/webapps
`
It may look different, but you should be able to understand what the above code does:
* FROM tomcat indicates that the Dockerfile is built based on Tomcat image.
* ADD deploy tells Docker to copy the "deploy" directory in the local file system to the / usr/local/tomcat/webapps path in the Tomcat image.
Compile the project locally using the maven command:
```sh
Mvn clean install
`
This will generate a war package, target/helloworld.war. Copy this file to the project's docker/deploy directory (you need to create it first). Finally, you need to use the above Dockerfile to build the Docker image, and execute the following command in the project's docker directory:
```sh
Docker build-t lygado/docker-tomcat.
`
This command takes Docker from the current directory (with a period. Build a new image and label lygado/docker-tomcat with "- t". In this case, lygado is my DockerHub user name and docker-image is the image name (you need to replace it with your own user name). To see if the build is successful, you can execute the following command:
```sh
$docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
Lygado/docker-tomcat latest ccb455fabad9 42 seconds ago 849.5 MB
`
Finally, you can load the image with the following command:
```sh
Docker run-d-p 8080 lygado/docker-tomcat
`
After this instance is launched, you can access it with the following URL (replace the IP in URL with the IP of your virtual machine):
```sh
Http://192.168.99.100:8080/helloworld/hello
`
Again, you can use the container's ID to terminate the instance.
Docker push
Once you have built and tested your Docker image, you can push it to your DockerHub account:
```sh
Docker push lygado/docker-tomcat
`
In this way, your image can be accessed all over the world, and of course, for privacy, you can also push it to a private Docker repository.
Next, we will integrate Docker into the application building process, with the goal of producing a Docker image containing the application after building the application.
> integrating Docker into the Maven build process
In the previous section, we created a custom Dockerfile and deployed the WAR package into it. This means copying the WAR package from the project's target directory to the docker/deploy directory and running docker from the command line. It doesn't take much effort, but if you need to change and test the code frequently, you will find the process cumbersome. And if you need to build an application on a CI server and produce a Docker image, you need to figure out how to integrate Docker and CI tools.
Now let's try a more efficient approach, using the Maven and Maven Docker plug-ins to build a Docker image.
My use cases are as follows:
1. I can create a Tomcat-based Docker image for deployment of my application.
two。 Can build it on its own during testing.
3. Can be integrated into pre-integration testing and post-integration testing.
Docker-maven-plugin meets these needs and is easy to use and understand.
About the Maven Docker plug-in
The plug-in itself is well documented, and here are two main components:
1. Configure the construction and operation of the Docker image in POM.xml.
two。 Describes which files to include in the image.
Listing 8 is the configuration of the plug-in in POM.xml, defining the configuration for building and running the image.
Listing 8. Build section of the POM file, Docker Maven plug-in configuration
```xml
Helloworld
Org.jolokia
Docker-maven-plugin
0.13.4
Tcp://192.168.99.100:2376 / Users/shaines/.docker/machine/machines/default
True
Lygado/tomcat-with-my-app:0.1
Tomcat
Tomcat
Dir
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.