Page 0 of 0
SQLite data disappears after docker compose down/up — what am I missing?
Posted: Tue Dec 01, 2026 10:51 pm
by PodcastNewbie
Hey all, I'm pretty new to Docker and I've got a small Flask app with SQLite that works fine locally. I moved it to a VPS with docker compose and now every time I recreate the container, my data is gone. I added a named volume dbdata:/app/data in the compose file and my app writes to data/app.db, but after docker compose down and up -d the tables are empty again. I thought named volumes were supposed to survive that. Am I missing something obvious? Where would you start debugging this? I can paste the compose file if that helps.
Re: SQLite data disappears after docker compose down/up — what am I missing?
Posted: Wed Dec 02, 2026 8:16 am
by SocialMediaSkeptic
Paste the compose file and the SQLAlchemy URI, because this is usually one of two things: the volume is mounted somewhere the app isn't actually writing, or the DB path is relative and points into the container filesystem. dbdata:/app/data only helps if the app writes to /app/data. Also, are you running docker compose down -v? That little -v deletes named volumes and trips up basically everyone once.
Re: SQLite data disappears after docker compose down/up — what am I missing?
Posted: Wed Dec 02, 2026 9:30 am
by TechJunkie
Yep, what SocialMediaSkeptic said. Check the actual path inside the container first: docker compose exec app ls -la /app/data. If the db file isn't there, your mount is fine but your app is writing elsewhere. Flask-SQLAlchemy is sneaky here — sqlite:///data/app.db is relative to the instance folder, not the project root. So you may be writing to /app/instance/data/app.db while your volume sits at /app/data. That one bit me before.
Re: SQLite data disappears after docker compose down/up — what am I missing?
Posted: Wed Dec 02, 2026 9:47 am
by PodcastNewbie
Thanks both. Here's the relevant bit:
services:
app:
build: .
volumes:
- dbdata:/app/data
volumes:
dbdata:
And in config I have SQLALCHEMY_DATABASE_URI = "sqlite:///data/app.db". I haven't used down -v, just down then up -d. So if I'm reading TechJunkie's reply right, my URI might be writing to the instance folder instead of /app/data. How do I make it absolute without hardcoding weird stuff? Sorry if that's basic.
Re: SQLite data disappears after docker compose down/up — what am I missing?
Posted: Wed Dec 02, 2026 9:55 am
by SocialMediaSkeptic
Yeah that's the bug. SQLite URIs are annoying: three slashes means relative, four slashes means absolute. So sqlite:////app/data/app.db should point at the mounted volume. Or set the path from an env var and build it in Python, but for a small app the four-slash version is fine. Also check ownership if you ever switch to a bind mount; named volumes usually behave, but SQLite can still throw permissions errors.
Re: SQLite data disappears after docker compose down/up — what am I missing?
Posted: Wed Dec 02, 2026 3:10 pm
by TechJunkie
Exactly. Four slashes, then restart and run docker compose exec app ls -la /app/data again — you should see app.db and maybe app.db-wal/app.db-shm if WAL mode is on. Don't panic at those extra files. And keep down -v out of your muscle memory unless you actually want to wipe data. If you're doing backups later, stop the container or use SQLite's .backup command rather than copying a live db file.
Re: SQLite data disappears after docker compose down/up — what am I missing?
Posted: Thu Dec 03, 2026 8:48 am
by PodcastNewbie
Just tried sqlite:////app/data/app.db and recreated the container — data survived the restart. That was it. Thanks a lot, both. I definitely would've spent another hour poking at the volume config. Quick follow-up: is there anything I should do for backups, or is copying the db file enough for a tiny app like this?
Re: SQLite data disappears after docker compose down/up — what am I missing?
Posted: Fri Dec 04, 2026 8:15 am
by SocialMediaSkeptic
For a tiny app, stop the container and copy the file, or use sqlite3 /app/data/app.db ".backup /app/data/backup.db" from inside the container. Copying a live db can work but it's a good way to get a weird corrupted backup if writes are happening. Also keep a copy off the VPS. If this ever becomes multi-user or you start seeing locked database errors, that's your sign to move to Postgres instead of fighting SQLite in Docker.