An example of running cron job in a docker container
ekito / docker-cron Goto Github PK
View Code? Open in Web Editor NEWAn example of running cron job in a docker container
An example of running cron job in a docker container
I disovered even if you have environement variable defined in the dockerfile or in the docker stack file , the environment variable will be available to shell if container but not to the cron. for that you would need to define env variable inside the cron file.
MYSQL_PASSWORD=secret_password
PYTHONPATH=/usr/local/app
*/45 * * * * python3 app.py >> /var/log/cron.log 2>&1
# An empty line is required at the end of this file for a valid cron file.
I cloned this repo on my Mac and it worked great. But when I tried it on my Windows 10 laptop running Docker and configured to use Linux containers, nothing ever showed up in /var/log/cron.log.
I'll continue to troubleshoot and update this issue with anything I learn.
I am doing exactly like in the example (but using debian:buster-slim
instead of ubuntu:latest
), but I run an additional command after cron
CMD ["cron"; "postgres", "-c", "hba_file=/etc/pg_hba.conf"]
The second command is started but not cron
.
Thanks for posting this, it got me started. I found an approach (using crontab - < /path/to/crontab
) that worked better for me. Thought I would post it here in case others found it useful.
modified Dockerfile
@@ -1,19 +1,16 @@
FROM ubuntu:latest
MAINTAINER [email protected]
-# Add crontab file in the cron directory
-ADD crontab /etc/cron.d/hello-cron
-
-# Give execution rights on the cron job
-RUN chmod 0644 /etc/cron.d/hello-cron
-
# Create the log file to be able to run tail
RUN touch /var/log/cron.log
#Install Cron
RUN apt-get update
RUN apt-get -y install cron
+# Install the crontab
+ADD crontab /tmp/crontab
+RUN crontab /tmp/crontab && rm /tmp/crontab
# Run the command on container startup
CMD cron && tail -f /var/log/cron.log
modified crontab
@@ -1,2 +1,2 @@
-* * * * * root echo "Hello world" >> /var/log/cron.log 2>&1
+* * * * * echo "Hello world" >> /var/log/cron.log 2>&1
# Don't remove the empty line at the end of this file. It is required to run the cron job
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.