简体   繁体   中英

Start a system service in the Postgres docker container

I want to extends the postgres:10.2 Dockerfile in order to add a cron job doing some SQL queries at specific dates:

FROM postgres:10.2

COPY task-purge.sh /usr/local/share/
RUN chown postgres:postgres /usr/local/share/task-purge.sh
RUN chmod 700 /usr/local/share/task-purge.sh

COPY query-task-purge.sql /usr/local/share/
RUN chown postgres:postgres /usr/local/share/query-task-purge.sql
RUN chmod 700 /usr/local/share/query-task-purge.sql

The problem is: the cron service is not started:

Inside the docker container:

root@5c17ce88c333:/# service cron status
[FAIL] cron is not running ... failed!
root@5c17ce88c333:/# pgrep cron
root@5c17ce88c333:/# 

I have difficulties to start it...

In the Dockerfile, I tried:

  • To add RUN service cron start : nothing change
  • To add CMD service cron start : when the container starts, it ends with Starting periodic command scheduler: cron without starting the DB.
  • To add CMD postgres && service cron start : when the container starts, it ends with "root" execution of the PostgreSQL server is not permitted. without starting the DB.
  • To add a wrappere CMD script like https://docs.docker.com/config/containers/multi-service_container/ : same behaviour.
  • To add ENTRYPOINT "docker-entrypoint.sh" && service cron start : idem
  • To add service cron start in a new docker-entrypoint.sh (modified from the official postgres:10.2 Dockerfile https://hub.docker.com/layers/postgres/library/postgres/10.2/images/sha256-4b6b7bd361a3b7b69531b2c16766a38b0f3a89e9243f5a49ff16180dd2d42273?context=explore ): Starting periodic command scheduler: croncron: can't open or create /var/run/crond.pid: Permission denied failed!
  • To add update-rc.d cron defaults && update-rc.d cron enable to the docker-entrypoint.sh : nothing change.
  • To add set -- su-exec root:root /bin/bash -c "service cron start" : nothing change
  • To add set -- su-exec root:root /bin/bash -c "update-rc.d cron defaults && update-rc.d cron enable" : nothing change
  • To add gosu root:root /bin/bash -c "service cron start" : the container ends with error: failed switching to "root:root": operation not permitted.
  • To add exec gosu root:root /bin/bash -c "service cron start" : the container ends with Starting periodic command scheduler: cron.

Do you have any idea how I can run a system service before postgres start? And I want to extends postgres:10.2.

Thanks !

Ok, I have understand why... I answer my own question to help everybody: the docker-entrypoint.sh script runs a exec gosu postgres "$BASH_SOURCE" "$@" command (even in the last postgres version: https://github.com/docker-library/postgres/blob/master/docker-entrypoint.sh ) which call again this script but as postgres user.

So every system operations needs to be executed before this command.

For example: write a function called system_configure and call it before that line:

# ... (outside main)
system_configure() {
    echo "[x] Crontab service start ..."
    service cron start
    echo "[x] Crontab service started"
}

# ... (inside main function path)
    system_configure

    exec gosu postgres "$BASH_SOURCE" "$@"

# ... (end main)

You could also use any other docker image like ubuntu with supervisord, but prefers distroless image for security reasons.

Just run it once in your Dockerfile using:

RUN service cron start

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM