Last active
August 23, 2021 17:31
-
-
Save gofabian/8a0f951bc1edc88b918ce1145ccfbb03 to your computer and use it in GitHub Desktop.
Dockerize Spring Boot <80MB => https://dev.to/gofabian/dockerize-spring-boot-80mb-58m9
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# (1) use Alpine Linux for build stage | |
FROM alpine:3.10.1 as build | |
# (2) install build dependencies | |
RUN apk --no-cache add openjdk11 | |
RUN apk --no-cache add maven | |
# build JDK with less modules | |
RUN /usr/lib/jvm/default-jvm/bin/jlink \ | |
--compress=2 \ | |
--module-path /usr/lib/jvm/default-jvm/jmods \ | |
--add-modules java.base,java.logging,java.xml,jdk.unsupported,java.sql,java.naming,java.desktop,java.management,java.security.jgss,java.instrument \ | |
--output /jdk-minimal | |
# fetch maven dependencies | |
WORKDIR /build | |
COPY pom.xml pom.xml | |
RUN mvn dependency:go-offline | |
# build | |
COPY src src | |
RUN mvn clean package | |
# prepare a fresh Alpine Linux with JDK | |
FROM alpine:3.10.1 | |
# get result from build stage | |
COPY --from=build /build/target/*.jar /app.jar | |
COPY --from=build /jdk-minimal /opt/jdk/ | |
VOLUME /tmp | |
EXPOSE 8080 | |
CMD /opt/jdk/bin/java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>lazy</groupId> | |
<artifactId>lazy</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>2.1.6.RELEASE</version> | |
</parent> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-web</artifactId> | |
</dependency> | |
</dependencies> | |
<properties> | |
<java.version>11</java.version> | |
</properties> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lazy; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.web.bind.annotation.GetMapping; | |
import org.springframework.web.bind.annotation.RestController; | |
@SpringBootApplication | |
@RestController | |
public class SpringBootApp { | |
@GetMapping("/hello") | |
public String hello() { | |
return "Hello World!"; | |
} | |
public static void main(String[] args) { | |
SpringApplication.run(SpringBootApp.class, args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment