做者:陶章好javascript
juejin.im/post/5d026212f265da1b8608828bjava
IDEA是Java開發利器,springboot是Java生態中最流行的微服務框架,docker是時下最火的容器技術,那麼它們結合在一塊兒會產生什麼化學反應呢?web
1、開發前準備面試
能夠參考:spring
https://docs.docker.com/install/docker
vi /usr/lib/systemd/system/docker.service
找到 ExecStart,在最後面添加 -H tcp://0.0.0.0:2375,以下圖所示apache
systemctl daemon-reload systemctl start docker
firewall-cmd --zone=public --add-port=2375/tcp --permanent
一、編輯配置瀏覽器
二、填遠程docker地址springboot
三、鏈接成功,會列出遠程docker容器和鏡像app
2、新建項目
項目結構圖
一、配置pom文件
<?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>
二、在src/main目錄下建立docker目錄,並建立Dockerfile文件
FROM openjdk:8-jdk-alpine ADD *.jar app.jar ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
三、在resource目錄下建立application.properties文件
logging.config=classpath:logback.xml logging.path=/home/developer/app/logs/ server.port=8990
四、建立DockerApplication文件
@SpringBootApplication public class DockerApplication { public static void main(String[] args) { SpringApplication.run(DockerApplication.class, args); } }
五、建立DockerController文件
@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/ 目錄下,將宿主機目錄掛載到容器內部目錄後,那麼日誌就會持久化容器外部的宿主機目錄中。
七、Maven打包
八、運行
先pull基礎鏡像,而後再打包鏡像,並將鏡像部署到遠程docker運行
這裏咱們能夠看到鏡像名稱爲docker-demo:1.1,docker容器爲docker-server
九、運行成功
十、瀏覽器訪問
十一、日誌查看
自此,經過IDEA 部署springboot項目到docker成功!不可思議,部署一個Javaweb項目居然如此簡單方便!
【推薦閱讀】
[技術]:面試官問你MySQL的優化,看這篇文章就夠了