Steps to deploy Docker project in IDEA

Steps to deploy Docker project in IDEA
Now most projects have begun to be deployed on Docker, but the deployment process is still a bit troublesome, so this article wants to explain how to use IDEA for one-click deployment.

Docker configuration

Modify the configuration file

Open the Docker configuration file:
vim /usr/lib/systemd/system/docker.service
Comment out the following line:
# ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Write a new line:
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
Reload the configuration file and start:
systemctl daemon-reload
systemctl start docker

As shown in the following figure:

Configuring Docker with IDEA

Install Docker plugin

Configure Docker information

To configure Docker in the settings, you need to configure the API URL. When Connection successful appears below:

Project Construction

Create a new springboot project

To build a project through IDEA, you don’t need to choose anything, just keep clicking:

Modify the pom file

The most important two points are:

1. Add in the properties tag

<docker.image.prefix>demo</docker.image.prefix>

2. Add a new plugin tag

<plugin>
 <groupId>com.spotify</groupId>
 <artifactId>docker-maven-plugin</artifactId>
 <version>1.2.1</version>
 <configuration>
   <imageName>${docker.image.prefix}/${project.artifactId}</imageName>
   <dockerDirectory></dockerDirectory>
   <resources>
     <resource>
       <targetPath>/</targetPath>
       <directory>${project.build.directory}</directory>
       <include>${project.build.finalName}.jar</include>
     </resource>
   </resources>
 </configuration>
</plugin>

Here is the complete pom file:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>2.2.1.RELEASE</version>
   <relativePath/> <!-- lookup parent from repository -->
 </parent>
 <groupId>com.example</groupId>
 <artifactId>demo</artifactId>
 <version>0.0.1</version>
 <name>demo</name>
 <description>Demo project for Spring Boot</description>

 <properties>
   <java.version>1.8</java.version>
   <docker.image.prefix>demo</docker.image.prefix>
 </properties>

 <dependencies>

   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter</artifactId>
   </dependency>

   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
   </dependency>

   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-test</artifactId>
     <scope>test</scope>
     <exclusions>
       <exclusion>
         <groupId>org.junit.vintage</groupId>
         <artifactId>junit-vintage-engine</artifactId>
       </exclusion>
     </exclusions>
   </dependency>
 </dependencies>

 <build>
   <plugins>
     <plugin>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-maven-plugin</artifactId>
     </plugin>
     <plugin>
       <groupId>com.spotify</groupId>
       <artifactId>docker-maven-plugin</artifactId>
       <version>1.2.1</version>
       <configuration>
         <imageName>${docker.image.prefix}/${project.artifactId}</imageName>
         <dockerDirectory></dockerDirectory>
         <resources>
           <resource>
             <targetPath>/</targetPath>
             <directory>${project.build.directory}</directory>
             <include>${project.build.finalName}.jar</include>
           </resource>
         </resources>
       </configuration>
     </plugin>
   </plugins>
 </build>

</project>

Create a new Dockerfile

You need to create a new Dockerfile file in the root directory

#Specify the base image and customize it FROM java:8

#Maintainer information MAINTAINER zhouzhaodong <[email protected]>

#The /tmp directory here will be automatically mounted as an anonymous volume at runtime, and any information written to /tmp will not be recorded in the container storage layer VOLUME /tmp

#Copy target/demo-1.0.0.jar in the context directory to the container COPY target/demo-0.0.1.jar demo-1.0.0.jar

#Execute in bash mode to make demo-1.0.0.jar accessible. #RUNCreate a new layer and execute these commands on it. After the execution is completed, commit the changes of this layer to form a new image.
RUN bash -c "touch /demo-1.0.0.jar"

#Declare the service port provided by the runtime container. This is just a declaration. The application will not open the service of this port at runtime because of this declaration. EXPOSE 8080

#Specify the container startup program and parameters <ENTRYPOINT> "<CMD>"
ENTRYPOINT ["java","-jar","demo-1.0.0.jar"]

Create a new controller file

Don't forget to add web dependency in pom file.

@RestController
public class testController {

  @RequestMapping("/")
  public String test(){
    return "test Docker";
  }

}

Maven packaging

Install packaging:


Generate jar package:

New Configuration

Create a new Dockerfile run configuration:


Add the following information, find the Dockerfile file you wrote, and configure the port mapping:

Generate a Docker image and run it

Simply run the newly created Dockerfile run configuration:


After the operation is successful, the log window will show the project operation information:


Accessing the corresponding address will display the information we entered:

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed steps to deploy SpringBoot projects using Docker in Idea
  • Detailed steps for one-click automated deployment of Linux+Docker+SpringBoot+IDEA
  • Java remote one-click deployment of springboot to Docker through Idea
  • Deploy the springboot project to docker based on idea
  • Detailed process of deploying Docker to WSL2 in IDEA

<<:  Two ways to clear table data in MySQL and their differences

>>:  Implementation of VUE infinite level tree data structure display

Recommend

Briefly understand the two common methods of creating files in Linux terminal

We all know that we can use the mkdir command to ...

An example of how to implement an adaptive square using CSS

The traditional method is to write a square in a ...

Solution to garbled display of Linux SecureCRT

Let's take a look at the situation where Secu...

How to configure multiple tomcats with Nginx load balancing under Linux

The methods of installing nginx and multiple tomc...

Basic usage of wget command under Linux

Table of contents Preface 1. Download a single fi...

js to realize simple shopping cart function

This article example shares the specific code of ...

Mysql optimization Zabbix partition optimization

The biggest bottleneck of using zabbix is ​​the d...

Docker connection mongodb implementation process and code examples

After the container is started Log in to admin fi...

MySQL 5.7 installation and configuration tutorial under CentOS7 64 bit

Installation environment: CentOS7 64-bit MINI ver...

Vue implements card flip carousel display

Vue card flip carousel display, while switching d...

...

JavaScript design pattern learning adapter pattern

Table of contents Overview Code Implementation Su...

CSS to achieve compatible text alignment in different browsers

In the front-end layout of the form, we often nee...