postgresqldockercrondocker-container

Cron in postgresql:alpine docker container


I am using the "plain" postgresql:alpine docker image, but have to schedule a database backup daily. I think this is a pretty common task.

I created a script backupand stored in the container in /etc/periodic/15min, and made it executable:

bash-4.4# ls -l /etc/periodic/15min/
total 4
-rwxr-xr-x    1 root     root            95 Mar  2 15:44 backup

I tried executing it manually, that works fine.

My problem is getting crond to run automatically.

If I exec docker exec my-postgresql-container crond, the deamon is started and cron works, but I would like to embed this into my Dockerfile

FROM postgres:alpine

# my backup script, MUST NOT have .sh extension
COPY backup.sh /etc/periodic/15min/backup 
RUN chmod a+x /etc/periodic/15min/backup

RUN crond # <- doesn't work

I have no idea how to rewrite or overwrite the commands in the official image. For update reasons I also would like to stay on these images, if possible.


Solution

  • Note: This option if you would like to use the same container with multiple service

    Install Supervisord which will makes you able to run crond and postgresql. The Dockerfile will be as the following:

    FROM postgres:alpine
    RUN apk add --no-cache supervisor
    RUN mkdir /etc/supervisor.d
    COPY postgres_cron.ini /etc/supervisor.d/postgres_cron.ini
    ENTRYPOINT ["/usr/bin/supervisord", "-c", "/etc/supervisord.conf"]
    

    And postgres_cron.ini will be as the following:

    [supervisord]
    logfile=/var/log/supervisord.log ; (main log file;default $CWD/supervisord.log)
    loglevel=info                ; (log level;default info; others: debug,warn,trace)
    nodaemon=true              ; (start in foreground if true;default false)
    
    [program:postgres]
    command=/usr/local/bin/docker-entrypoint.sh postgres
    autostart=true
    autorestart=true
    
    [program:cron]
    command =/usr/sbin/crond -f
    autostart=true
    autorestart=true
    

    Then you can start the docker build process and run a container from your new image. Feel free to modify the Dockerfile or postgres_cron.ini as needed