Skip to content

Instantly share code, notes, and snippets.

@krishofmans
Forked from liemle3893/Docker-multistage-example.MD
Last active November 25, 2019 21:41
Show Gist options
  • Select an option

  • Save krishofmans/6c7b2ad75ce9250a7603da921a85598e to your computer and use it in GitHub Desktop.

Select an option

Save krishofmans/6c7b2ad75ce9250a7603da921a85598e to your computer and use it in GitHub Desktop.
Docker Multistage + Spring Boot = Smaller Container Sized

docker-multi-stage

Spring Boot + Docker Multistage = Smaller container size

Use can use prebuild version by using:

$ docker run -d -p 8080:8080 saboteurkid/smaller-spring:1.0

Wait for docker to pull and up. Then jump to step #6

1. Clone example project from Spring Boot repository

$ git clone https://github.com/spring-guides/gs-spring-boot.git
$ cd gs-spring-boot/complete

2. Create Dockerfile

wget https://gist.githubusercontent.com/krishofmans/6c7b2ad75ce9250a7603da921a85598e/raw/11094575ec75f7e5f60bbb12c1075999f36ff73a/Dockerfile

3. Build the image

$ docker build -t saboteurkid/smaller-spring:1.0 .

4. Check the images

$ docker images

You will see something like this:

saboteurkid/smaller-spring   1.0                 f880454cde3e        23 minutes ago      99.4MB

5. Create new container that run the newly created image

$ docker run -d -p 8080:8080 saboteurkid/smaller-spring:1.0

6. Test the result

$ curl localhost:8080

You will received: Greetings from Spring Boot!

FROM gradle:6.0.1-jdk8 as compile
COPY . /home/source/java
WORKDIR /home/source/java
# Default gradle user is `gradle`. We need to add permission on working directory for gradle to build.
USER root
RUN chown -R gradle /home/source/java
USER gradle
RUN gradle clean build
FROM openjdk:8-jre-alpine
WORKDIR /home/application/java
COPY --from=compile "/home/source/java/build/libs/sample-app-0.0.1-SNAPSHOT.jar" .
EXPOSE 8080
ENTRYPOINT [ "java", "-jar", "/home/application/java/sample-app-0.0.1-SNAPSHOT.jar"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment