Spring Boot 创建Docker镜像



  • Spring Boot 创建Docker镜像

    Docker是一种容器管理服务,可简化构建和部署。如果你没有学习过Docker,您可以到我们的Docker教程去学习,在本章中,我们将看到如何为Spring Boot应用程序使用Maven和Gradle依赖项来创建Docker映像。
  • 创建Dockerfile

    首先,在目录src/main/docker下创建一个名为Dockerfile的文件,其内容如下所示。请注意,此文件对于创建Docker映像很重要。
    
    FROM java:8
    VOLUME /tmp
    ADD demo-0.0.1-SNAPSHOT.jar app.jar
    RUN bash -c 'touch /app.jar'
    ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
    
    
  • Maven

    对于Maven,将Docker Maven插件添加到您的构建配置文件pom.xml中
    
    <properties>
       <docker.image.prefix>spring-boot-jc2182</docker.image.prefix>
    </properties>
    
    <build>
       <plugins>
          <plugin>
             <groupId>com.spotify</groupId>
             <artifactId>docker-maven-plugin</artifactId>
             <version>1.2.2</version>
             
             <configuration>
                <imageName>${docker.image.prefix}/${project.artifactId}</imageName>
                <dockerDirectory>src/main/docker</dockerDirectory>
                <resources>
                   <resource>
                      <directory>${project.build.directory}</directory>
                      <include>${project.build.finalName}.jar</include>
                   </resource>
                </resources>
             </configuration>
          </plugin>
          
          <plugin>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-maven-plugin</artifactId>
          </plugin>
       </plugins>
       
    </build>
    
    
    完整的pom.xml文件在下面给出-
    
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.3.0.M4</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.jc2182</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>demo</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
            <docker.image.prefix>spring-boot-jc2182</docker.image.prefix>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
            
            <plugin>
             <groupId>com.spotify</groupId>
             <artifactId>docker-maven-plugin</artifactId>
             <version>1.2.2</version>
             
             <configuration>
                <imageName>${docker.image.prefix}/${project.artifactId}</imageName>
                <dockerDirectory>src/main/docker</dockerDirectory>
                <resources>
                   <resource>
                      <directory>${project.build.directory}</directory>
                      <include>${project.build.finalName}.jar</include>
                   </resource>
                </resources>
             </configuration>
          </plugin>
    
        </build>
    
        <repositories>
            <repository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/milestone</url>
            </repository>
        </repositories>
        <pluginRepositories>
            <pluginRepository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/milestone</url>
            </pluginRepository>
        </pluginRepositories>
    
    </project>
    
    
    现在,您可以使用Maven命令mvn package docker:build来运行您的应用程序
    构建成功后,您可以在控制台上看到输出,如下所示:
    spring docker
    现在,使用docker images通过命令查看Docker映像,并在控制台上查看镜像信息。
    spring docker
  • Gradle

    完整的build.gradle文件在下面给出。
    
    plugins {
        id 'org.springframework.boot' version '2.3.0.M4'
        id 'io.spring.dependency-management' version '1.0.9.RELEASE'
        id 'java'
        id 'com.palantir.docker' version '0.25.0'
    }
    
    group = 'com.jc2182'
    version = '0.0.1-SNAPSHOT'
    sourceCompatibility = '1.8'
    
    repositories {
        mavenCentral()
        maven { url 'https://repo.spring.io/milestone' }
    }
    
    dependencies {
        
        implementation 'org.springframework.boot:spring-boot-starter-web'
        testImplementation('org.springframework.boot:spring-boot-starter-test') {
            exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
        }
    }
    
    test {
        useJUnitPlatform()
    }
    
    docker {
        dockerfile file('src/main/docker/Dockerfile')  
        name "${project.group}/${jar.baseName}"
        files jar.archivePath
        buildArgs(['JAR_FILE': "${jar.archiveName}"])
    }
    
    
    现在,使用下面显示的命令创建一个Docker镜像:
    请使用命令gradle build docker来创建对象
    执行命令后,您可以在控制台窗口中看到BUILD SUCCESSFUL日志。
    spring docker
    现在,可以看到刚刚创建的docker 镜像。
    spring docker
    使用docker运行刚刚创建的镜像
    spring docker