Docker Mounting: Bind, Named, & Anonymous Volumes
Cara menggunakan bind mount, named volume, dan anonymous volume untuk persistent storage di Docker dengan long syntax pada Docker Compose & CLI.
Persistent storage dalam konteks Docker sangat penting untuk memastikan data tetap ada meskipun container dihentikan atau dihapus.
Apa Itu Anonymous Volume di Docker?
Anonymous volume adalah volume sementara yang dibuat otomatis oleh Docker tanpa nama khusus. Ini terjadi saat kita menggunakan flag -v dengan path di dalam container tanpa menentukan nama volume.
Contoh Anonymous Volume
Misalnya, dalam perintah berikut:
docker run -v /app/node_modules alpine ls /app
- Docker akan membuat volume baru untuk direktori
/app/node_modules. - Volume ini tidak memiliki nama dan hanya dikenali dengan ID acak.
- Data dalam volume ini akan tetap ada meskipun container dihentikan atau dihapus, kecuali volume dibersihkan secara manual.
Untuk melihat anonymous volume yang dibuat:
docker volume ls
Outputnya bisa seperti ini:
DRIVER VOLUME NAME
local 93f9b6b0581e57d97b0f
Volume dengan ID 93f9b6b0581e57d97b0f adalah anonymous volume.
Mengapa Menggunakan Anonymous Volume?
Dalam konteks perintah:
-v /app/node_modules
- Digunakan untuk mencegah penimpaan
/app/node_modulessaat bind-mount. - Jika folder
node_modulesada di host, isinya bisa berbeda dengan yang ada di container. - Solusinya: Docker otomatis membuat anonymous volume agar
node_modulestidak tertimpa oleh bind-mount dari host.
Perbedaan Anonymous Volume vs Named Volume vs Bind Mount
| Tipe | Cara Mendeklarasikan | Persistensi | Lokasi di Host |
|---|---|---|---|
| Anonymous Volume | -v /app/node_modules |
Bertahan sampai semua container yang menggunakannya dihapus | /var/lib/docker/volumes/ |
| Named Volume | -v feedback:/app/feedback |
Bertahan meskipun container dihapus | /var/lib/docker/volumes/feedback/_data |
| Bind Mount | -v $(pwd):/app |
Tidak bertahan (tergantung host) | Lokasi yang kita tentukan (misal /home/user/project) |
Bagaimana Cara Menghapus Anonymous Volume?
Setelah container dihentikan, anonymous volume masih tersimpan. Untuk membersihkannya:
docker volume prune
Perintah ini akan menghapus semua volume yang tidak lagi digunakan oleh container manapun.
Kesimpulan
- Anonymous volume dibuat otomatis tanpa nama saat
-vdigunakan tanpa menyebutkan nama volume. - Fungsinya dalam contoh tadi adalah mencegah penimpaan
/app/node_modulesoleh bind-mount host. - Bisa dihapus menggunakan
docker volume prune. - Berbeda dari named volume dan bind-mount, yang masing-masing punya kegunaan berbeda.
Penjabaran Sintaks Docker Mount (Long Syntax)
1. Bind Mount
Bind mount menghubungkan folder atau file dari host ke dalam container secara langsung.
Contoh di Docker Compose
version: '3.8'
services:
app:
image: alpine:3.18
container_name: feedback-app
command: sh
volumes:
- type: bind
source: ./project
target: /app
read_only: true
Contoh di Docker CLI
docker run -it --rm --name feedback-app \
--mount type=bind,source="$(pwd)",target=/app,readonly \
alpine:3.18 sh
2. Named Volume
Named volume adalah volume yang dikelola oleh Docker dan tidak bergantung pada direktori host.
Contoh di Docker Compose
version: '3.8'
services:
app:
image: alpine:3.18
container_name: feedback-app
command: sh
volumes:
- type: volume
source: feedback
target: /app/feedback
Contoh di Docker CLI
docker run -it --rm --name feedback-app \
--mount type=volume,source=feedback,target=/app/feedback \
alpine:3.18 sh
3. Anonymous Volume
Anonymous volume dibuat tanpa nama dan hanya dikenali oleh Docker.
Contoh di Docker Compose
version: '3.8'
services:
app:
image: alpine:3.18
container_name: feedback-app
command: sh
volumes:
- type: volume
target: /app/node_modules
Contoh di Docker CLI
docker run -it --rm --name feedback-app \
--mount type=volume,target=/app/node_modules \
alpine:3.18 sh
Kesimpulan
| Tipe Mounting | Sintaks `--mount` (CLI) | Docker Compose (Long Syntax) |
|---|---|---|
| Bind Mount | --mount type=bind,source=<host_path>,target=<container_path> |
type: bind, source: <host_path>, target: <container_path> |
| Named Volume | --mount type=volume,source=<volume_name>,target=<container_path> |
type: volume, source: <volume_name>, target: <container_path> |
| Anonymous Volume | --mount type=volume,target=<container_path> |
type: volume, target: <container_path> |
Perbandingan Short vs Long Syntax di Docker
Perbandingan Bind Mount (Short vs. Long Syntax)
| Aspek | Short Syntax | Long Syntax |
|---|---|---|
| Perintah | -v $(pwd):/app |
--mount type=bind,source="$(pwd)",target=/app |
| Tipe Mount | Bind Mount | Bind Mount |
| Sumber | $(pwd) (Direktori di host) |
source="$(pwd)" (Direktori di host) |
| Tujuan di Container | /app |
/app |
| Read-Only? | -v $(pwd):/app:ro |
--mount type=bind,source="$(pwd)",target=/app,readonly |
| Keunggulan | Lebih ringkas dan mudah ditulis | Lebih eksplisit dan fleksibel |
| Kekurangan | Kurang fleksibel | Lebih panjang dan verbose |
Contoh Short Syntax:
docker run -it --rm -v $(pwd):/app alpine sh
Contoh Long Syntax:
docker run -it --rm --mount type=bind,source="$(pwd)",target=/app alpine sh
Perbandingan Named Volume (Short vs. Long Syntax)
| Aspek | Short Syntax | Long Syntax |
|---|---|---|
| Perintah | -v feedback:/app/feedback |
--mount type=volume,source=feedback,target=/app/feedback |
| Tipe Mount | Named Volume | Named Volume |
| Sumber | feedback (Volume Docker) |
source=feedback (Volume Docker) |
| Tujuan di Container | /app/feedback |
/app/feedback |
| Read-Only? | -v feedback:/app/feedback:ro |
--mount type=volume,source=feedback,target=/app/feedback,readonly |
| Keunggulan | Lebih cepat diketik | Lebih eksplisit dan bisa ditambahkan opsi lain |
| Kekurangan | Tidak bisa menyebutkan banyak opsi | Lebih panjang dan verbose |
Contoh Short Syntax:
docker run -it --rm -v feedback:/app/feedback alpine sh
Contoh Long Syntax:
docker run -it --rm --mount type=volume,source=feedback,target=/app/feedback alpine sh
Kesimpulan
| Tipe Mount | Short Syntax | Long Syntax |
|---|---|---|
| Bind Mount | -v <host_path>:<container_path> |
--mount type=bind,source=<host_path>,target=<container_path> |
| Named Volume | -v <volume_name>:<container_path> |
--mount type=volume,source=<volume_name>,target=<container_path> |
| Anonymous Volume | -v <container_path> |
--mount type=volume,target=<container_path> |
| Read-Only | -v <source>:<target>:ro |
--mount ...,readonly |
| Keunggulan | Lebih ringkas | Lebih fleksibel dan eksplisit |
| Kekurangan | Tidak bisa menambahkan banyak opsi | Lebih panjang dan verbose |
Warning!
We are not responsible for any loss whatsoever due to this site, also if you want to take this article please read terms of use or touch us via contact page.
If there is question, please discuss below. Very welcome and expected to provide corrections, criticisms, and suggestions.
Be the first :D