- A way to persist data outside the container’s lifecycle.
- They live on the host, not inside the container.
- Useful for sharing data between containers or keeping data when containers are destroyed/recreated.
Types of Docker Volumes
Docker supports different types of volumes to handle data persistence:
Named Volumes
- Managed by Docker.
- Stored in a specific part of the host filesystem (like
/var/lib/docker/volumes/). - Best for when you want Docker to handle storage location.
- Example:
docker volume create my_volume docker run -v my_volume:/app/data my_image
Anonymous Volumes
- Automatically created by Docker when you mount a volume without specifying a name.
- Harder to manage because they don’t have easy names.
- Good for short-lived data.
- Example:
(Docker will create a random volume name.)docker run -v /app/data my_image
Bind Mounts
- Directly bind a specific path on the host to a path in the container.
- You control exactly where the data lives.
- Great for development (e.g., sync local source code into the container).
- Example:
docker run -v /path/on/host:/path/in/container my_image
tmpfs Mounts
-
Data is stored in memory only (RAM), not on disk.
-
Super fast, but non-persistent (data disappears when the container stops).
-
Useful for sensitive information or temporary, high-speed data that you don’t want to be saved anywhere.
-
Works only on Linux hosts (natively).
-
Example:
```bash
docker run —tmpfs /app/cache my_image
```
(This mounts /app/cache as an in-memory filesystem.)
Summary Table
| Type | Managed by Docker | Custom Path | Use Case |
|---|---|---|---|
| Named Volume | Yes | No | Persistent, portable data |
| Anonymous Volume | Yes | No | Temporary, unmanaged data |
| Bind Mount | No | Yes | Full control, dev workflows |
| tmpfs Mount | No | No | Non-persistent |
Quick Tips
- Use named volumes for production.
- Use bind mounts for local development.
- Be careful with anonymous volumes — they can clutter your system if not cleaned.
- Use tmpfs when you need speed and don’t want data saved.
- Remember: tmpfs = in-memory only — no disk writes!