icbestCA / giftmanager

Gift ideas manager for families. See the demo here: https://giftmanagerdemo.pages.dev/
MIT License
74 stars 4 forks source link

Store data in own directory #17

Closed MarcelBruckner closed 2 months ago

MarcelBruckner commented 2 months ago

Problem

After every restart of the docker container all data stored in ideas.json and users.json is lost.

Idea

We need to have the files at a location that can be mounted into the container, so that it can be persisted.

Implementation

I moved the files to a dedicated data directory, stored at /app/data. The files are read from the directory /app/data instead of ./ as previously. The path /app/data can now be mounted into the docker container and the ideas.json and users.json are persisted.

When a directory is mounted into a docker container, the directory of the host overrides the directory in the container. Using a predefined users.json and ideas.json was not possible before, as mounting the directory from the host would override the directory (and thus the files) in the container. I additionally implemented the prepopulation of the files with the default values programatically.

Other

This merge request should also fix this issue: https://github.com/icbestCA/giftmanager/issues/9#issue-2485308802

icbestCA commented 2 months ago

So this is a problem with the permissions? So you suggest to mount the data folder in a location with controlled permissions?? If so, we will also need to modify the docker script with the backup system. Thanks I will look into that.

MarcelBruckner commented 2 months ago

No, its not a permission problem but how docker containers work.

When you stop a container, all data is wiped. On restart of the container, the container is in its original state. So by stopping the container, the content of the users.json and ideas.json was lost.

To persist data in a container, you have to mount a directory from the host (your PC) into the container. This is now possible by using the /app/data directory, which can be mounted.

See: https://docs.docker.com/engine/storage/ https://docs.docker.com/engine/storage/volumes/

MarcelBruckner commented 2 months ago

BTW: Thank you for the nice app! I was looking for a gift manager for a long time, but never had time to implement it myself! Keep up the good work!

MarcelBruckner commented 2 months ago

What you additionally gain by using this approach is, that you don't have to backup data anymore. Everything is already persisted in the mounted directory of the host, so no backups needed.

MarcelBruckner commented 2 months ago

Hey, thank you for the feedback! I fixed the f-strings as per your stacktrace. Can you please try running it in Docker again?

MarcelBruckner commented 2 months ago

Can you maybe add the Dockerfile to the repository so that I can build and test it myself?

icbestCA commented 2 months ago

The dockerfile is in this repo. https://github.com/icbestCA/dockergiftmanager I will merge it with the main later this day. Because the app.py file is different from the flask app, I will make a separate folder. But for now you can take it on the other repo, it's the one I use.

icbestCA commented 2 months ago

Thanks!! I've used your code but added it to a new folder. Thank you, I've pushed the new docker image.