How to use Docker for Java entry-level development

Source: Internet
Author: User



"Editor's note" This is an entry-level tutorial , recommended for Java developers to read, and a simple example of how to develop Java in Docker is shown in this article. You don't need maven, you don't need a JDK, you just have to give your little buddy a dockerfile and leave the rest to Docker to do it.

This week, I had a meeting with Anna, Stephan and Timo in W-jax, Munich, about enterprise technology, especially for Java. I didn't realize there were so many people interested in Docker, but how did Java develop on Docker? I personally prefer a short example, which can help you understand a technology by using a framework that contains several small files. Unfortunately, this is difficult to achieve in the Java world, as most of the examples require an IDE and an appropriate understanding of the web framework. In this article, I'll try to use a short example to help you quickly learn how to do Java development in Docker.

Get readywork

There are a lot of Java Web frameworks now, but I'm not going to use them here. All I want is a small frame so I chose Spark, which is a very small framework based on Java 8. Spark uses MAVEN as the build tool.

Source code and configuration files

In this example you will add three files:

    • Maven Configuration files: Pom.xml
    • A Java class: Hello.java
    • A dockerfile

If a reader cannot wait, clone this repo:

Https://github.com/giantswarm/sparkexample

Below we will explain in detail the structure of these three files, you can get a quick look at this video. (Readers can see the video in the original text, watching the video basic can learn how to do)

Pom.xml

The pom.xml contains some basic MAVEN configurations, such as configuring the Java 8 that spark relies on. It encapsulates all the dependencies into a large jar package. I'm not a MAVEN expert, so I can't write the examples easier and smoother to make them more popular. This is the Pom file address, you can look at my configuration: Https://gist.github.com/luebke ... m-xml

Hello.java

Pom.xml file Definition MainClass is Sparkexample.hello, we need to create a Hello.java file in the src/main/java/sparkexample/directory.

Dockerfile

Finally, let's write the Dockerfile file, which uses the Java image (JAVA:ORACLE-JAVA8) and starts with installing MAVEN. Next it will install the project dependencies. We parse these dependencies through Pom.xml, which, as you can see, allows Docker to cache these dependencies. Next, we'll compile and package our app and launch the app. If we rebuild the app, the Pom.xml file doesn't have any changes, the previous steps are cached, and the app is launched directly to the last step. This can speed up the rebuild of your app.

Create and run

Once these three files have been completed, it is easy to create a docker image.

  
 
   
  
  1. - T Giantswarm / Sparkexample

Note: The first boot will take some time because it installs maven and downloads all dependencies. It will take a few seconds before it starts, because everything is already cached. After the image is created, create the container with the following command:

  
 
   
  
  1. --4567:4567 giantswarm/ sparkexample

Use the following command to access:

  
 
   
  
  1. Curl localhost : 4567 from sparkjava. com

Now you can change the source (return what you want to return) and rebuild it, doesn't that look great?

Reprint Please specify: Play earn music? How to use Docker for Java entry-level development

How to use Docker for Java entry-level development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.