✅ PostgreSQL in docker container
Hello everyone,
I hope this isn't too silly a question.
I just pulled the docker image of postgres, started a container and then used that container with my .NET API and Entity framework project to create a database and populate it.
The only problem is that I've seen some strange information on the Internet that say that docker container is stateless and that, in principle, you'd have to manipulate the database data so that it's copied somewhere on the host disk if you don't want the data to disappear after each container shutdown.
However, thanks to DBeaver, I can see that my database has all the data I seeded with entity framework, even after restarting the container, and I can also see that I can't access my database until I start the container.
So where is my database data stored? In the container or on my local computer disk?
8 Replies
A word of clarification: I have access to my data. It's not really a problem, it's a question of understanding what I actually do.
At the end it is all on your local computer disk - you can imagine it as a "virtual machine" that runs linux (wsl) and on that "machine" are all you docker related files.
most container hosts (such as docker) include mechanisms to create persistent volumes so data isn't lost if you remove / delete containers. Also, the same container will maintain its own state (via a virtual disk of sorts) between multiple restarts. Data is only lost if you delete a container and do not have any form of persistent storage set up.
for example, this is the compose file I use to run postgres on my homelab:
This compose file mounts a directory
/mnt/database/postgres-15
to the folder postgres uses to store database information (/var/lib/postgresql/data
). Even if I manually delete my postgres-15
container I can create another, point it to the same folder, and all of my data is still there.Oh right, thank you very much, it's much clearer now ❤️
So if I understand correctly, the data is saved between a stop and a relaunch of the container, but it's a bit fragile in the sense that deleting the container will also delete all the data saved in it.
I've chosen to use a docker container for my PostgreSQL database solely for the development of my application. So if I've understood correctly, as long as I'm still in development, there's no problem in doing it this way 😄
And in the future, I hope to deploy my application using an Azure database-style service. I won't need docker for my database at this stage.
Once again, thank you very much for your answers!! 😁
It is still fine to use virtualization like that in production, we use k8s for all of our environments at work (including prod).
Oh that's really nice, I didn't know it was a thing, at least for production databases 😮
But maybe it will require from me a better knowledge of how docker works, to manage well the DB, I don't think I will have the time to learn it enough, before my internship starts 😞
Depending on the size of company you're going to be interning / working at, you may not be the one managing databases. Having a solid understanding of docker is always good, though, as it is great for local development and testing.
Thanks again for your answers 🙏
I will close the thread now, see you soon on the server 👋