简体   繁体   English

如何为 Spring Boot 应用程序创建 Dockerfile?

[英]How to create Dockerfile for Spring Boot app?

Hello Fellow Developers,各位开发者好,

I am new to software development and looking forward to doing transitioning to software development.我是软件开发的新手,期待着过渡到软件开发。

I am learning new skills and tools to scale up and I came across DOCKER and KUBERNETES我正在学习新的技能和工具来扩大规模,我遇到了 DOCKER 和 KUBERNETES

I have completed my backend with spring boot, java and MYSQL I just wanted to know how to dockerize an image for the spring boot application in an industrial real-time manner just to get a hang of it.我已经用 spring boot、java 和 MYSQL 完成了我的后端我只是想知道如何以工业实时方式为 spring boot 应用程序 dockerize 图像只是为了掌握它。

Things to image要形象的东西

Environment: JAVA 17环境:Java 17

Dependencies: POM.xml依赖项: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.7.5</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.greatlearning.employeemanagementservice</groupId>
    <artifactId>employeemanagementservice</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>employeemanagementservice</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.19</version>
        </dependency>
        <!-- spring security -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</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>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Application-properties:应用程序属性:

server.port=8082
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/empdb
spring.datasource.username=root
spring.datasource.password=*s12
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.main.allow-bean-definition-overriding=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect

This is an employee management project with simple CRUD operation I have done.这是我做过的一个简单CRUD操作的员工管理项目。 It would be helpful if someone can guide me through Docker image settings.如果有人可以指导我完成 Docker 映像设置,那将很有帮助。

Pardon me, If it is inappropriate to post such questions in a stack overflow.请原谅,如果在堆栈溢出中发布此类问题是不合适的。

Try the follwoing Dockerfile.试试下面的 Dockerfile。 It performs a multi-stage build where in the first stage, your code is compiled to an executable.它执行多阶段构建,在第一阶段,您的代码被编译为可执行文件。 Then in the next stage, only the parts necessary for running your application are copied so that your Docker image has less size than if you copied all your uncompiled and compiled code in the Docker image.然后在下一阶段,只复制运行应用程序所必需的部分,这样您的 Docker 镜像就比复制 Docker 镜像中所有未编译和已编译代码的体积更小。 For more infomation about multi-stage builds, consult the official documentation or one of the countless internet tutorials.有关多阶段构建的更多信息,请参阅官方文档或无数 Internet 教程之一。

FROM maven:3.8-openjdk-17 as maven
WORKDIR /app
COPY ./pom.xml .
RUN mvn dependency:go-offline
COPY ./src ./src
RUN mvn package -DskipTests=true
WORKDIR /app/target/dependency
RUN jar -xf ../employeemanagementservice.jar

FROM ibm-semeru-runtimes:open-17-jre-centos7
ARG DEPENDENCY=/app/target/dependency
COPY --from=maven ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY --from=maven ${DEPENDENCY}/META-INF /app/META-INF
COPY --from=maven ${DEPENDENCY}/BOOT-INF/classes /app
CMD java -server -Xmx1024m -Xss1024k -XX:MaxMetaspaceSize=135m -XX:CompressedClassSpaceSize=28m -XX:ReservedCodeCacheSize=13m -XX:+IdleTuningGcOnIdle -Xtune:virtualized -cp app:app/lib/* com.greatlearning.employeemanagementservice.Application

I made the following assumptions:我做了以下假设:

  1. Your build artifact is named employeemanagementservice.jar .您的构建工件被命名为employeemanagementservice.jar Check your target directory after your maven build to verify.在您的 Maven 构建之后检查您的target目录以进行验证。 I always configure it like this in the pom.xml我总是在 pom.xml 中这样配置
 <build>
        <finalName>${project.artifactId}</finalName>
 </build>
  1. You run your tests in a CI/CD pipeline and don't want to run them in the Docker image build step.您在 CI/CD 管道中运行测试,但不想在 Docker 映像构建步骤中运行它们。 If you wanted to run your tests you'd have to remove the -DskipTests=true from the RUN mvn package -DskipTests=true comannd.如果您想运行测试,则必须从RUN mvn package -DskipTests=true命令中删除-DskipTests=true =true。

  2. Your main class is called Application and it resides in the com.greatlearning.employeemanagementservice package.您的主类称为Application ,它位于com.greatlearning.employeemanagementservice包中。 If not, change it in the Docker CMD command.如果不是,请在 Docker CMD命令中更改它。

  3. You want your service to use at most 1GB of RAM.您希望您的服务最多使用 1GB 的 RAM。 If not, change the -Xmx1024m to your desired amount.如果不是,请将-Xmx1024m更改为您想要的数量。 The other java arguments are for optimization purposes, you can look them up online.其他java参数是为了优化目的,你可以上网查一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM