手头有个任务,须要用java经过jni调用一个开源算法库gmssl的功能,可是gmssl只提供了源码,须要编译后才能使用。按照一般的作法,咱们会部署好centos的虚拟机和开发环境,安装好gmssl的依赖环境,而后再基于这个部署好的开发环境进行开发和调试。java
这样的作法,会在开发和部署过程当中会出现一些问题:git
为了解决上面提到的问题,经过引入docker,并支持快速调试。主要思路以下:github
#build docker build -t 10.10.8.166:5000/gmssl . FROM 10.10.8.166:5000/centos-java:latest RUN yum -y update RUN yum install -y unzip RUN yum install -y gcc RUN yum install -y openssl-devel RUN yum install -y perl RUN wget https://codeload.github.com/guanzhi/GmSSL/zip/2.0 RUN unzip 2.0 RUN rm -rf 2.0 WORKDIR "/GmSSL-2.0" RUN ./config RUN make && make install ADD maven/test-1.0-SNAPSHOT.jar /opt/test.jar ADD maven/startup.sh /opt/startup.sh RUN chmod +x /opt/startup.sh WORKDIR "/opt" EXPOSE "5005" ENTRYPOINT ["/opt/startup.sh"]
使用docker-maven-plugin,对docker文件进行打包、发布处理算法
<plugin> <groupId>io.fabric8</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.15.16</version> <executions> <execution> <id>package</id> <phase>package</phase> <goals> <goal>build</goal> </goals> </execution> <execution> <id>deploy</id> <phase>deploy</phase> <goals> <goal>build</goal> <goal>push</goal> </goals> </execution> </executions> <configuration> <verbose>true</verbose> <machine> <name>default</name> <autoCreate>true</autoCreate> <createOptions> <driver>virtualbox</driver> </createOptions> </machine> <images> <image> <!-- Artifact Image--> <name>10.10.8.166:5000/gmssl:${project.version}</name> <run> <ports> <port>5005:5005</port> </ports> </run> <build> <dockerFileDir>${project.basedir}/docker</dockerFileDir> <assembly> <mode>dir</mode> <inline> <id>gmssl-it</id> <fileSets> <fileSet> <includes> <include>*.jar</include> </includes> <directory>${project.build.directory}</directory> <outputDirectory>/</outputDirectory> </fileSet> <fileSet> <includes> <include>*.sh</include> </includes> <directory>${project.build.directory}/classes</directory> <outputDirectory>/</outputDirectory> </fileSet> </fileSets> </inline> </assembly> </build> </image> </images> </configuration> </plugin>
为了保证镜像保持最新,须要将before launch设置为:docker
执行mvn docker:stop的目的是为了可以在启动前,将以前运行的镜像删除。centos
在startup脚本中,加入远程调试的脚本bash
#!/bin/bash java -agentlib:jdwp=transport=dt_socket,address=5005,suspend=y,server=y -cp /opt/*:/opt Main