SpringBoot(一): 使用Docker打包SpringBoot并Push到DockerHub

SpringBoot: 2.1.1
com.spotify.dockerfile-maven-plugin: 1.4.8

1. Docker打包

1.1 修改application.yml
spring:
  profiles:
    active: @spring.profiles.active@

** @spring.profiles.active@中的pring.profiles.activepom.xml->profiles->profile->properties->spring.profiles.active标签名称一致.**
**对于多环境的配置文件,SpringBoot提供了Profile功能来,因此在项目的resources目录下创建application-{profile的名称(与pom.xml->profile->id属性值保持一致)}.yml(或.properties)格式文件,并在打包或者运行时指定Profile即可.配置文件的名称,参考如下图所示: **
SpringBoot(一): 使用Docker打包SpringBoot并Push到DockerHub_第1张图片
表明只有两个profile:dev和test

1.2 Dockerfile
FROM openjdk:8-jdk-alpine3.7
VOLUME /tmp

ARG JAR_FILE
ADD target/${JAR_FILE} target/app.jar

EXPOSE 8080

RUN touch target/app.jar
ENTRYPOINT ["java","-jar","target/app.jar"]
1.3 配置pom.xml

添加如下内容:

	<properties>
		<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
		<java.version>1.8java.version>
        <dockerfile-maven-version>1.4.8dockerfile-maven-version>
        <docker.image.prefix>star936docker.image.prefix>
        <docker.repository.name>rest-api-mybatisdocker.repository.name>
	properties>

    <build>
        <finalName>${docker.image.prefix}/${docker.repository.name}finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                    
                    <fork>truefork>
                configuration>
            plugin>
            <plugin>
                <groupId>com.spotifygroupId>
                <artifactId>dockerfile-maven-pluginartifactId>
                <version>${dockerfile-maven-version}version>
                <executions>
                    <execution>
                        <id>build-imageid>
                        <phase>packagephase>
                        <goals>
                            <goal>buildgoal>
                            <goal>taggoal>
                        goals>
                        <configuration>
                        configuration>
                    execution>
                    <execution>
                        <id>push-imageid>
                        <phase>deployphase>
                        <goals>
                            <goal>pushgoal>
                        goals>
                    execution>
                executions>
                <configuration>
                    <useMavenSettingsForAuth>trueuseMavenSettingsForAuth>
                    <repository>${docker.image.prefix}/${docker.repository.name}repository>
                    <tag>${project.version}tag>
                    <buildArgs>
                        <JAR_FILE>${project.build.finalName}.jarJAR_FILE>
                    buildArgs>
                configuration>
            plugin>
        plugins>
        <resources>
            <resource>
                <directory>src/main/resourcesdirectory>
                <filtering>truefiltering>
            resource>
        resources>
    build>
    <profiles>
        
        <profile>
            <id>testid>
            <activation>
                <activeByDefault>trueactiveByDefault>
            activation>
            <properties>
                <spring.profiles.active>testspring.profiles.active>
            properties>
        profile>
        
        <profile>
            <id>devid>
            <activation>
                <activeByDefault>falseactiveByDefault>
            activation>
            <properties>
                <spring.profiles.active>devspring.profiles.active>
            properties>
        profile>
    profiles>
1.2 打包

参照下图:
SpringBoot(一): 使用Docker打包SpringBoot并Push到DockerHub_第2张图片

  • 第一步: 勾选Profile;
  • 第二步: 选中Lifecycle->clean并运行;
  • 第三步: 选中Lifecycle->package并运行.

2. Push镜像到DockerHub私有仓库

2.1 配置Maven的setting.xml

添加如下内容:

<servers>
    <server>
        <id>docker.ioid>
        <username>DockerHub用户名username>
        <password>DockerHub密码password>
        <configuration>
        <email>DockerHub注册邮箱email>
        configuration>
    server>
servers>
2.2 Push

参照上图选中Plugins->dockerfile->dockerfile:push并运行.

你可能感兴趣的:(Spring)