namrithakumar / mealoptimizer

An app that takes in a list of user meals and provides a menu - developed using Springboot + Hibernate
0 stars 0 forks source link

Setup docker for SpringBoot #74

Open namrithakumar opened 3 years ago

namrithakumar commented 3 years ago

Step 3.3: Create image on your own for mealoptimizer-rest-api (Manually create image) 1) Create jar for the application 2) Test if jar is running: cd to the folder in cmd and run using java -jar --spring.profiles.active=test and test url 3) Run the below commands: docker run -dit openjdk:8-jdk-alpine (container created: 25940965e5ea)

Go to the folder containing target/jar in cmd and copy the jar to /tmp folder on the running container docker container cp target/mealoptimizer-0.0.1-SNAPSHOT.jar 25940965e5ea:/tmp

Check if the jar has been copied properly - check the /tmp folder inside the container docker container exec cool_tharp ls /tmp (cool_tharp is the name of the container got using command docker container ls -a)

Create an image out of the container: docker container commit cool_tharp meal-optimizer/meal-optimizer-rest-api:v1 (Here meal-optimizer/meal-optimizer-rest-api:v1 is the image name and tag or version)

When the image is launched up, run the java -jar command on top of the image: docker container commit --change='CMD ["java","-jar","/tmp/mealoptimizer-0.0.1-SNAPSHOT.jar"]' cool_tharp meal-optimizer/meal-optimizer-rest-api:v2 NOTE: The profile is passed as an environment variable - refer next step

docker run -d -p 5000:5000 -e \"SPRING_PROFILES_ACTIVE=test\" meal-optimizer/meal-optimizer-rest-api:v2 As per https://devops.datenkollektiv.de/using-spring-profiles-with-docker.html, setting the SPRING_PROFILES_ACTIVE variable when starting the container will swicth the active profile of the Spring application.

(OR)

docker run -d -p 5000:5000 -e spring.profiles.active="test" meal-optimizer/meal-optimizer-rest-api:v2 as per http://tomaszdziurko.com/2016/01/dockerizing-spring-boot-application/


ALTERNATIVE TO STEP 3.3: Automate creating Docker images 1) Search Google for dockerfile-maven-plugin (MATURE not INACTIVE) 2) Copy plugin info from https://github.com/spotify/dockerfile-maven -> Ties up creation docker image with maven lifecycle. User mvn package to create the package Under goals, retain only 'build', comment out 'push' for now 3) Create dockerHub ID and set it up under configuration -> repository 4) Add a file called Dockerfile (no extension) and add sample Dockerfile 5) Each step in the Dockerfile is executed as a step. So modify entrypoint to include --spring.profiles.active ENTRYPOINT ["/usr/bin/java", "-Dspring.profiles.active=dev", "-jar", "/usr/share/myservice/myservice.jar"]

PUSH THE IMAGE TO dockerhub - hub.docker.com docker login => configure your user id and password docker push meal-optimizer/meal-optimizer-rest-api where meal-optimizer/meal-optimizer-rest-api is the image name

namrithakumar commented 3 years ago

issue 74 is parked for now. It is the recommended approach. Issue 75 is used as a stop gap solution to deploy to test env. The complete solution is derived by replacing step 5 in issue 74 with step 4 ... in issue 75. When the application is hosted on test env, the recommended approach will be followed.