简体   繁体   中英

Spring Boot Scheduled task not working on docker container

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"]

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM