原创

打包docker镜像,并设置jvm启动参数

pom.xml 增加插件

<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>${spotify}</version>
    <configuration>
        <imageName>${docker-registry}/${project.artifactId}:${revision}.${images-version}</imageName>
        <baseImage>java</baseImage>
        <env>
            <JVM_OPTIONS>"-Xms128m -Xmx256m"</JVM_OPTIONS>
        </env>
        <entryPoint>["sh", "-c", "java $JVM_OPTIONS -jar /${project.build.finalName}.jar"]</entryPoint>
        <resources>
            <resource>
                <targetPath>/</targetPath>
                <directory>${project.build.directory}</directory>
                <include>${project.build.finalName}.jar</include>
            </resource>
        </resources>
        <serverId>my-docker-registry</serverId>
    </configuration>
</plugin>
执行mvn install,然后在执行docker build
[INFO] -------------------------< cn.lili:buyer-api >--------------------------
[INFO] Building buyer-api 4.3
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- docker-maven-plugin:1.2.2:build (default-cli) @ buyer-api ---
[INFO] Using authentication suppliers: [ConfigFileRegistryAuthSupplier, FixedRegistryAuthSupplier]
[INFO] Copying /Users/liuhaihua/IdeaProjects/lilishop/buyer-api/target/buyer-api-4.3.jar -> /Users/liuhaihua/IdeaProjects/lilishop/buyer-api/target/docker/buyer-api-4.3.jar
[INFO] Building image registry.cn-qingdao.aliyuncs.com/shanbei/buyer-api:4.3.1
Step 1/4 : FROM java

 ---> d23bdf5b1b1b
Step 2/4 : ENV JVM_OPTIONS "-Xms128m -Xmx256m"

 ---> Using cache
 ---> ca05c6783b17
Step 3/4 : ADD /buyer-api-4.3.jar //

 ---> 3f30ab905fca
Step 4/4 : ENTRYPOINT ["sh", "-c", "java $JVM_OPTIONS -jar /buyer-api-4.3.jar"]

 ---> Running in 002ef3fec70d
Removing intermediate container 002ef3fec70d
 ---> 25bfe89a8384
ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
Successfully built 25bfe89a8384
Successfully tagged registry.cn-qingdao.aliyuncs.com/shanbei/buyer-api:4.3.1
[INFO] Built registry.cn-qingdao.aliyuncs.com/shanbei/buyer-api:4.3.1
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 32.612 s
[INFO] Finished at: 2023-08-31T21:48:05+08:00
[INFO] ------------------------------------------------------------------------

Process finished with exit code 0
查看生成的dockfile文件
FROM java
ENV JVM_OPTIONS "-Xms128m -Xmx256m"
ADD /buyer-api-4.3.jar //
ENTRYPOINT ["sh", "-c", "java $JVM_OPTIONS -jar /buyer-api-4.3.jar"]
运行docker,设置启动参数 11  
ps -ef|grep java
  22

Entrypoint指令

用于设定容器启动时第一个运行的命令及其参数。 语法:
ENTRYPOINT ["executable", "param1", "param2"]
使用这种语法,Docker不会使用shell来运行。这意味着通常的shell处理过程不会发生。 如果你需要在shell环境中运行,那么你可以这样做:
ENTRYPOINT [ "sh", "-c", "echo $HOME" ]
正文到此结束
Loading...