This page needs JavaScript activated to work correctly !

This page will be redirect in 3 second !

Docker Mounting: Bind, Named, & Anonymous Volumes - Programming | IDRaya.com

Docker Mounting: Bind, Named, & Anonymous Volumes

Triawan PROGRAMMING 31/01/2025 0 Discuss 388 Views

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_modules saat bind-mount.
  • Jika folder node_modules ada di host, isinya bisa berbeda dengan yang ada di container.
  • Solusinya: Docker otomatis membuat anonymous volume agar node_modules tidak 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 -v digunakan tanpa menyebutkan nama volume.
  • Fungsinya dalam contoh tadi adalah mencegah penimpaan /app/node_modules oleh 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

Berikut adalah perbandingan short syntax dan long syntax untuk bind mount dan volume dalam Docker CLI.

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
Jika ingin cepat dan mudah, gunakan short syntax. Jika butuh konfigurasi lebih kompleks, gunakan long syntax.

Agus Triawan/Triawan

 matriawan@gmail.com

Triawan is just an ordinary person, founder idraya[dot]com who just a little bit knows also likes try and error about devices, networks and programming/applications to solve challenges related to information technology.

If there is question, please discuss below. Very welcome and expected to provide corrections, criticisms, and suggestions.


We'll not share/display your email.
Example: Say <b>Hello</b> &lt;?php echo 'World'; ?&gt;
Output: Say Hello <?php echo 'World'; ?>
Words can come true for you, so be wise in speaking.

Be the first :D