Skip to content

Instantly share code, notes, and snippets.

@jeffersonbezerra
Last active March 31, 2019 20:38
Show Gist options
  • Save jeffersonbezerra/0c0c82a7856cc88a18edeb386166873d to your computer and use it in GitHub Desktop.
Save jeffersonbezerra/0c0c82a7856cc88a18edeb386166873d to your computer and use it in GitHub Desktop.
Dockerfile (Multistage)
FROM adoptopenjdk/openjdk12:alpine AS builder
MAINTAINER Jefferson Bezerra <[email protected]>
ENV DEMO_APP_FOLDER=/usr/src/app
RUN mkdir -p $DEMO_APP_FOLDER
WORKDIR $DEMO_APP_FOLDER
# add pom.xml only here, and download dependency
RUN apk --no-cache add maven
ADD pom.xml $DEMO_APP_FOLDER
RUN ["mvn", "dependency:go-offline", "clean"]
ADD . $DEMO_APP_FOLDER
RUN ["mvn", "package"]
# --- release ---
FROM adoptopenjdk/openjdk12:alpine-jre AS release
MAINTAINER Jefferson Bezerra <[email protected]>
ENV DEPENDENCY=/usr/src/app/target/dependency
#to work in this way, you need to unpack your jar during the mvn package phase (see maven-dependency-plugin goal unpack)
COPY --from=builder ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY --from=builder ${DEPENDENCY}/META-INF /app/META-INF
COPY --from=builder ${DEPENDENCY}/BOOT-INF/classes /app
VOLUME /var/log/app
EXPOSE 8080
WORKDIR /app
ENTRYPOINT ["java","-cp",".:./lib/*","com.example.demo.DemoApplication"]
@jeffersonbezerra
Copy link
Author

In the pom.xml file, I configured this plugin in order to unpack the jar file and allow the separated copies of libs e classes. So the image of the libs will be cached, and only changed when the pom.xml be modified.

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-dependency-plugin</artifactId>
   <executions>
       <execution>
           <id>unpack</id>
           <phase>package</phase>
           <goals>
               <goal>unpack</goal>
           </goals>
           <configuration>
               <artifactItems>
                   <artifactItem>
                       <groupId>${project.groupId}</groupId>
                       <artifactId>${project.artifactId}</artifactId>
                       <version>${project.version}</version>
                   </artifactItem>
               </artifactItems>
           </configuration>
       </execution>
   </executions>
</plugin>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment