『現學現忘』Docker基礎 — 29、Docker容器資料卷的應用

繁華似錦Fighting發表於2022-03-22

1、驗證容器和宿主機之間資料共享

通過上面的操作,接下來我們演示一下通過資料卷的掛載,實現容器和宿主機之間的資料共享。

步驟1:在宿主機中的HostDataVolume目錄中建立host.txt檔案。

# 檢視宿主機當前位置
[root@192 HostDataVolume]# pwd
/home/HostDataVolume

# HostDataVolume目錄中建立host.txt檔案
[root@192 HostDataVolume]# touch host.txt

# 檢視建立的檔案
[root@192 HostDataVolume]# ls -l
總用量 0
-rw-r--r--. 1 root root 0 3月  18 20:18 host.txt

步驟2:在CentOS容器的ContainerDataVolume目錄檢視內容。

# 進入ContainerDataVolume目錄
[root@5f63a0fc88c5 /]# cd /home/ContainerDataVolume/
[root@5f63a0fc88c5 ContainerDataVolume]# pwd
/home/ContainerDataVolume

# 檢視ContainerDataVolume目錄檢視內容
[root@5f63a0fc88c5 ContainerDataVolume]# ls -l
total 0
-rw-r--r--. 1 root root 0 Mar 18 12:18 host.txt

我們可以看到,宿主機HostDataVolume目錄中建立的host.txt檔案,同步到了CentOS容器的ContainerDataVolume目錄中了。

步驟3:在CentOS容器的ContainerDataVolume目錄中建立container.txt檔案。

# 檢視容器中的當前位置
[root@5f63a0fc88c5 ContainerDataVolume]# pwd
/home/ContainerDataVolume

# 建立container.txt檔案
[root@5f63a0fc88c5 ContainerDataVolume]# touch container.txt

# 在host.txt檔案中新增hello docker,說明資料卷中可以讀寫檔案。
[root@0115c0470f8d ContainerDataVolume]# vi host.txt 
[root@0115c0470f8d ContainerDataVolume]# cat host.txt 
hello docker

# 檢視ContainerDataVolume目錄的內容
[root@5f63a0fc88c5 ContainerDataVolume]# ls -l
total 0
-rw-r--r--. 1 root root 0 Mar 18 12:23 container.txt
-rw-r--r--. 1 root root 0 Mar 18 12:18 host.txt

步驟4:在宿主機的HostDataVolume目錄中檢視內容。

# 檢視宿主機的位置
[root@192 HostDataVolume]# pwd
/home/HostDataVolume

# 檢視HostDataVolume目錄中的內容
[root@192 HostDataVolume]# ll
總用量 0
-rw-r--r--. 1 root root 0 3月  18 20:23 container.txt
-rw-r--r--. 1 root root 0 3月  18 20:18 host.txt

# 檢視host.txt檔案中是否有內容。
[root@192 HostDataVolume]# cat host.txt 
hello docker

我們可以看到,在CentOS容器的ContainerDataVolume目錄中建立container.txt檔案,同步到了宿主機的HostDataVolume目錄中。

這就驗證了,資料卷掛載實現了容器和宿主機之間資料共享。

2、容器停止退出後,主機修改後資料是否同步

接上面練習,繼續驗證容器停止退出後,主機修改資料卷中的資料後,重啟容器是否同步。

步驟1:停止CentOS容器。

# 退出centos容器
[root@5f63a0fc88c5 ContainerDataVolume]# exit
exit

