[Thực hành Docker Volume] Use a read-only volume

Ta thực hành như sau:

Để chia sẻ thư mục host và container:

docker run -it -v D:\workspace\docker\share_data:/home/data_container busybox

trong đó:

  1. D:\workspace\docker\share_data là thư mục host local
  2. home/data_container: là thư mục container được mount với host

Dữ liệu giữa 2 thư mục này được map với nhau, nếu container lưu dữ liệu vào thư mục này thì cũng lưu trên host, khi xóa container thì dữ liệu vẫn được lưu trên máy host.

Để chia sẻ dữ liệu giữa 2 container ta có 2 cách:

  1. Là tạo chung một volume, tất nhiên 2 container được chia sẽ chung cùng một volume thì dữ liệu cũng sẽ được chia sẽ với nhau.
  2. Ta sử dụng keywork: -volumes-from

Ví dụ tương ứng với cách 1:

docker run -it --name c1 -v PROJECT_1:/home/disk_01 busybox:latest bash
docker run -it --name c2 -v PROJECT_1:/home/disk_02 busybox:latest sh

Ví dụ tương ứng với cách 2:

docker run -it --name c3 --volumes-from 6c39789f6dd4 busybox:lates

Lệnh trên ta tạo container có tên là c3 từ image busybox và được chia sẻ dữ liệu từ container có Id là 6c39789f6dd4 (container tạo trước đó)

chạy xong lệnh trên thì ta đang đứng trong container c3 vừa tạo, cd vào thư mục home sẽ thấy thư mục disk_02 được chia sẻ

Gán ổ đĩa vào container

docker run -it --name container1 --mount source=D1,target=/home/disk2 ubuntu

Lệnh trên là ta tạo container nên là container1

Tham số –mount là để gán thêm ổ đĩa khi tạo container

source=D1 là tham số ổ đĩa cần gán vào container

target=/home/disk2 với /home/disk2 là thư mục ánh xạ ổ đĩa D1 vào container

Ta thoát và xóa container đó đi thì ổ đĩa vẫn còn

Tạo ổ đĩa ánh xạ lên một foder trên máy host

docker volume create --opt device=D:\workspace\docker\share_data/ --opt type=none --opt o=bind PROJECT_2

Ổ đĩa PROJECT_2 đã được tạo và ánh xạ vơi thư mục trên máy host, ta có thể kiểm tra ổ đĩa bằng lệnh docker volume inspect PROJECT_2

Tạo container kèm gán ổ đĩa ánh xạ trên local

docker run -it --name c4 -v PROJECT_2:/home/disk_test busybox sh

Use a read-only volume

For some development applications, the container needs to write into the bind mount so that changes are propagated back to the Docker host. At other times, the container only needs read access to the data. Multiple containers can mount the same volume. You can simultaneously mount a single volume as read-write for some containers and as read-only for others.

The following example modifies the one above but mounts the directory as a read-only volume, by adding ro to the (empty by default) list of options, after the mount point within the container. Where multiple options are present, you can separate them using commas.

Docs:

docker run -d \
 --name=nginxtest \
 -v nginx-vol:/usr/share/nginx/html:ro \
 nginx:latest

ví dụ:

docker run -it --name c5 -v PROJECT_2:/home/disk_test:ro busybox sh

Ta thử tạo file ở container c5 tất nhiên sẽ báo là:a.txt: Read-only file system :

Nhưng ta có thể read-write ở thư mục host.

 

 

Về GauDev

Tôi là Gau Dev - thích viết lách, tìm hiểu công nghệ mới và không ngừng học hỏi!
Xem tất cả các bài viết của GauDev →