9

I have a spring-boot project and I want automatically redeploy my jar in the container. How to do it correctly? So far, all I see is this way. It's the right way?

# cd /home/jdev; 
# sudo docker stop ca_spring_boot;
# sudo docker rm ca_spring_boot;  
# sudo docker rmi ca_app_image; 
# sudo docker build -t ca_app_image .;
# sudo docker run -d -p 8888:8080 --name ca_spring_boot ca_app_image

And my Dockerfile

FROM java:8
VOLUME /tmp
EXPOSE 8080
ADD docker-storage/jenkins/workspace/CA/build/libs/ca-1.0.jar app.jar
RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-Dspring.profiles.active=container","-jar","/app.jar"]

Thanks.

JDev
  • 2,157
  • 3
  • 31
  • 57

2 Answers2

9

You could mount a volume and put your app.jar in there. So you do not need to rebuild the image, you just restart the container.

Dockerfile

FROM java:8
ENTRYPOINT [ "sh", "-c", "java -jar /mnt/app.jar" ]

Put your app.jar in /docker/spring/

Build and run:

docker build -t spring_test .
docker run -d -v /docker/spring/:/mnt -p 12384:8080 --name spring_test_running spring_test

If you update your spring application you just do:

docker restart spring_test_running
Sha7x
  • 478
  • 3
  • 11
  • Good solution, I will definitely try. – JDev Feb 02 '17 at 10:27
  • Your solution works. Thank you. And could you please tell me, what it is for option? "-Djava.security.egd=file:/dev/./urandom" I need it? If so, for what? It also works without it. – JDev Feb 04 '17 at 09:51
  • 1
    This option does not affect the spring application. The default random function java uses is /dev/random, which gets changed to /dev/urandom with the option "-Djava.security.egd=file:/dev/./urandom". – Sha7x Feb 05 '17 at 10:23
  • Thanks for the answer )) – JDev Feb 05 '17 at 14:03
  • then the build process should be taken place in the host system, isn't it better to do the build process on the docker container too? for example using gradle or maven. then the only things that developers should install on their computer will be Java and an IDE. – Ali Abazari Nov 28 '17 at 04:28
  • @JDev, hi. I know it was a long time ago, but maybe you have the file example where you mounted the volume like in the answer above? I'm new with docker and trying to do the same as you asked: automatically redeploy spring boot app in docker. – Den B Sep 19 '19 at 07:30
  • Hi, mount volume. Copy JAR in mounted volume. docker restart. – JDev Oct 07 '19 at 14:18
2

The previous answer is good. But there is need to restart container every time when you want to test your code. But we can avoid this problem. Just use Spring dev tool And mount destination directory as described above.

Bukharov Sergey
  • 9,767
  • 5
  • 39
  • 54
  • 2
    If you are running the spring application with "java -jar", spring dev tools will be disabled. http://stackoverflow.com/a/37702560/3861751 – Sha7x Feb 02 '17 at 10:36
  • Yes, I use dev-tools. But little do not understand how to do it .. I have Jenkins, it creates jar, next steps? What I need to do? – JDev Feb 02 '17 at 10:37
  • It is unclear from origin question. Fellow tries to set up CI/CD with docker. It is closer to some dev or pre-production (demo) environment. In this case it is often run on dedicated machine. But spring's dev-tools is more intended to be used in *local* machine development environment – Timur Milovanov Jan 09 '19 at 13:38