Volume Lifecycle #
Dalam arsitektur container, data memiliki lifecycle yang berbeda dengan container. Container bersifat ephemeral (mudah dibuat dan dihancurkan), sedangkan data sering kali perlu bertahan lama, bahkan ketika container mati, di-recreate, atau di-scale.
Di sinilah Docker Volume berperan. Namun, yang sering luput dibahas adalah bagaimana lifecycle sebuah volume bekerja, kapan ia dibuat, digunakan, dipertahankan, dan akhirnya dihapus.
Agar lebih mudah dipahami, perhatikan diagram lifecycle berikut:
+------------------+
| docker volume |
| create |
+---------+--------+
|
v
+------------------+ +------------------+
| Volume Exists |<----->| Container Run |
| (Idle/Active) | | (Mount Volume) |
+---------+--------+ +---------+--------+
| |
| v
| +------------------+
| | Container Stop |
| | / Remove |
| +---------+--------+
| |
v v
+------------------+ +------------------+
| Volume Still |<------+ Volume Unused |
| Persists | | (Dangling) |
+---------+--------+ +---------+--------+
|
v
+------------------+
| docker volume |
| rm / prune |
+------------------+
Diagram ini menunjukkan satu prinsip penting:
Menghapus container TIDAK menghapus volume secara otomatis.
Apa Itu Docker Volume? #
Docker Volume adalah mekanisme penyimpanan data yang:
- Dikelola langsung oleh Docker
- Disimpan di luar filesystem container
- Dapat digunakan lintas container
- Tidak ikut terhapus saat container dihapus
Volume berbeda dengan:
- Container filesystem → hilang saat container dihapus
- Bind mount → langsung bergantung ke filesystem host
Tahapan Lifecycle Volume di Docker #
Volume Creation #
Volume bisa dibuat dengan dua cara:
a. Explicit (Manual) #
docker volume create mydata
Ciri-ciri:
- Volume berdiri sendiri
- Tidak terikat langsung ke container tertentu
- Aman untuk data jangka panjang
b. Implicit (Otomatis oleh Docker) #
docker run -v mydata:/app/data nginx
Jika mydata belum ada:
- Docker akan otomatis membuat volume
Volume Mounting ke Container #
Saat container dijalankan:
docker run -v mydata:/var/lib/mysql mysql
Yang terjadi:
- Volume
mydatadi-mount ke path container - Container membaca/menulis data ke volume
- Data tidak disimpan di layer container
🔑 Container hanyalah consumer, bukan owner data
Volume in Active Use #
Selama container berjalan:
Volume berada dalam status active
Bisa di-mount ke:
- Satu container
- Banyak container (shared volume)
Contoh:
docker run -v shared:/logs app1
docker run -v shared:/logs app2
Cocok untuk logging, sharing asset, atau IPC sederhana
Container Stop atau Remove #
docker stop mycontainer
docker rm mycontainer
Yang terjadi:
- Container hilang
- Volume tetap ada
- Data aman dan tidak terpengaruh
Ini adalah inti lifecycle volume:
Volume tidak mengikuti lifecycle container
Volume Menjadi Unused (Dangling) #
Jika:
- Semua container yang menggunakan volume sudah dihapus
Maka volume:
- Tetap ada di host
- Tidak terpakai
- Disebut dangling volume
Cek volume:
docker volume ls
Detail volume:
docker volume inspect mydata
Volume Removal #
Volume hanya bisa dihapus jika tidak digunakan oleh container mana pun.
docker volume rm mydata
Atau bersihkan semua volume tidak terpakai:
docker volume prune
⚠️ Peringatan:
pruneakan menghapus SEMUA volume yang tidak terpakai- Data akan hilang permanen
Lifecycle Volume di Docker Compose #
Dalam Docker Compose:
services:
db:
image: mysql
volumes:
- dbdata:/var/lib/mysql
volumes:
dbdata:
Lifecycle-nya:
docker compose up→ volume dibuat (jika belum ada)docker compose down→ container hilang- Volume tetap ada kecuali:
docker compose down -v
Perbandingan: Container vs Volume #
| Aspek | Container | Volume |
|---|---|---|
| Default lifecycle | Pendek | Panjang |
| Hilang saat rm | Ya | Tidak |
| Cocok untuk data | ❌ | ✅ |
| Bisa dishare | Terbatas | Ya |
| Managed Docker | Ya | Ya |
Best Practice #
Gunakan volume untuk semua data stateful
Jangan bergantung pada container filesystem
Beri nama volume yang jelas
app-db-data app-upload-dataAudit volume secara berkala
docker volume ls docker volume pruneBackup volume untuk data penting
Gunakan driver volume (NFS, EBS, dll) untuk production
Kesimpulan #
Docker Volume memiliki lifecycle yang independen dari container. Inilah yang membuat Docker cocok untuk:
- Database
- File upload
- Persistent cache
- Shared state antar container
Memahami lifecycle volume akan membantu Anda:
- Menghindari kehilangan data
- Mendesain arsitektur container yang aman
- Mengelola storage Docker secara lebih profesional
📌 Intinya: container boleh mati, tapi data harus tetap hidup.