How to persist data between container runs? (junior)
To keep data from disappearing when a container is recreated, it is moved outside the container using volumes or bind mounts.
Two main approaches (junior)
1. Docker volume (the best option for production)
bash
docker volume create mydata
docker run -v mydata:/app/data IMAGEThe data is stored outside the container, but managed by Docker.
2. Bind mount (mounting a host folder)
bash
docker run -v /path/on/host:/app/data IMAGEThe data lives directly on the host, and the container simply uses it.
What this gives you
- data doesn't disappear on
docker stop,docker rm, or when the image is rebuilt; - other containers can connect to it;
- it's convenient for backups.
Summary
To persist data between container runs, use a volume or bind mount - they move the data outside the container so it doesn't get lost.
If needed, I can draw a simple diagram or show how to check a volume with docker volume ls.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.