Use Docker to run Spring-boot apps in IntelliJ idea

Source: Internet
Author: User
Tags bind docker run

There are a lot of articles about using Docker in IntelliJ idea, but mostly the following way

1. Using a Web App (War package) for deployment

2. Use of Dockerfile under the same project

3. Configure Pom.xml to put the jar file into the reporting Dockerfile directory

But the actual project

1. More is the use of JAR packages

2. It is hoped that existing projects will make Docker deployment more convenient, but do not want to make changes on the original project

So, based on the actual situation, this paper makes an exploration to see if the above requirements can be met.

--------------------------------------------I'm a split line---------------------------------------------------- running environment

Docker for Windows (Windows 10)


First step: Install Docker Plugin in IntelliJ idea

This step in a lot of articles are introduced, here is not detailed, a picture, set the path to File->settings

Step Two: Envision the application pattern

Based on the "want existing projects to be more convenient for Docker deployment, but do not want to make changes on the original project", we need to create 2 projects,

A springboot project, we are temporarily named "Spring-boot-maker",

A Docker project, temporarily named "Docker-maker".

Spring-boot-maker project for its own application development, not involving any Docker-related content

Docker-maker project, make the Spring-boot-maker project a docker image (image) and start the run as Docker.

Two items are independent of each other and cannot be accessed from one another.


Step Three: Create a springboot project

Create a Springboot project Spring-boot-maker

Pom.xml

<?xml version= "1.0"Encoding= "UTF-8"?> <project xmlns= "http://maven.apache.org/POM/4.0.0" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" Xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>docker</groupId> <artifactid>s Pring-boot-maker</artifactid> <version>1.0-SNAPSHOT</version> <packaging>jar</packagi
    Ng> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifa Ctid>spring-boot-starter-parent</artifactid> <version>1.5.10.RELEASE</version> </paren t> <dependencies> <dependency> <groupid>org.springframework.boot</groupid > <artifactId>spring-boot-starter-web</artifactId> </dependency> </depende ncies> <build> <plugins> <plugin> &LT;GROUPID&GT;ORG.SPRINGF Ramework.boot</groupid> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> &lt ;/plugins> </build> </project>

Note that the:<build> block must be written, otherwise the packaged jar will not run

Springbootmakerapplication.java (startup class, very simple one)

Package docker.spring.boot;

Import org.springframework.boot.SpringApplication;
Import Org.springframework.boot.autoconfigure. springbootapplication;

@SpringBootApplication
 public class Springbootmakerapplication {public
    static void Main (string[] args) {
        Springapplication.run ( Springbootmakerapplication.class, args);
    }
}

Springbootmakercontroller.java (Controller Class)

Package Docker.spring.boot.controller;

Import Org.springframework.stereotype. Controller;
Import Org.springframework.web.bind.annotation. pathvariable;
Import Org.springframework.web.bind.annotation. requestmapping;
Import Org.springframework.web.bind.annotation.RequestMethod;
Import Org.springframework.web.bind.annotation. responsebody;

@Controller
 @RequestMapping "/spring/boot")
public class Springbootmakercontroller {

    @RequestMapping"/hello/{name}", method = Requestmethod.get)
    @ResponseBody
    public string Hello (string name ) {
        + name;
    }
    
}

Once the project is created, we launch it and try it out in the browser to make sure the app is accessible.


Fourth Step: Create a Docker project

The Spring-boot project is a MAVEN project, and the Docker project is a directory.

We created the directory Spring-boot-maker under the project to indicate that this was created for the Spring-boot-maker project.

Create a file under the Spring-boot-maker directory Dockerfile

Because the Add and copy commands in Dockerfile only support relative paths, they cannot be manipulated with absolute paths.

While modifying the Pom.xml file in the Spring-boot-maker project, the jar file is packaged directly into the Dockerfile directory, which does not conform to our intention of "not modifying the original project".

So the idea is: The Spring-boot-maker project is compiled and packaged, the package of the jar files copied to the Dockerfile directory, these are executed by the Bat (batch) file, Dockerfile to perform the creation of image (image) operation.


Docker.run.bat

cd/d D:\SourceCode\workspace\test-for-docker\spring-boot-maker
mvn clean package-u-dskiptests
CD /d%~dp0
del *.jar
copy D:\SourceCode\workspace\test-for-docker\spring-boot-maker\target\ Spring-boot-maker-*.jar spring-boot-maker-*.jar
ren spring-boot-maker-*.jar spring-boot-maker.jar
Docker Build-t Spring-boot-maker.
Docker run-d-P 8080:8080--name spring-boot-maker Spring-boot-maker


Dockerfile

# from Java image, Version:8
java:8

# Mount app directory
VOLUME/app

# COPY or ADD to Imag E
spring-boot-maker.jar app.jar

-"Touch/app.jar"
 
8080["java""-jar""App.jar"]

Executes the bat file directly, starting the container successfully.


Access in the browser, success.



Above, we successfully achieved the desired requirements, without affecting the original application on the basis of the completion of the Docker transformation.

Git address: https://github.com/SimonZhang1980/docker4springboot


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.