O
O
Orkhan Hasanli2021-09-28 00:49:49
Task Schedulers
Orkhan Hasanli, 2021-09-28 00:49:49

How to run a docker container using CRON?

Good afternoon.
There is a java application (parser) that will run inside the container. Created a Dockerfile, as well as docker-compose.yml
Everything works correctly, but after the parser completes, the container stops.
Is it correct to start a container with CRON? If yes, how to do it?
I tried to run the jar file by cron INSIDE the container, but as I understand it, when the jar file is completed, the container stops and, accordingly, the cron does not work further.
Thanks in advance.

Dockerfile

FROM adoptopenjdk/openjdk11:x86_64-ubuntu-jdk-11.0.12_7 as build

COPY . /srv
RUN cd /srv && ./mvnw clean package
WORKDIR /srv

ARG API_VERSION=1.0

FROM adoptopenjdk/openjdk11:x86_64-ubuntu-jdk-11.0.12_7
RUN mkdir -p /srv/uploads/

#CRON
#ADD docker-cron /etc/cron.d/docker-cron
#RUN chmod 0644 /etc/cron.d/docker-cron
#RUN touch /var/log/cron.log
#RUN apt-get update && apt-get install cron
#CMD cron && tail -f /var/log/cron.log

ENV API_VERSION=1.0
COPY --from=build /srv/target/parser-${API_VERSION}.jar /srv/parser-${API_VERSION}.jar
ENTRYPOINT exec java -jar /srv/parser-${API_VERSION}.jar


docker cron
0 0 0/2 1/1 * ? * java -jar /srv/parser-${API_VERSION}.jar >> /var/log/cron.log 2>&1
# An empty line is required at the end of this file for a valid cron file.


docker-compose
version: '3.8'

services:
  parser:
    build:
      context: ./
      dockerfile: Dockerfile
    container_name: parser
    restart: always

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Shumov, 2021-09-28
@azerphoenix

It is necessary to start cron on the host machine with the launch of the container and the transfer of commands to it, followed by a stop

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question