Prompt
why is my app losing files after container restart
Latest observation
If your app “loses files” after a container restart, the most common reason is that the files were stored inside the container’s writable layer, which is ephemeral. When the container is recreated or restarted, that layer may be reset or replaced.
Common causes
1) Files are written only inside the container filesystem
Containers are meant to be disposable. Anything saved to paths like:
/tmp- app working directories
/var/app/datainside the container
will disappear if the container is removed and recreated, or sometimes if the runtime replaces the container.
2) No volume or bind mount is attached
If you want files to survive restarts, you need to store them on:
- a Docker volume
- a bind mount
- external storage like S3, NFS, EBS, Azure Disk, etc.
Example Docker volume:
docker run -v mydata:/data myimage
Example bind mount:
docker run -v /host/path:/data myimage
3) The container is being recreated, not just restarted
A true restart usually keeps the container filesystem, but orchestration tools often:
- stop the old container
- create a new one
Examples:
- Docker Compose recreating containers
- Kubernetes restarting pods
- ECS replacing tasks
- Cloud Run creating fresh instances
In those cases, files not on persistent storage are lost.
4) The app writes to the wrong path
You may have mounted persistent storage, but your app is saving files somewhere else, such as:
- a different directory
- a temp folder
- a path configured differently in production
5) Permissions or cleanup scripts remove files
Less common, but possible:
- startup scripts clean directories
- log rotation or app cleanup jobs delete files
- permissions prevent proper writes, leading to partial persistence issues
How to check
- Inspect where the app saves files
- Verify whether that path is mounted as a volume
- Check if your platform recreates containers/pods on restart
- Confirm the data directory exists on persistent storage
- Test by creating a file, restarting, and checking whether it’s still on the mounted volume
What to do
- Store persistent files outside the container filesystem
- Use a mounted volume or external storage
- Make sure the app writes to that mounted path
- For Kubernetes, use a PersistentVolumeClaim
- For Docker, use a named volume or bind mount
Example
If your app writes to /app/uploads, but you didn’t mount anything there:
docker run myimage
Those uploads will vanish when the container is replaced.
Fix:
docker run -v uploads:/app/uploads myimage
If you want, I can help you debug this for your specific setup — Docker, Docker Compose, Kubernetes, ECS, etc.