I recently deployed a website that only uses a database for sessions (for dark mode). I kept having problems with it because there is no database. I found a temporary solution by doing a makemigrations and migrate from the fly console, but it doesn’t actually solve the problem, as I still encounters it (I had to disable the dark mode).
Basically, what I need is the app to have the sqlite database I already use on my django project. I read about volumes, etc, but I really don’t get it.
How can I use my db.sqlite3 permanently in my app, on fly.io ?
It is quite important to me, as I have another (bigger) django website to deploy, and this one has a database that I want to use.
When an application creates a Sqlite DB without a volume, it is created inside the VM defined by the Dockerfile. That’s great and all until you restart or deploy a new version. No changes made by the application are saved (aka persisted) in the file system of the Docker image, or VM.
Normally this isn’t a problem if the data being accessed is in an external database like MySQL, Postgres, Redis, etc. Those other databases are persisting the data.
If the database is SQLite, it is running WITH your application, inside the temporary, ephemeral system. Volumes let you attach a persistent file system to your machine. That’s where the SQLite database should be written to. That way, when you deploy a new version of your app, your data still exists and is attached to the machine.
Even if you’re only storing session data in the DB, every time you restart or deploy a new version, it would lose all the session data, effectively logging everyone out. Not the behavior you want.
Think of a volume as a way of attaching a physical disk to the a temporary, throw-away machine. If you want to keep any data, you either write it to another machine that’s storing it with a volume (ie Postgres), or you write it to a volume yourself that’s attached to the machine.