Cron在postgresql:alpine docker容器中 [英] Cron in postgresql:alpine docker container

查看:386
本文介绍了Cron在postgresql:alpine docker容器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用普通的postgresql:alpine泊坞映像,但是必须每天计划数据库备份。我认为这是一个很普通的任务。

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.

我创建了一个脚本 backup 并存储在 / etc / periodic / 15min ,并使其可执行:

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.

我的问题是让 crond 自动运行。

My problem is getting crond to run automatically.

如果我执行 docker exec my-postgresql-container crond ,将启动守护进程并执行cron,但是我想将其嵌入到我的Dockerfile中

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.

推荐答案


注意:如果您想将同一容器与多重服务

安装 Supervisord 其中将使您能够运行 crond postgresql Dockerfile 将如下所示:

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

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

然后,您可以启动docker构建过程并从新映像运行容器。随时根据需要修改 Dockerfile postgres_cron.ini

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

这篇关于Cron在postgresql:alpine docker容器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