Prompt
persistent storage for Docker containers
Latest observation
Oct 23, 2026
OpenAI APIWeb search: off
Docker containers are ephemeral by default, so if you want data to survive container deletion/recreation, you should use persistent storage. The main options are:
1) Volumes (recommended)
Docker-managed storage stored outside the container lifecycle.
Create and use a volume
docker volume create mydata
docker run -d --name app -v mydata:/data myimage
- Data in
/datapersists even if the container is removed. - Best choice for databases and application data.
Inspect or list volumes
docker volume ls
docker volume inspect mydata
Remove a volume
docker volume rm mydata
2) Bind mounts
Mount a specific directory from the host into the container.
docker run -d --name app -v /host/path:/data myimage
- Data is stored in
/host/pathon the host machine. - Good for development, config files, or when you need direct host access.
- Less portable than volumes.
3) tmpfs mounts
Data is stored in memory only, not persistent.
docker run -d --tmpfs /data myimage
- Not persistent.
- Useful for temporary sensitive data or performance-sensitive temporary storage.
Best practice
- Use named volumes for persistent application data.
- Use bind mounts for development and config sharing.
- Avoid storing important data inside the container filesystem itself.
Example with Docker Compose
services:
db:
image: postgres:16
volumes:
- dbdata:/var/lib/postgresql/data
volumes:
dbdata:
If you want, I can also show:
- how persistent storage works in Docker Compose
- how to migrate data from a container to a volume
- how to use persistent storage for PostgreSQL/MySQL/Redis