Oll*_*etz 4 postgresql cron docker docker-container
我正在使用“普通”postgresql:alpine docker 映像,但必须每天安排数据库备份。我认为这是一个非常常见的任务。
我创建了一个脚本backup
并将其存储在容器中/etc/periodic/15min
,并使其可执行:
bash-4.4# ls -l /etc/periodic/15min/
total 4
-rwxr-xr-x 1 root root 95 Mar 2 15:44 backup
Run Code Online (Sandbox Code Playgroud)
我尝试手动执行它,效果很好。
我的问题是crond
自动运行。
如果我 exec docker exec my-postgresql-container crond
,守护进程将启动并且 cron 工作,但我想将其嵌入到我的 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
Run Code Online (Sandbox Code Playgroud)
我不知道如何重写或覆盖官方镜像中的命令。出于更新原因,如果可能的话,我也想保留这些图像。
注意:如果您想将同一个容器用于多个服务,请选择此选项
安装Supervisord,这将使您能够运行crond
和postgresql
. 将会Dockerfile
如下:
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"]
Run Code Online (Sandbox Code Playgroud)
并将postgres_cron.ini
如下所示:
[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
Run Code Online (Sandbox Code Playgroud)
然后您可以启动 docker 构建过程并从新映像运行容器。请随意修改Dockerfile
或postgres_cron.ini
根据需要
归档时间: |
|
查看次数: |
8585 次 |
最近记录: |