Question

How do I persist data in a Docker container?

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?


Submit an answer


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!

Sign In or Sign Up to Answer

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.

Bobby Iliev
Site Moderator
Site Moderator badge
March 20, 2025
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

alexdo
Site Moderator
Site Moderator badge
March 21, 2025

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

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

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

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.