SpringBoot集成docker镜像生成

一、添加maven打包插件

<properties>
        <!--  依赖版本  -->
        <spring-boot.version>2.3.3.RELEASE</spring-boot.version>
        <!-- 应用所在的启动类 -->
        <mainClass>com.business.BusinessApplication</mainClass>
        <!-- 资源打包插件版本 -->
        <maven-resources-plugin.version>3.1.0</maven-resources-plugin.version>
</properties>
<build>
        <plugins>
            ...
            <!-- springBoot 指定主类打包 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>${mainClass}</mainClass>
                    <executable>true</executable>
                    <fork>true</fork>
                    <!--
                    Enable the line below to have remote debugging of your application on port 5005
                    <jvmArguments>-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005</jvmArguments>
                    -->
                </configuration>
            </plugin>
           ...
        </plugins>
    </build>

二、添加docker镜像生成时相关文件

1、编写Dockerfile相关文件

# 这里为基础镜像,就是包含了jdk相关的docker镜像
FROM apline_openjdk8_fonts:latest
# 基本环境参数
ENV SPRING_OUTPUT_ANSI_ENABLED=ALWAYS \
    APP_SLEEP=0 \
    JAVA_OPTS="-Dfile.encoding=utf-8"

# Add a newuser user to run our application so that it doesn't need to run as root
RUN adduser -D -s /bin/sh newuser 
WORKDIR /home/newuser 

# 授权应用相关启动时的参数环境使用
ADD entrypoint.sh entrypoint.sh
RUN chmod 755 entrypoint.sh && chown newuser:newuser entrypoint.sh
USER newuser 

# 将jar包添加到docker镜像中并且改名为app.jar
ADD *.jar app.jar

# docker启动时 需要执行的脚本
ENTRYPOINT ["./entrypoint.sh"]

EXPOSE 12001 5701/udp

2、编写entrypoint.sh脚本

#!/bin/sh

echo "The application will start in ${APP_SLEEP}s..." && sleep ${APP_SLEEP}
exec java ${JAVA_OPTS} -Djava.security.egd=file:/dev/./urandom -jar "${HOME}/app.jar" "$@"

3、在pom中指定需要maven构建时的相关资源

<build>
        <plugins>
           ...
            <!-- 资源打包插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>${maven-resources-plugin.version}</version>
                <executions>
                    <execution>
                        <id>docker-resources</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <!-- 输出目录 -->
                            <outputDirectory>target/</outputDirectory>
                            <resources>
                                <resource>
                                    <!-- 资源目录 -->
                                    <directory>src/main/docker/</directory>
                                    <filtering>false</filtering>
                                    <!-- 排除在外的资源 -->
                                    <excludes>
                                        <exclude>**/*.yml</exclude>
                                    </excludes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            ...

        </plugins>
    </build>

 三、docker镜像生成,并且上传镜像到镜像仓库,继续为后面自动化部署做铺垫,这里的版本号可以使用时间戳之类的用于自动化部署

        docker build -t harbor.cloud.com/test/app:test .
        docker login -u admin -p Harbor12345  harbor.cloud.com
        docker push harbor.cloud.com/test/app:test

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值