Git Product home page Git Product logo

docker-cron's Introduction

docker-cron's People

Contributors

analogj avatar maab avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

docker-cron's Issues

Ubuntu: /etc/crontab must be 0644 or will not work [it does not work in your way]

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.

using non root user

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

More Info

Dockerfile

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

entrypoint.sh

#!/bin/sh

set -ex

env >> /etc/environment
crond -l 2

exec "$@"

cronjob

*   *   *   *   *   date

cmd.sh

#!/bin/sh

while true;
do
    echo "running"
    sleep 12
done

Related Links

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

cron jobs apparently do not run

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?

Crontab via docker-compose issue, alpine image

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.