Fork me on GitHub

idea远程部署SpringBoot项目到Docker

安装docker服务或者系统学习docker参考这篇文档:https://shimo.im/docs/fE0eJCx8IIojQXzB/

1.配置docker的远程端口

vim /usr/lib/systemd/system/docker.service

找到ExecStart,在最后追加

-H tcp://0.0.0.0:2375

2.重启docker

 systemctl daemon-reload
 systemctl stop docker
 systemctl start docker

3.如果开启了防火墙需开发端口,我这里直接将防火墙关闭,所以不用执行此命令

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

4.idea安装docker插件

5.连接远程docker

连接成功够会展示docker中容器和镜像

 

 6.之后在需要部署的项目pom文件中添加以下配置:

 1  <build>
 2         <plugins>
 3             <plugin>
 4                 <groupId>org.springframework.boot</groupId>
 5                 <artifactId>spring-boot-maven-plugin</artifactId>
 6             </plugin>
 7             <plugin>
 8                 <groupId>com.spotify</groupId>
 9                 <artifactId>docker-maven-plugin</artifactId>
10                 <version>1.0.0</version>
11                 <configuration>
12                     <dockerDirectory>src/main/docker</dockerDirectory>
13                     <resources>
14                         <resource>
15                             <targetPath>/</targetPath>
16                             <directory>${project.build.directory}</directory>
17                             <include>${project.build.finalName}.jar</include>
18                         </resource>
19                     </resources>
20                 </configuration>
21             </plugin>
22             <plugin>
23                 <artifactId>maven-antrun-plugin</artifactId>
24                 <executions>
25                     <execution>
26                         <phase>package</phase>
27                         <configuration>
28                             <tasks>
29                                 <copy todir="src/main/docker" file="target/${project.artifactId}-${project.version}.${project.packaging}"></copy>
30                             </tasks>
31                         </configuration>
32                         <goals>
33                             <goal>run</goal>
34                         </goals>
35                     </execution>
36                 </executions>
37             </plugin>
38 
39         </plugins>
40     </build>

 

 7.在src/main目录下创建docker目录,并创建Dockerfile文件,文件内容:

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

8.idea配置docker

 

 

 

Image tag : 指定镜像名称和tag,镜像名称为 docker-demo,tag为1.1
Bind ports : 绑定宿主机端口到容器内部端口。格式为[宿主机端口]:[容器内部端口]

9.添加maven打包命令:第一步清除上次编译或者打包的文件,第二步是对项目进行编译以及打成jar

 

clean package -Dmaven.test.skip=true

 10.启动完成后

 

 

访问:

 

posted @ 2019-07-30 14:03  L波涛  阅读(1180)  评论(0编辑  收藏  举报