<p>Hello Fellow Developers,</p>
<p>I am new to software development and looking forward to doing transitioning to software development.</p>
<p>I am learning new skills and tools to scale up and I came across DOCKER and KUBERNETES</p>
<p>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.</p>
<p>Things to image</p>
<p>Environment: JAVA 17</p>
<p>Dependencies: POM.xml</p>
<pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;parent&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
        &lt;version&gt;2.7.5&lt;/version&gt;
        &lt;relativePath /&gt; &lt;!-- lookup parent from repository --&gt;
    &lt;/parent&gt;
    &lt;groupId&gt;com.greatlearning.employeemanagementservice&lt;/groupId&gt;
    &lt;artifactId&gt;employeemanagementservice&lt;/artifactId&gt;
    &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
    &lt;name&gt;employeemanagementservice&lt;/name&gt;
    &lt;description&gt;Demo project for Spring Boot&lt;/description&gt;
    &lt;properties&gt;
        &lt;java.version&gt;17&lt;/java.version&gt;
    &lt;/properties&gt;
    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
            &lt;scope&gt;runtime&lt;/scope&gt;
            &lt;optional&gt;true&lt;/optional&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
            &lt;artifactId&gt;lombok&lt;/artifactId&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;mysql&lt;/groupId&gt;
            &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
            &lt;version&gt;8.0.19&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;!-- spring security --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-security&lt;/artifactId&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
            &lt;scope&gt;test&lt;/scope&gt;
            &lt;exclusions&gt;
                &lt;exclusion&gt;
                    &lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
                    &lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
                &lt;/exclusion&gt;
            &lt;/exclusions&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;


    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
                &lt;configuration&gt;
                    &lt;excludes&gt;
                        &lt;exclude&gt;
                            &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
                            &lt;artifactId&gt;lombok&lt;/artifactId&gt;
                        &lt;/exclude&gt;
                    &lt;/excludes&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;

&lt;/project&gt;
</code></pre>
<p>Application-properties:</p>
<pre><code>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
</code></pre>
<p>This is an employee management project with simple CRUD operation I have done. It would be helpful if someone can guide me through Docker image settings.</p>
<p>Pardon me, If it is inappropriate to post such questions in a stack overflow.</p>

<p>Try the follwoing 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. For more infomation about multi-stage builds, consult the <a href="https://docs.docker.com/build/building/multi-stage/" rel="nofollow noreferrer">official documentation</a> or one of the countless internet tutorials.</p>
<pre><code>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
</code></pre>
<p>I made the following assumptions:</p>
<ol>
<li>Your build artifact is named <code>employeemanagementservice.jar</code>. Check your <code>target</code> directory after your maven build to verify. I always configure it like this in the pom.xml</li>
</ol>
<pre class="lang-xml prettyprint-override"><code> &lt;build&gt;
        &lt;finalName&gt;${project.artifactId}&lt;/finalName&gt;
 &lt;/build&gt;
</code></pre>
<ol start="2">
<li><p>You run your tests in a CI/CD pipeline and don't want to run them in the Docker image build step. If you wanted to run your tests you'd have to remove the <code>-DskipTests=true</code> from the <code>RUN mvn package -DskipTests=true</code> comannd.</p>
</li>
<li><p>Your main class is called <code>Application</code> and it resides in the <code>com.greatlearning.employeemanagementservice</code> package. If not, change it in the Docker <code>CMD</code> command.</p>
</li>
<li><p>You want your service to use at most 1GB of RAM. If not, change the <code>-Xmx1024m</code> to your desired amount. The other <code>java</code> arguments are for optimization purposes, you can look them up online.</p>
</li>
</ol>
