手頭有個任務,須要用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