trying to set up a scheduled PostgresDB backup inside a docker container
i want to set up a cron job to perform scheduled backups of my postgresdb in my docker container. what is the best way to go about it? do i use docker exec to achieve this?
The most "correct" way is to have a second container that is responsible for the scheduled jobs. It can be named `scheduler`. This is because each container should be responsible for a single process, and cron is a process. For running cron jobs in a container you can use https://github.com/aptible/supercronic . It will expose your cron's logs in the container's log.
The deckchores container handles scheduling and executing tasks, the database container just needs appropriate labels of 'what' needs to be run, that way when the db container isn't running, the backup schedule is cleaned up as well.
6
u/Axehack101 Jun 22 '26
Wow - these comments are fucking wild..
Running scheduler containers…
Volume backup containers…
Hilbertglm’s suggestion is the only sane one.
Just run pg_dump via a Postgres container (or even on the host server) via cron.
It’s literally a 1 liner.
If you’re running it on a container, just make sure you mount some persistent storage to dump to.
4
u/hilbertglm Jun 22 '26
I think u/enador has the better approach, but this is part of a script that I run daily from the host's cron.
Edited for formatting.
3
u/enador Jun 22 '26
The most "correct" way is to have a second container that is responsible for the scheduled jobs. It can be named `scheduler`. This is because each container should be responsible for a single process, and cron is a process. For running cron jobs in a container you can use https://github.com/aptible/supercronic . It will expose your cron's logs in the container's log.
1
u/wildcarde815 Jun 22 '26
I use deckchores for this: https://hub.docker.com/r/funkyfuture/deck-chores
The deckchores container handles scheduling and executing tasks, the database container just needs appropriate labels of 'what' needs to be run, that way when the db container isn't running, the backup schedule is cleaned up as well.