# 當前docker中沒有執行的容器
[root@192 ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

步驟2:在宿主機的/hmoe/HostDataVolume目錄中建立檔案和修改檔案。

# 檢視宿主機當前位置
[root@192 HostDataVolume]# pwd
/home/HostDataVolume

# 建立檔案host-02.txt,
[root@192 HostDataVolume]# touch host-02.txt
[root@192 HostDataVolume]# ll
總用量 4
-rw-r--r--. 1 root root  0 3月  18 20:23 container.txt
-rw-r--r--. 1 root root  0 3月  18 21:07 host-02.txt
-rw-r--r--. 1 root root 13 3月  18 21:03 host.txt

# 修改container.txt檔案,在檔案中新增hello world
[root@192 HostDataVolume]# vim container.txt 
[root@192 HostDataVolume]# cat container.txt 
hello world

步驟3:重新啟動CentOS容器,並檢視ContainerDataVolume目錄中的內容。

# 檢視centos容器,是Exited狀態
[root@192 ~]# docker ps -l
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                    PORTS
0115c0470f8d   centos    "/bin/bash"   10 minutes ago   Exited (0) 6 minutes ago

# 啟動centos容器
[root@192 ~]# docker start 0115c0470f8d
0115c0470f8d

# 進入centos容器
[root@192 ~]# docker attach 0115c0470f8d
[root@0115c0470f8d /]# 

# 進入ContainerDataVolume,檢視內容
[root@0115c0470f8d /]# cd /home/ContainerDataVolume/

# 檢視是否有host-02.txt
[root@0115c0470f8d ContainerDataVolume]# ls -l
total 8
-rw-r--r--. 1 root root 12 Mar 18 13:08 container.txt
-rw-r--r--. 1 root root  0 Mar 18 13:07 host-02.txt
-rw-r--r--. 1 root root 13 Mar 18 13:03 host.txt

# 檢視container.txt檔案中的內容
[root@0115c0470f8d ContainerDataVolume]# cat container.txt
hello world

我們可以看到,容器停止退出後,主機修改掛載的資料卷中的資料後,重啟容器依然同步。

所以我們以後修改一些相關的配置檔案,掛載了資料卷之後,只需要在本地修改即可,容器內會自動同步。

3、帶只讀許可權的掛載資料卷

命令:docker run -it -v /宿主機絕對路徑目錄:/容器內目錄:ro 映象名或映象ID

說明roread only 只讀。

還是複用之前的練習之上。

步驟1:啟動CentOS容器,加入帶ro許可權的掛載。

# 啟動centos容器
[root@192 ~]# docker run -it -v /home/HostDataVolume:/home/ContainerDataVolume:ro centos /bin/bash

# 檢視ContainerDataVolume資料夾中的內容
[root@2c8d185e17b1 /]# ls /home/ContainerDataVolume/
container.txt  host-02.txt  host.txt
# 說明:
# 因為啟動容器之前ContainerDataVolume資料夾和裡邊的檔案就存在
# 在使用ContainerDataVolume目錄做資料卷掛載,不會刪除已經存在的檔案。
# 如果需要ContainerDataVolume目錄中的檔案,
# 可以在上面啟動centos容器之前,把宿主機/home/HostDataVolume中的目錄檔案全部刪除。
# 這樣在啟動容器之後,centos容器的ContainerDataVolume目錄會自動同步,也就沒有檔案了。

步驟2:在宿主機的/home/HostDataVolume目錄中,建立host-03.txt檔案。

# 檢視宿主機的當前位置
[root@192 HostDataVolume]# pwd
/home/HostDataVolume

# 建立host-03.txt檔案
[root@192 HostDataVolume]# touch host-03.txt

# 檢視是否建立成功host-03.txt檔案
[root@192 HostDataVolume]# ll
總用量 8
-rw-r--r--. 1 root root 12 3月  18 21:08 container.txt
-rw-r--r--. 1 root root  0 3月  18 21:07 host-02.txt
-rw-r--r--. 1 root root  0 3月  18 22:27 host-03.txt
-rw-r--r--. 1 root root 13 3月  18 21:03 host.txt

# 在host-03.txt新增內容
[root@192 HostDataVolume]# vim host-03.txt 
[root@192 HostDataVolume]# cat host-03.txt 
hello world,hello docker.

步驟3:在CentOS容器中檢視/home/ContainerDataVolume目錄中的內容。

# 檢視ContainerDataVolume目錄中的內容
[root@2c8d185e17b1 /]# ls -l /home/ContainerDataVolume/
total 8
-rw-r--r--. 1 root root 12 Mar 18 13:08 container.txt
-rw-r--r--. 1 root root  0 Mar 18 13:07 host-02.txt
-rw-r--r--. 1 root root  0 Mar 18 14:27 host-03.txt
-rw-r--r--. 1 root root 13 Mar 18 13:03 host.txt

# 檢視host-03.txt檔案中的內容
[root@2c8d185e17b1 /]# cat /home/ContainerDataVolume/host-03.txt 
hello world,hello docker.

我們可以看到,宿主機上的建立的host-03.txt檔案,同步到CentOS容器中了。

同時我們也可以檢視到host-03.txt檔案中的內容。

步驟4:在CentOS容器的/home/ContainerDataVolume目錄中,建立檔案和編寫內容。

# 進入ContainerDataVolume目錄
[root@2c8d185e17b1 /]# cd /home/ContainerDataVolume/
[root@2c8d185e17b1 ContainerDataVolume]# pwd
/home/ContainerDataVolume

# 建立container-02.txt檔案
[root@2c8d185e17b1 ContainerDataVolume]# touch container-02.txt
touch: cannot touch 'container-02.txt': Read-only file system
# 我們可以看到提示,不能建立container-02.txt檔案,因為是隻讀檔案系統。

# 修改container.txt檔案中的內容
[root@2c8d185e17b1 ContainerDataVolume]# cat container.txt 
hello world
[root@2c8d185e17b1 ContainerDataVolume]# vi container.txt 
# 在我們編輯完成儲存時,,出現如下圖的提示:

image

意思是:設定了“只讀”選項。對於寫操作不支援。

步驟5:使用docker inspect命令檢視當前CentOS容器。

# 檢視當前執行的centos容器
[root@192 HostDataVolume]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS         PORTS   NAMES
2c8d185e17b1   centos    "/bin/bash"   25 minutes ago   Up 25 minutes

# 檢視centos容器的具體資料
[root@192 HostDataVolume]# docker inspect 2c8d185e17b1
[
    {
        "Id": "2c8d185e17b113326ea298f34896b6469e7717fa7450eeec5138767a5bb6915f",
        "Created": "2021-03-18T14:20:33.842841276Z",
        ... # 省略

        "HostConfig": { # 主機配置
            "Binds": [
                "/home/HostDataVolume:/home/ContainerDataVolume:ro"
            ], # 可以看到主機繫結的掛載上顯示ro

        ... # 省略
        },
        "Mounts": [  # 容器掛載配置
            {
                "Type": "bind",
                "Source": "/home/HostDataVolume",
                "Destination": "/home/ContainerDataVolume",
                "Mode": "ro", # 這裡標識的只讀模式
                "RW": false,     # 這裡的讀寫許可權,標識為false,
                "Propagation": "rprivate"
            }  # "RW" 之前版本的表示為 "VolumesRW"引數
        ],
        "Config": {
            ... # 省略

            # 新版的Dodker把Volumes資訊放到了Mounts的Json字待串裡了
            "Volumes": null,

            ... # 省略
        },

        ... # 省略
    }
]

總結:

通過上面的演示:資料卷掛載設定ro(只讀)許可權,只允許宿主機單向的寫操作,來同步到容器中。而容器中是無法在資料卷目錄中做任何寫操作,如建立檔案或者修改檔案內容等。

我感覺是:一個容器在啟動時,會自動同步宿主機上掛載目錄中的內容。(這點要記住)

相關文章