Idea is a powerful tool for Java development, SpringBoot is the most popular micro-service framework in the Java ecosystem, and Docker is the most popular container technology. So what chemical reaction will be produced when they are combined together?

I. Preparation before development

1. The installation of the Docker can refer to https://docs.docker.com/install/

2. Configure the Docker remote connection port

  vi /usr/lib/systemd/system/docker.service
Copy the code

Find ExecStart and add -h TCP ://0.0.0.0:2375 at the end, as shown in the figure below

3. Restart the docker

 systemctl daemon-reload
 systemctl start docker
Copy the code

4. Open ports

firewall-cmd --zone=public --add-port=2375/tcp --permanent  
Copy the code

5. Idea Install the plug-in and restart

6. Connect to the remote Docker

(1) Edit the configuration

(2) Fill in the remote Docker address

(3) If the connection is successful, the remote Docker container and image will be listed

2. New projects

1. Create a SpringBoot project

Project structure drawing

(1) Configure the 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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> < modelVersion > 4.0.0 < / modelVersion > < groupId > docker - demo < / groupId > < artifactId > com. The demo < / artifactId > < version > 1.0 - the SNAPSHOT < / version > < the parent > < groupId > org. Springframework. Boot < / groupId > <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> <relativePath /> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <docker.image.prefix>com.demo</docker.image.prefix> <java.version>1.8</java.version> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> < plugin > < groupId > com. The company < / groupId > < artifactId > docker maven - plugin < / artifactId > < version > 1.0.0 < / version > <configuration> <dockerDirectory>src/main/docker</dockerDirectory> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory>
                    <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> <plugin> <artifactId>maven-antrun-plugin</artifactId>  <executions> <execution> <phase>package</phase> <configuration> <tasks> <copy todir="src/main/docker" file="target/${project.artifactId}-${project.version}.${project.packaging}"></copy>
                        </tasks>
                     </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    </execution>
            </executions>
        </plugin>

       </plugins>
    </build>
<dependencies>
    <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>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4J </artifactId> <version>1.2.17</version> </dependency> </dependencies> </dependencies> </project>Copy the code

(2) Create a docker directory in SRC /main and create a Dockerfile file

FROM openjdk:8-jdk-alpine
ADD *.jar app.jar
ENTRYPOINT ["java"."-Djava.security.egd=file:/dev/./urandom"."-jar"."/app.jar"]
Copy the code

(3) Create the application.properties file in the resource directory

logging.config=classpath:logback.xml
logging.path=/home/developer/app/logs/
server.port=8990
Copy the code

Create the DockerApplication file

@SpringBootApplication public class DockerApplication { public static void main(String[] args) { SpringApplication.run(DockerApplication.class, args); }}Copy the code

Create DockerController file

@RestController
public class DockerController {
    static Log log = LogFactory.getLog(DockerController.class);

    @RequestMapping("/")
    public String index() {
        log.info("Hello Docker!");
        return "Hello Docker!"; }}Copy the code

(6) Add configurations

Image tag: specify the Image name and tag. The Image name is docker-demo, and the tag is 1.1 Bind ports: Bind host ports to internal container ports. The format is “host port” : “internal port”. Bind mounts: The host directory is attached to an internal directory in the container. The format is [host directory]:[Container internal directory]. The springboot project will log printed on the container/home/developer/app/logs/directory, will host machine directory mounted to the container after internal directory, then the log will be persistent container outside of the host machine directory.

(7) Maven packaging

(8)

(9) The operation is successful

(10) Browser access

(11) Log viewing

Since then, the springboot project has been successfully deployed to Docker through IDEA! It’s hard to imagine how easy it is to deploy a Javaweb project!

Author: Tao Zhang Good link: juejin.cn/post/684490… The copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please indicate the source.

Please click if you think it will help you“Praise”