I'm new to using Docker and currently have a container running a Python script alongside an SQLite database. Originally, I stored everything in one container for simplicity, following advice I found about best practices for small projects. Now, I want to update the Python script with the latest version, but I'm concerned about the potential loss of my database if I destroy and rebuild the container. What's the best approach here? Should I back up my database and then rebuild the container, or is there a way to just replace the script inside without affecting the database? I understand that it's usually better to keep databases outside the container, but I liked having everything in one place for easy mobility. Any tips?
3 Answers
Can you explain your database setup further? It seems like it might be a bit tangled right now. Keeping everything inside the same container can lead to issues like this, but I understand you were aiming for simplicity at the time. If you’re worried about losing your DB while updating, just focus on backing it up. That's the safest route.
It's really important to separate your persistent data from your container. Right now, it sounds like you're set up in a way that could lead to losing your data if something goes wrong with your container. The best approach is to back up your SQLite database, change your container setup to use volumes or bind mounts for the database, and then rebuild the container with your updated scripts. That way, you can keep your data safe and easily manage updates.
So just to confirm, if I ever need to rebuild the system, I just have to recreate those mounts on the host right? It seems a bit annoying, but I guess it’s better than losing my database.
Yeah, you really want to avoid committing a running container if you can help it. Doing that just makes your images larger and complicates things later. Storing your SQLite database in a volume or bind mount lets you delete the container without losing the data. When you need to set things up on a new machine, you can just recreate that mount to bring back your data alongside your updated container.
So ultimately, the ideal way would be to create the container so that deleting it doesn’t wipe out my database by using bind mounts, right? That would definitely simplify things in the long run!

Basically, I have an SQLite file sitting inside the container, and now I realize that if the container goes, I lose all my data. I personally just wanted a straightforward setup two years ago without dealing with configurations. Now I need to figure out how to update my Python script safely.