In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how Java engineers develop on Docker". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Installation
Now there are many Java Web frameworks, choose a very small framework, choose Spark! It is a miniature Sinatra framework based on Java-8. If you read the documentation for Spark, you will know that it uses Maven as its build tool.
In this example, the hierarchical file system (UnionFS) of Maven and Docker is used to install everything from scratch. At the same time, it takes some time to recompile the changed content.
Therefore, the preparatory knowledge you need is: no Java, no Maven, just Docker.
Source code and configuration files
In this example, you have to add the following three files:
* Maven configuration: `pom.xml`
* Java file: `Hello.java`
* `Dockerfile`
If you think the space is too long, you can directly [clone the following repo]:
`
Git clone https://github.com/giantswarm/sparkexample
`
Pom.xml
The file `pom.xml` contains a basic Maven configuration. This is very familiar to everyone! It uses the Java1.8 compiler to configure Spark dependencies and creates a large jar package with all the dependencies. How big is it? But it must be bigger than Xiyun's micro-mirror! If you don't know Xiyun's micro-image, you will soon be OUT. Micromirror address: http://csphere.cn/hub
Hello.java
The file `pom.xml` defines the main class (main class) of a class called `sparkexample.Hello`. Create a Hello.java file under the subpath `src/main/java/sparkexample/ `.
As you can see, this is the latest Java code: static imports and lambda expressions, making the example very compact. The class contains a main method (the main method) that responds to the root request ("/"). As common as HelloWorld, the response is a simple string.
Dockerfile
The last and most important file: `Dockerfile`
`
FROM java:8
# Install maven
RUN apt-get update
RUN apt-get install-y maven
WORKDIR / code
# Prepare by downloading dependencies
ADD pom.xml / code/pom.xml
RUN ["mvn", "dependency:resolve"]
RUN ["mvn", "verify"]
# Adding source, compile and package into a fat jar
ADD src / code/src
RUN ["mvn", "package"]
EXPOSE 4567
CMD ["/ usr/lib/jvm/java-8-openjdk-amd64/bin/java", "- jar", "target/sparkexample-jar-with-dependencies.jar"]
`
Install Maven and build jar package based on `java8`. (if you want to build a docker image of jdk,jre, please see http://csphere.cn/hub/jdk). Build by adding pom.xml files to resolve dependencies.
In practice, we will find that if we want to recompile app without changing pom.xml, the previous step has been cached and only the last step has been run, which will make the recompilation faster. The advantage of docker is very obvious!
Create and run
With the above three files, it becomes very easy to create a Docker image:
$docker build-t csphere/sparkexample.
Note: it takes a while to build for the first time. You need to download and install Maven first, and also download all project dependencies. It only takes a few seconds for each compilation to start in the future, don't ask me why it only takes a few seconds, because there is a cache.
After the image is created, start the container:
$docker run-d-p 4567 4567 csphere/sparkexample
Test:
$curl localhost:4567
Hello from sparkjava.com
Creating a mirror is as simple as that! Now, you can modify the source code and recompile it, isn't it simple and great?
This is the end of the content of "how Java engineers develop on Docker". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.