Idea is a Java development tool, springboot Java ecosystem is the most popular micro-services framework, docker is among the most fire container technology, they combine what would be the chemistry of it?
First, the development of the former ready
1. Docker installation can refer https://docs.docker.com/install/
2. Configure docker remote connection port
vi /usr/lib/systemd/system/docker.service
复制代码
Found ExecStart , the last position -H TCP: //0.0.0.0: 2375 , as shown in FIG.
3. Restart docker
systemctl daemon-reload
systemctl start docker
复制代码
4. open ports
firewall-cmd --zone=public --add-port=2375/tcp --permanent
复制代码
5. Idea install the plug, reboot
6. Connect remote docker
(1) Edit Configuration
(2) Fill remote docker address
(3) successful connection, the container will be listed and remote mirroring docker
Second, new projects
1. Create a project springboot
Program structure.
(1) pom configuration 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.demo</artifactId>
<version>1.0-SNAPSHOT</version>
<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.spotify</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>
</project>
复制代码
(2) Create a docker directory under src / main directory, file and create Dockerfile
FROM openjdk:8-jdk-alpine
ADD *.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
复制代码
(3) create a file in the resource directory application.properties
logging.config=classpath:logback.xml
logging.path=/home/developer/app/logs/
server.port=8990
复制代码
(4) Create a file DockerApplication
@SpringBootApplication
public class DockerApplication {
public static void main(String[] args) {
SpringApplication.run(DockerApplication.class, args);
}
}
复制代码
(5) 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!";
}
}
复制代码
(6) increase the allocation
Command interpreter
Image tag: Specifies the image name and Tag , image name Docker-Demo , Tag is 1.1
the Bind the ports: Binding host port to the interior of the container port. Format [host port]: [vessel internal port]
the Bind mounts: The host directory linked to the interior of the container into the directory. Format [host Catalog]: [vessel internal directory]. This project will log springboot print container / home / developer / app / logs / after the directory, will host directory is mounted to the container internal directory, the log will persist outside of the container host directory.
(7) Maven package
(8) run
First pull the base image, and then packaged mirror, and deploy the image to a remote docker run Here we can see the image name as docker-demo: 1.1, docker container docker-server(9) run successfully
(10) browser access
(11) Log Viewer
Since then deploy springboot project idea through to docker success! Hard to imagine a Javaweb deployment project could be so easy!