cron
base images for various distros.
See https://blog.thesparktree.com/cron-in-docker for more information
`cron` base images for various distros.
cron
base images for various distros.
See https://blog.thesparktree.com/cron-in-docker for more information
Hi, thanks for this repo. It's really useful. I have one problem though. I want to use a non-root user in the image. Do you have any idea how this could be done?
I get some error like this:
crond[11]: can't set groups: Operation not permitted
FROM python:3.8-alpine
ENV PYTHONPATH /opt/app
RUN rm -rf /etc/periodic
COPY entrypoint.sh /usr/bin/
COPY cronjob /var/spool/cron/crontabs/root
ARG UID=9000
ARG USER=uwsgi
RUN adduser \
--disabled-password \
--gecos "" \
--home ${PYTHONPATH} \
--no-create-home \
--uid ${UID} \
${USER}
RUN touch /etc/environment
RUN chown ${UID}:${UID} /etc/environment
WORKDIR ${PYTHONPATH}
COPY cmd.sh .
USER ${USER}
ENTRYPOINT [ "entrypoint.sh" ]
CMD [ "./cmd.sh" ]
#!/bin/sh
set -ex
env >> /etc/environment
crond -l 2
exec "$@"
* * * * * date
#!/bin/sh
while true;
do
echo "running"
sleep 12
done
I found one old question on SO regarding this:
https://stackoverflow.com/questions/53103621/how-to-start-crond-as-non-root-user-in-a-docker-container
Issue is when I want to link crontab via Docker-compose.yaml, crontab will not execute jobs. Where could be the issue ? I want to keep crontab outside of image and have better flexibility and avoid regenerating image everytime I change crontab.
Solution 1 is something I look for for flexibility, Solution 2 works fine but not flexible.
Solution 1: does not work but flexible
- Dockerfile
FROM python:3.11.2-alpine3.17
ADD . /scrapper
WORKDIR /scrapper
RUN which crond && \
rm -rf /etc/periodic && \
apk add --no-cache tzdata && \
pip install -r requirements.txt
RUN chmod +x /scrapper/entrypoint.sh
ENTRYPOINT ["/scrapper/entrypoint.sh"]
# source: `docker run --rm -it alpine crond -h`
# -f | Foreground
# -l N | Set log level. Most verbose 0, default 8
CMD ["crond", "-f", "-l", "2"]
- Docker-compose.yaml
version: '3.7'
services:
scrapper:
image: salvq/scrapper:1.0.0
container_name: scrapper
restart: unless-stopped
init: true
environment:
- TZ=Europe/Prague
volumes:
- /share/Container/trade-scrapper:/trade-scrapper
- /share/Container/trade-scrapper/config/crontab:/var/spool/cron/crontabs/root
- docker-compose executed but no job being executed
Creating trade-scrapper ... done
Attaching to trade-scrapper
trade-scrapper | crond -f -l 2
- Crontab file is present
/scrapper # cat /var/spool/cron/crontabs/root
# do daily/weekly/monthly maintenance
# min hour day month weekday command
# */15 * * * * run-parts /etc/periodic/15min
# 0 * * * * run-parts /etc/periodic/hourly
# 0 2 * * * run-parts /etc/periodic/daily
# 0 3 * * 6 run-parts /etc/periodic/weekly
# 0 5 1 * * run-parts /etc/periodic/monthly
* * * * * python3 /scrapper/mailscrapper.py
Solution 2: works but not flexible
When I do same but COPY
crontab directly in image, docker-compose works just fine
- Dockerfile
FROM python:3.11.2-alpine3.17
ADD . /scrapper
WORKDIR /scrapper
COPY crontab /var/spool/cron/crontabs/root
RUN which crond && \
rm -rf /etc/periodic && \
apk add --no-cache tzdata && \
pip install -r requirements.txt
RUN chmod +x /scrapper/entrypoint.sh
ENTRYPOINT ["/scrapper/entrypoint.sh"]
# source: `docker run --rm -it alpine crond -h`
# -f | Foreground
# -l N | Set log level. Most verbose 0, default 8
CMD ["crond", "-f", "-l", "2"]
- Docker-compose.yaml
version: '3.7'
services:
scrapper:
image: salvq/scrapper:1.0.0
container_name: scrapper
restart: unless-stopped
init: true
environment:
- TZ=Europe/Prague
volumes:
- /share/Container/trade-scrapper:/trade-scrapper
- Result via dockerfile is OK
Creating trade-scrapper ... done
Attaching to trade-scrapper
trade-scrapper | crond -f -l 2
trade-scrapper | Program started
trade-scrapper | Program finished
trade-scrapper | OK
I've tried it on my system and it does not seem to run for any of the 3 containers. After the containers have started, this is all the output I ever get, no matter how many minutes I wait:
Attaching to ubuntu-cron, centos-cron, alpine-cron
alpine-cron | crond -f -l 2
ubuntu-cron | cron -f -l 2
centos-cron | crond -n
System info:
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 21.04
Release: 21.04
Codename: hirsute
$ docker-compose version
docker-compose version 1.25.0, build unknown
docker-py version: 4.1.0
CPython version: 3.9.5
OpenSSL version: OpenSSL 1.1.1j 16 Feb 2021
$ docker version
Client: Docker Engine - Community
Cloud integration 0.1.18
Version: 20.10.6
API version: 1.41
Go version: go1.13.15
Git commit: 370c289
Built: Fri Apr 9 22:47:17 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.6
API version: 1.41 (minimum version 1.12)
Go version: go1.13.15
Git commit: 8728dd2
Built: Fri Apr 9 22:45:28 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.4
GitCommit: 05f951a3781f4f2c1911b05e61c160e9c30eaa8e
runc:
Version: 1.0.0-rc93
GitCommit: 12644e614e25b05da6fd08a38ffa0cfe1903fdec
docker-init:
Version: 0.19.0
GitCommit: de40ad0
Any idea why it's not working for me?
Your method of loading the crontab via a volume does not work, at least in Ubuntu jammy as base image.
The issue is that cron will refuse to load a crontab that has group/others write privileges, and when you load it via a volume it get 0664
This can be fixed by including the /etc/crontab during the build and using a RUN step to force the 0644 perms, or just forcing the perms in the entrypoint just before call to start the cron daemon.
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.