docker-maven-plugin打包docker镜像

1,544 阅读1分钟

1、pom插件及其配置

  <plugins>
         
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                                 <mainClass>com.*.common.*.MainApplicatiion</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-my-jar-with-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source><!-- 源代码使用的开发版本 -->
                    <target>1.8</target><!-- 需要生成的目标class文件的编译版本 -->
                    <!-- 一般而言,target和source保持一致的,但有时候不同:为了让程序能在其他版本的jdk中运行(对于低版本目标jdk,源代码中需要不使用低版本jdk不支持的语法) -->
                </configuration>
            </plugin>

            <!--docker 镜像插件-->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.2.2</version>
                <configuration>
                    <imageName>${project.artifactId}</imageName>
                    <dockerDirectory>src/main/docker</dockerDirectory>
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
<!--                            <include>${project.build.finalName}.jar</include>-->
                            <include>demoApplication-with-dependencies.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
        </plugins>

2、Dockerfile的编写

# Docker image for springboot file run
# 基础镜像使用java
FROM java:8
# 作者
MAINTAINER jiang <jiang@163.com>
# VOLUME 指定了临时文件目录为/tmp。
# 其效果是在主机 /var/lib/docker 目录下创建了一个临时文件,并链接到容器的/tmp
VOLUME /tmp
# 将jar包添加到容器中并更名为app.jar
ADD  demoApplication-with-dependencies.jar demoApplication.jar
# 运行jar包
RUN bash -c 'touch /app.jar'
ENTRYPOINT java -Djava.security.egd=file:/dev/./urandom -jar /app.jar

3、编辑打包docker镜像

 mvn clean -DskipTests package docker:build

控制台打印日志

[INFO] --- docker-maven-plugin:1.2.2:build (default-cli) @ iap_workflow_common ---
[INFO] Using authentication suppliers: [ConfigFileRegistryAuthSupplier]
[INFO] Copying /Users/apple/Desktop/demoApplication/iap_workflow_common/target/iap_workflow_common-jar-with-dependencies.jar -> /Users/apple/Desktop/demoApplication/iap_workflow_common/target/docker/iap_workflow_common-jar-with-dependencies.jar
[INFO] Copying src/main/docker/Dockerfile -> /Users/apple/Desktop/demoApplication/iap_workflow_common/target/docker/Dockerfile
[INFO] Building image iap_workflow_common
Step 1/6 : FROM java:8

Pulling from library/java
5040bd298390: Already exists 
fce5728aad85: Already exists 
76610ec20bf5: Already exists 
60170fec2151: Already exists 
e98f73de8f0d: Already exists 
11f7af24ed9c: Already exists 
49e2d6393f32: Already exists 
bb9cdec9c7f3: Already exists 
Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
Status: Downloaded newer image for java:8
 ---> d23bdf5b1b1b
Step 2/6 : MAINTAINER jiang <jiang@163.com>

 ---> Running in edba2589a7c3
Removing intermediate container edba2589a7c3
 ---> 92371872dc85
Step 3/6 : VOLUME /tmp

 ---> Running in 330783c916b1
Removing intermediate container 330783c916b1
 ---> 83f216f376f3
Step 4/6 : ADD  demoApplication-with-dependencies.jar demoApplication.jar

 ---> fd8a844c26dd
Step 5/6 : RUN bash -c 'touch /app.jar'

 ---> Running in 20f5b5f64b0d
Removing intermediate container 20f5b5f64b0d
 ---> e83e5aec4487
Step 6/6 : ENTRYPOINT java -Djava.security.egd=file:/dev/./urandom -jar /app.jar

 ---> Running in ac7acf5f2479
Removing intermediate container ac7acf5f2479
 ---> 9ede21f0d753
ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
Successfully built 9ede21f0d753
Successfully tagged iap_workflow_common:latest
[INFO] Built demoApplication
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  58.244 s
[INFO] Finished at: 2021-05-20T14:20:38+08:00
[INFO] ------------------------------------------------------------------------
localhost:demoApplication apple$ mvn clean -DskipTests package docker:build

4、docker镜像查看

localhost:docker apple$ docker images
REPOSITORY                     TAG       IMAGE ID       CREATED         SIZE
demoApplication            latest    9ede21f0d753   8 minutes ago   737MB
localhost:docker apple$ 

其他操作,可以参考这边文章