Skip to content

Instantly share code, notes, and snippets.

@jahentao
Forked from twang2218/Dockerfile
Last active March 6, 2019 10:18
Show Gist options
  • Save jahentao/c55779544466b70f29e79b810ba32e1d to your computer and use it in GitHub Desktop.
Save jahentao/c55779544466b70f29e79b810ba32e1d to your computer and use it in GitHub Desktop.
Docker cron example
* * * * * root /app/task.py >> /var/log/task.log 2>&1
FROM python:3.5.2
ENV TZ=Asia/Shanghai
RUN apt-get update \
&& apt-get install -y cron \
&& apt-get autoremove -y
COPY ./cronpy /etc/cron.d/cronpy
CMD ["cron", "-f"]
#!/bin/sh
build() {
docker build -t cronjob:latest $@ .
}
up() {
docker run $@ \
--name cronjob \
-d \
-v $(pwd)/task.py:/app/task.py \
cronjob:latest
}
down() {
docker rm -f -v cronjob
}
log() {
docker logs cronjob $@
}
sh() {
docker exec $@ -it cronjob bash
}
main() {
Command=$1
shift
case "${Command}" in
build) build $@ ;;
up) up $@ ;;
down) down $@ ;;
log) log $@ ;;
sh) sh $@ ;;
*) echo "Usage: $0 {build|up|down|log|sh}" ;;
esac
}
main $@
#!/usr/local/bin/python
from datetime import datetime
print("Cron job has run at {0} with environment variable ".format(str(datetime.now())))
@jahentao
Copy link
Author

jahentao commented Mar 6, 2019

remember to give task.py execution priviledge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment