簡體   English   中英

在Postgres docker容器中啟動一個系統服務

[英]Start a system service in the Postgres docker container

我想擴展postgres:10.2 Dockerfile 以添加一個 cron 作業,在特定日期執行一些 SQL 查詢:

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

問題是:cron 服務沒有啟動:

在 docker 容器內:

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

我很難啟動它...

在 Dockerfile 中,我試過:

  • 添加RUN service cron start :沒有變化
  • 添加CMD service cron start :當容器啟動時,它以Starting periodic command scheduler: cron結束而不啟動數據庫。
  • 要添加CMD postgres && service cron start :當容器啟動時,它以"root" execution of the PostgreSQL server is not permitted. 無需啟動數據庫。
  • 添加包裝 CMD 腳本,如https://docs.docker.com/config/containers/multi-service_container/ :相同的行為。
  • 添加ENTRYPOINT "docker-entrypoint.sh" && service cron start :同上
  • 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=探索): Starting periodic command scheduler: croncron: can't open or create /var/run/crond.pid: Permission denied failed!
  • update-rc.d cron defaults && update-rc.d cron enable添加到docker-entrypoint.sh :沒有任何變化。
  • 添加set -- su-exec root:root /bin/bash -c "service cron start" : 沒有變化
  • 添加set -- su-exec root:root /bin/bash -c "update-rc.d cron defaults && update-rc.d cron enable" :沒有變化
  • 要添加gosu root:root /bin/bash -c "service cron start" :容器以error: failed switching to "root:root": operation not permitted.
  • 要添加exec gosu root:root /bin/bash -c "service cron start" :容器以Starting periodic command scheduler: cron.

您知道如何在 postgres 啟動之前運行系統服務嗎? 我想擴展 postgres:10.2。

謝謝 !

好的,我明白為什么......我回答我自己的問題來幫助大家: docker-entrypoint.sh腳本運行exec gosu postgres "$BASH_SOURCE" "$@"命令(即使在最后一個 postgres 版本中: https:/ /github.com/docker-library/postgres/blob/master/docker-entrypoint.sh )再次調用此腳本,但作為postgres用戶。

所以每一個系統操作都需要在這個命令之前執行。

例如:編寫一個名為system_configure的 function 並在該行之前調用它:

# ... (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)

您還可以將任何其他 docker 映像與 supervisord 一起使用,例如 ubuntu,但出於安全原因更喜歡 distroless 映像。

只需在您的Dockerfile中使用以下命令運行一次:

RUN service cron start

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM