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.
SQLite data disappears after docker compose down/up — what am I missing?
-
PodcastNewbie
- New member
- Posts: 3
- Joined: Sat Nov 07, 2026 12:42 pm
SQLite data disappears after docker compose down/up — what am I missing?
-
SocialMediaSkeptic
- New member
- Posts: 5
- Joined: Wed Nov 04, 2026 6:42 am
Re: SQLite data disappears after docker compose down/up — what am I missing?
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.
-
TechJunkie
- New member
- Posts: 4
- Joined: Sun Oct 25, 2026 9:42 pm
Re: SQLite data disappears after docker compose down/up — what am I missing?
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.
-
PodcastNewbie
- New member
- Posts: 3
- Joined: Sat Nov 07, 2026 12:42 pm
Re: SQLite data disappears after docker compose down/up — what am I missing?
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.
-
SocialMediaSkeptic
- New member
- Posts: 5
- Joined: Wed Nov 04, 2026 6:42 am
Re: SQLite data disappears after docker compose down/up — what am I missing?
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.
-
TechJunkie
- New member
- Posts: 4
- Joined: Sun Oct 25, 2026 9:42 pm
Re: SQLite data disappears after docker compose down/up — what am I missing?
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.
-
PodcastNewbie
- New member
- Posts: 3
- Joined: Sat Nov 07, 2026 12:42 pm
Re: SQLite data disappears after docker compose down/up — what am I missing?
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?
-
SocialMediaSkeptic
- New member
- Posts: 5
- Joined: Wed Nov 04, 2026 6:42 am
Re: SQLite data disappears after docker compose down/up — what am I missing?
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.
Who is online
Users browsing this forum: No registered users and 4 guests