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!
Accepted Answer
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:
docker run -v /my/local/path:/app/data your-image-name
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:
volumes:
- ./data:/app/data
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.
docker run -d \ --name mycontainer \ -v /path/on/host:/app/data \ myimage
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
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.