首頁 > 軟體

使用Dockerfile實現資料卷的掛載問題(推薦)

2022-03-09 13:00:05

大家好,我們今天分享使用Dockerfile來實現資料卷的掛載

首先呢,要了解一下什麼是Dockerfile

Dockerfile就是 一個可以用來構建映象的檔案,可以理解為一個指令碼檔案

可以通過這個指令碼檔案自定義生成一個我們想要的映象,映象都是一層一層的,裡面的每一條命令就是一層

建立一個目錄

root@localhost home]# mkdir  docker-vl2

編輯這樣這樣的一個檔案

root@localhost docker-vl2]# vim dockerfile01
FROM centos
VOLUME ["volume01","volume02"]

CMD echo "------end-----"
CMD /bin/bash
~                                                                                                           
~                            
~                                                                                                           
~       

儲存退出:

構建成新的映象,注意看shuaige/centos:2.0有一個小點

[root@localhost docker-vl2]# docker build -f dockerfile01 -t shuaige/centos:2.0 .
Sending build context to Docker daemon  2.048kB
Step 1/4 : FROM centos
 ---> 5d0da3dc9764
Step 2/4 : VOLUME ["volume01","volume02"]
 ---> Running in 96445950bbef
Removing intermediate container 96445950bbef
 ---> ffcf0eb8973b
Step 3/4 : CMD echo "------end-----"
 ---> Running in a4fa6fce633b
Removing intermediate container a4fa6fce633b
 ---> 5416582373e0
Step 4/4 : CMD /bin/bash
 ---> Running in 7d7df79e0b36
Removing intermediate container 7d7df79e0b36
 ---> d8e0f44b8da3
Successfully built d8e0f44b8da3
Successfully tagged shuaige/centos:2.0

看這個過程,一共有4步,對應著我們剛剛寫的dockerfile 檔案

也是一層一層的

檢視映象:

這樣,我們的這個dockerfile 檔案就構建成功了

進入我們自己寫的容器

[root@localhost docker-vl2]# docker run -it d8e0f44b8da3   /bin/bash
[root@b36bc0c1e4a9 /]# 

檢視容器內部的檔案結構:

[root@b36bc0c1e4a9 /]# ls -l
total 0
lrwxrwxrwx.   1 root root   7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x.   5 root root 360 Mar  8 14:09 dev
drwxr-xr-x.   1 root root  66 Mar  8 14:09 etc
drwxr-xr-x.   2 root root   6 Nov  3  2020 home
lrwxrwxrwx.   1 root root   7 Nov  3  2020 lib -> usr/lib
lrwxrwxrwx.   1 root root   9 Nov  3  2020 lib64 -> usr/lib64
drwx------.   2 root root   6 Sep 15 14:17 lost+found
drwxr-xr-x.   2 root root   6 Nov  3  2020 media
drwxr-xr-x.   2 root root   6 Nov  3  2020 mnt
drwxr-xr-x.   2 root root   6 Nov  3  2020 opt
dr-xr-xr-x. 265 root root   0 Mar  8 14:09 proc
dr-xr-x---.   2 root root 162 Sep 15 14:17 root
drwxr-xr-x.  11 root root 163 Sep 15 14:17 run
lrwxrwxrwx.   1 root root   8 Nov  3  2020 sbin -> usr/sbin
drwxr-xr-x.   2 root root   6 Nov  3  2020 srv
dr-xr-xr-x.  13 root root   0 Mar  4 07:14 sys
drwxrwxrwt.   7 root root 171 Sep 15 14:17 tmp
drwxr-xr-x.  12 root root 144 Sep 15 14:17 usr
drwxr-xr-x.  20 root root 262 Sep 15 14:17 var
drwxr-xr-x.   2 root root   6 Mar  8 14:09 volume01
drwxr-xr-x.   2 root root   6 Mar  8 14:09 volume02

可以看見這倆行:(這就是我們剛才使用dockerfile 檔案做的資料卷掛載)

drwxr-xr-x.   2 root root   6 Mar  8 14:09 volume01
drwxr-xr-x.   2 root root   6 Mar  8 14:09 volume02

看dockerfile 檔案的內容:

FROM centos
VOLUME ["volume01","volume02"]

CMD echo "------end-----"
CMD /bin/bash
~                                    

可以看出,它是匿名掛載:

但凡是掛載,就是一定與外部有一個對應的目錄

檢視正在執行的容器

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED             STATUS             PORTS                                     NAMES
b36bc0c1e4a9   d8e0f44b8da3   "/bin/bash"              14 minutes ago      Up 14 minutes                                                compassionate_brown
6590da464b6e   nginx          "/docker-entrypoint.…"   About an hour ago   Up About an hour   0.0.0.0:49154->80/tcp, :::49154->80/tcp   nginx03
d8e9b9084cf8   nginx          "/docker-entrypoint.…"   2 hours ago         Up 2 hours         0.0.0.0:49153->80/tcp, :::49153->80/tcp   nginx02
6d6fa6a4c6d7   5d0da3dc9764   "/bin/bash"              33 hours ago        Up 33 hours                                                  nice_panini

看容器的系統資訊

[root@localhost ~]# docker inspect b36bc0c1e4a9  

這就是我們剛才資料卷掛載的位置

 "Mounts": [
            {
                "Type": "volume",
                "Name": "155842f6ab8581ad5d04fa25d6a1979647f481aad9532306834e577f88daa2a1",
                "Source": "/var/lib/docker/volumes/155842f6ab8581ad5d04fa25d6a1979647f481aad9532306834e577f88daa2a1/_data",
                "Destination": "volume01",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            },
            {
                "Type": "volume",
                "Name": "1c6664c1368e4a11d36fd0b629c5e7b7d40d1d2f88322f9188383e92e52aa2f1",
                "Source": "/var/lib/docker/volumes/1c6664c1368e4a11d36fd0b629c5e7b7d40d1d2f88322f9188383e92e52aa2f1/_data",
                "Destination": "volume02",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],

在容器內部新建一個檔案

[root@b36bc0c1e4a9 /]# cd volume01
[root@b36bc0c1e4a9 volume01]# touch hsud.txt
[root@b36bc0c1e4a9 volume01]# 

看系統資訊:

[root@localhost _data]# docker inspect b36bc0c1e4a9  

在Linux主機上看容器中看

[root@localhost ~]# cd /var/lib/docker/volumes/155842f6ab8581ad5d04fa25d6a1979647f481aad9532306834e577f88daa2a1/_data
[root@localhost _data]# ls
hsud.txt
[root@localhost _data]# 

好了,這樣我們就做好了,有關於使用Dockerfile實現資料卷的掛載就到這裡了,謝謝大家了

到此這篇關於使用Dockerfile實現資料卷的掛載的文章就介紹到這了,更多相關Dockerfile資料卷掛載內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com