jonashackt/spring-boot-vuejs

Create docker image

mgh87 opened this issue · 5 comments

mgh87 commented

Hey jonas,

Would you be interested in a pullrequest that enhanced your build with the possibility to build a docker image to deploy your application and showcase this deployment strategy?

Kind regards

Hi @mgh87 , you mean the Heroku deployment process? So using a Docker image and the Heroku Docker Registry to deploy the app, inkl. TravisCI integration?!? Why not 😃 - would be great to see it. Cheers

mgh87 commented

Just thought at first about the approach from https://spring.io/guides/gs/spring-boot-docker/
for creation of the docker image.

But I can also have a look at Heroku+TravisCi.
Its also on my list of technologies to look up.

I have setup this. And this was actually pretty easy.

  1. The backend can be packaged with the dockerfile-maven-plugin (created by Spotify!)
  2. The frontend, when built to production is just an HTML and a JS file. You can simply copy this to any webserver, or include it into the backend and let it be served by Springs bundled Jetty.

Hey @mgh87 - didn't have the time until now, but I provided the project with a full Dockerfile to build & run the complete app. If you need more information, have a look into the docs at: https://github.com/jonashackt/spring-boot-vuejs#build-and-run-with-docker

I didn't want to use the really great dockerfile-maven-plugin as @Doogiemuc mentioned - all because I wanted a more in-depth approach, where there's no so much magic and everyone can have a look into what's going on. The Dockerfile therefore uses Docker multi-stage build, where the second stage is lent from @mgh87 's mentioned guide https://spring.io/guides/gs/spring-boot-docker/:

# Docker multi-stage build

# 1. Building the App with Maven
FROM maven:3-jdk-11

ADD . /springbootvuejs
WORKDIR /springbootvuejs

# Just echo so we can see, if everything is there :)
RUN ls -l

# Run Maven build
RUN mvn clean install


# Just using the build artifact and then removing the build-container
FROM openjdk:11-jdk

MAINTAINER Jonas Hecht

VOLUME /tmp

# Add Spring Boot app.jar to Container
COPY --from=0 "/springbootvuejs/backend/target/backend-0.0.1-SNAPSHOT.jar" app.jar

ENV JAVA_OPTS=""

# Fire up our Spring Boot app by default
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]

With that, there are no build artifacts left in the resulting Docker image. Additionally it is also crucial to use a .dockerignore file - otherwise underlying OS specific node modules will be copied into the Docker build container, which could lead to problems (like as with my Mac ✌️ ):

# exclude underlying OS specific node modules
frontend/node*

Just build your own Docker image with docker build . --tag spring-boot-vuejs:latest and run your Docker container with docker run -d -p 8088:8088 --name myspringvuejs spring-boot-vuejs. Then access your app at http://localhost:8088. Have fun!

mgh87 commented

That's really nice! Didn't find the time to do it. 😿
Thanks for the work.