I’m running a Docker container on my Droplet and noticed that every time I recreate the container, all my data is gone. What’s the proper way to store data so it doesn’t get lost when the container restarts or gets rebuilt?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Hey there 👋
Great question! By default, Docker containers are ephemeral, meaning all data inside the container is lost when it’s removed. To persist data, you should use volumes.
Here’s a simple example:
This mounts a local folder (
/my/local/path
) on your Droplet to/app/data
inside the container — so anything your container writes there will stay even if the container is removed or restarted.If you’re using
docker-compose
, it looks like this:Also if you are just getting started with Docker, this free eBook might be helpful: 👉 https://github.com/bobbyiliev/introduction-to-docker-ebook
Hope that helps!
- Bobby
Heya, @1329f70102f4446f94457e686ea96f
I’ll also recommend to use volumes for this. The other option will be to use bind Mounts (Host Directory)
You can mount a directory from your host system directly into the container.
This gives you full control and visibility of the files on the host machine.
⚠️ Be careful with permissions and SELinux/AppArmor when using bind mounts.
I’ll still however vote for volumes over bind mounts.
Regards