How to take backup of erpnext using crontab in a docker? The bench command is working but it does not execute it from crontab? Any solution?
you cant run a cronjob inside a container, but you can on the host running docker.
then you can just pass it a command docker exec -i frappecontainername cmd
* * * * docker exec -i sugarcrm_sugarcrm_1 sudo -u www-data php -f /var/www/html/cron.php > /dev/null 2>&1
Above is an example from our docker host, that runs a sugarcrm php page for example
@cjpit has it right. You should run commands from the host.
- Backup the entire Volume or Bind Mount.
- Run mysqldump
- Use ‘docker exec’ to run commands inside the container. Maybe you want to rsync directories, or run mariabackup
That is why I wanted to setup a ERPNext docker configuration with the database out on the underlying host. This way it is easy to use the 'mysqldump" command to backup as often as you want.
Unfortunately this also then requires that the …/private/files and …/public/files directories also need to be kept out on the underlying host as well.
Technically, it’s all on the host anyway. If you run MariaDB inside of docker, then your database files are either in a Volume (which is on your host). Or mapped using a Bind Mount (which points at your host).
To run “mysqldump” against a Docker MariaDB, you would just do something like this:
$ docker exec CONTAINER /usr/bin/mysqldump -u root --password=root DATABASE > backup.sql
Yeah, but that still puts the resulting file in another volume somewhere and not out where you can use simple tools like crontab to manipulate them.
For example: You cannot easily run “scp” from within docker to move files from one physical host to another backup host.
No, the resulting backup file is on your laptop. Or wherever you called ‘docker exec’ from.
In docker we run the command by going to folder “frappe_docker” and the command is “sudo ./dbench backup”, so why can’t I run this command in crontabs? It works normally why not with crons? I don’t need to call crons from inside the container
Docker is not the same as a virtual machine. Cron is not running automatically in the background. You have to manually start it.
So it’s possible to run cron jobs from a docker container. It’s just not recommended. I’ve included a few links:
How to run a cron job inside a docker container? - Stack Overflow
How to install/run Cron in a Docker Container · GitHub
The best option is to use your host’s cron. And add “docker exec …” commands.
But I am using the host machine crontab and running the command that I mentioned above not the container’s crontab.
Can you show the crontab line?
What happens if you execute the command just using a terminal?
Normally running the command created the backup folder but If I run this command from crontab it doesn’t do anything even it shows in cronjobs list
- cd ~/frappe_docker & sudo ./dbench backup
It still looks like your running it inside of the container, which won’t work.
really? that would result into not being ablke to “transport” such cronjobs with any given container (docker, lxc, …) can you explain what the benefit from running this from your container host?
… Now as I am thinking of it. Maybe it is (not that much a matter on lxd container which may be more equal to a VM then a docker container) that you would loose the cronjob once you update.or re-run/rebuild the docker container?
Yes, LXD containers are most similar to virtual machines.
Docker containers are different.
- They don’t have hardware virtualization, a hypervisor, or a boot & init system.
- They don’t have a complete operating system.
- They are tightly integrated with the Host.
- Their data and storage is not persistent. Each time you start a Docker container, it launches from scratch. It does not remember anything from the last time it was run. (exceptions → any files you map to a Volume or Bind Mount)
Docker containers are intended to run just 1 service each.
- MariaDB: That’s 1 docker container.
- Redis: That’s 1 more docker container.
- Nginx: Another docker container.
- SMTP: Another docker container.
- MongoDB: Another docker container.
You can launch them together (Docker Compose). You can link them together (IP address, Docker Swarm). But they are designed to perform 1 service, each.
Can you make them do 2+ services? Yes. But that’s not what they were designed for.
Docker’s purpose is to virtualize a service. Example:
- I can run my MariaDB in complete isolation.
- Nothing I do on my laptop/servers will ever impact MariaDB Docker.
- Nothing I do in MariaDB Docker will ever touch my host operating system or files.
- I can upgrade from MariaDB v10 to v11 by replacing my container. And be 100% confident that I have not changed anything on my host, or its services.
- I can start/stop a container in 1-2 seconds.
- They are tiny. Alpine Linux = 5 megabytes. Nginx Reverse Proxy = 15 megabytes. MariaDB = 300 megabytes.
This is very different from a Virtual Machine. On a VM, all the applications are running together. Sharing virtual disks. Sharing files. Sharing libraries, dependencies, IP addresses, sockets, etc.