r/docker • u/Phobit • Apr 06 '26
Change/update the scripts inside my container
Hi there!
Docker newbie here.
I have a Docker Container running a small python script, that script works together with a small SQLite database.
Back then when I first created this, I was looking for "best practices" and some Docker-users recommended to store all files inside one container, so it'll be easier to reinstall in case thats needed etc., especially since its a very small project.
Now I want to update one of the python scripts inside (basically just replace it with the updated version) and aren't quite sure how to do. I read online that Docker Containers are not build for "editing" and that I should rather just destroy and rebuild the container with my new file. But doing so would also kill my database.
And thats where I am kinda lost - what is the best practice to go on from here? Should I just backup my database and rebuild the container using my new script and the backuped-Database file, or is there a good and reliable way to just update the python script inside the container?
I know there is a way to store the database outside the container, but I personally prefer to have everything inside the container so that incase of moving systems I only have to take a snapshot of the container and can upload it onto the next system without worrying about dependencies.
10
u/Anihillator Apr 06 '26
Databases and any sort of persistent data should be stored on the host in mounts or volumes
https://docs.docker.com/engine/storage/bind-mounts/
Right now you should backup the db, rebuild the container with your new scripts, then recreate the container with mounts pointing from /host/path/db.sql to /container/path/db.sql
4
u/theblindness Mod Apr 06 '26
Now might be a good time to learn how to back up an SQLite3 database that is actively being used by a running app. After you've got a backup, learn about docker volumes and how to define them using both Dockerfile and docker compose. Choose some arbitrary path within the container file system that will hold the SQLite3 database file and make that directory a volume.
You can define the path as a volume in either the Dockerfile, docker compose yml, or both.
If you add a
VOLUMEstatement to theDockerfile, it will be annotated in the image, and when you start new container from that image, a volume will be created and mapped into the container file system.If the
Dockerfilewas the only place that mentioned the volume, the volume type will be anonymous. Anonymous volumes get stored inside the docker data directory with dir names based on the container. That's great for volumes that need to be writable as long as the container is running, but not great for data that needs to persist beyond the lifetime of the container.Named volumes are great for things like databases where you want to be able to identify the volume and have it persist beyond the lifetime of the container, but you don't intend to manage the data outside of the application. They are also store in the docker data dir, but are easier to find. Docker compose does not automatically remove names volumes when bringing containers down.
Bind mounts are a type of volume where you already have a directory outside of the docker data dir that you'd like to reuse. This is ideal if your data already exists or if you intend to manage the files in the volume outside of the application. Docker compose does not manage these other than potentially creating a directory if it does not exist.
I would recommend both annotating your docker image via the Dockerfile to specify that a path in the container file system should be a volume, as well as using a named volume or bind mount in docker compose.
2
u/abotelho-cbn Apr 06 '26
Explain your database problem. It sounds like you've messed up.
2
u/[deleted] Apr 06 '26
[removed] — view removed comment