2

I have a problem with my Spring Boot project running on docker container. Scheduled task doesn't work if I run container as demonized ( docker run -d). When I run image non in background everything is working. Unfortunately, I have to run it as demonized and i have no idea how to resolve that problem. Thank you for any solution :)

My Scheduled annotation: @Scheduled(fixedDelay = 1440000)

This is my Dockerfile:

FROM java:openjdk-8
ENV SPRING_PROFILES_ACTIVE dev,docker
WORKDIR /app
EXPOSE 9000 9000
RUN apt-get update && apt-get -y install cron
RUN service cron start
COPY build/libs/app.jar /app/app.jar
CMD ["/bin/sh", "-c", "java -jar /app/app.jar --spring.profiles.active=$SPRING_PROFILES_ACTIVE"]
Paolo Juve
  • 31
  • 2
  • 3
  • Can you amend your question with the full commands used to start your container in both scenarios? In your current `Dockerfile` example, the cron service is not actually running. The annotation would run scheduled inside the JVM, not in cron. In this case, cron shouldn't actually be needed (I'd try removing it to confirm it isn't actually used in any way). – Andy Shinn Dec 21 '17 at 03:28
  • Can you please share how did you resolve this issue? – Thej Feb 26 '21 at 03:43

1 Answers1

0

When you run the container as a demon you will not see the output directly in the console. You can use docker logs to check what is going on. In fact it works independently from the -d parameter.

Try to wrap the minimalistic scheduled example from the spring documentation into an image and run it with -d.

Identify the running container id by running

docker ps 

And then collect the logs from using

docker logs your-container-id

You'll see that the scheduled tasks works as expected.

Sasha Shpota
  • 9,436
  • 14
  • 75
  • 148