Idea是Java開發利器,springboot是Java生態中最流行的微服務框架,docker是時下最火的容器技術,那麼它們結合在一塊兒會產生什麼化學反應呢?java
vi /usr/lib/systemd/system/docker.service
複製代碼
找到 ExecStart,在最後面添加 -H tcp://0.0.0.0:2375,以下圖所示web
systemctl daemon-reload
systemctl start docker
複製代碼
firewall-cmd --zone=public --add-port=2375/tcp --permanent
複製代碼
項目結構圖spring
<?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>
複製代碼
FROM openjdk:8-jdk-alpine
ADD *.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
複製代碼
logging.config=classpath:logback.xml
logging.path=/home/developer/app/logs/
server.port=8990
複製代碼
@SpringBootApplication
public class DockerApplication {
public static void main(String[] args) {
SpringApplication.run(DockerApplication.class, args);
}
}
複製代碼
@RestController
public class DockerController {
static Log log = LogFactory.getLog(DockerController.class);
@RequestMapping("/")
public String index() {
log.info("Hello Docker!");
return "Hello Docker!";
}
}
複製代碼
命令解釋
Image tag : 指定鏡像名稱和tag,鏡像名稱爲 docker-demo,tag爲1.1
Bind ports : 綁定宿主機端口到容器內部端口。格式爲[宿主機端口]:[容器內部端口]
Bind mounts : 將宿主機目錄掛到到容器內部目錄中。格式爲[宿主機目錄]:[容器內部目錄]。這個springboot項目會將日誌打印在容器 /home/developer/app/logs/ 目錄下,將宿主機目錄掛載到容器內部目錄後,那麼日誌就會持久化容器外部的宿主機目錄中。docker
自此經過idea 部署springboot項目到docker成功!不可思議,部署一個Javaweb項目居然如此簡單方便!apache
做者:陶章好
連接:juejin.im/post/5d0262…
來源:掘金
著做權歸做者全部。商業轉載請聯繫做者得到受權,非商業轉載請註明出處。瀏覽器