1. 程式人生 > >Docker 容器內新增資料卷的2種方式

Docker 容器內新增資料卷的2種方式

文章目錄

1、容器資料卷是什麼?

  • 容器的持久化
  • 容器間繼承 + 共享資料

卷 就是 目錄或檔案,存在於一個或多個容器中,由docker掛載到容器中,但不屬於聯合檔案系統,因此能夠繞過Union File System提供一些用於持續儲存或共享資料的特性。

卷的設計的目的就是資料的持久化,完全獨立於容器的生存週期,因此Docker不會在容器刪除時刪除其掛載的資料卷。

特點:
1:資料卷可以在容器之間共享或重用資料
2:卷中的更改可以直接生效
3:資料卷中的更改不會包含在映象的更新中
4:資料卷的生命令週期預設會一直存在,即使容器被刪除。

2、容器內 新增資料卷的2種方式

  • 直接命令新增
  • DockerFile 新增

3、直接命令新增

定義3個終端:

  1. 終端host
  2. 終端container
  3. 終端test

3.1、命令

格式:
docker run -it -v /宿主機絕對目錄:/容器內目錄 映象名

示例:

docker run -it -v /myDataVolume:/dataVolumerContainer centos

終端host:

檢視根據目錄

[[email protected] java]# ls -l
total 12
-rw-r--r--.   1 root root 12030 Oct  6 19:15 anaconda-post.log
lrwxrwxrwx.   1 root root     7 Oct  6 19:14 bin -> usr/bin
drwxr-xr-x.   5 root root   360 Nov 25 06:56 dev
drwxr-xr-x.   1 root root    66 Nov 25 06:56 etc
drwxr-xr-x.   2 root root     6 Apr 11  2018 home
lrwxrwxrwx.   1 root root     7 Oct  6 19:14 lib -> usr/lib
lrwxrwxrwx.   1 root root     9 Oct  6 19:14 lib64 -> usr/lib64
drwxr-xr-x.   2 root root     6 Apr 11  2018 media
drwxr-xr-x.   2 root root     6 Apr 11  2018 mnt
drwxr-xr-x.   2 root root     6 Apr 11  2018 opt
dr-xr-xr-x. 274 root root     0 Nov 25 06:56 proc
dr-xr-x---.   2 root root   114 Oct  6 19:15 root
drwxr-xr-x.  10 root root   130 Oct  6 19:15 run
lrwxrwxrwx.   1 root root     8 Oct  6 19:14 sbin -> usr/sbin
drwxr-xr-x.   2 root root     6 Apr 11  2018 srv
dr-xr-xr-x.  13 root root     0 Nov 25 06:17 sys
drwxrwxrwt.   7 root root   132 Oct  6 19:15 tmp
drwxr-xr-x.  13 root root   155 Oct  6 19:14 usr
drwxr-xr-x.  18 root root   238 Oct  6 19:14 var

終端test:
驗證是否有容器執行

[[email protected] java]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

終端container

檢視根據目錄

[[email protected] java]# ls -l
total 12
-rw-r--r--.   1 root root 12030 Oct  6 19:15 anaconda-post.log
lrwxrwxrwx.   1 root root     7 Oct  6 19:14 bin -> usr/bin
drwxr-xr-x.   5 root root   360 Nov 25 06:56 dev
drwxr-xr-x.   1 root root    66 Nov 25 06:56 etc
drwxr-xr-x.   2 root root     6 Apr 11  2018 home
lrwxrwxrwx.   1 root root     7 Oct  6 19:14 lib -> usr/lib
lrwxrwxrwx.   1 root root     9 Oct  6 19:14 lib64 -> usr/lib64
drwxr-xr-x.   2 root root     6 Apr 11  2018 media
drwxr-xr-x.   2 root root     6 Apr 11  2018 mnt
drwxr-xr-x.   2 root root     6 Apr 11  2018 opt
dr-xr-xr-x. 274 root root     0 Nov 25 06:56 proc
dr-xr-x---.   2 root root   114 Oct  6 19:15 root
drwxr-xr-x.  10 root root   130 Oct  6 19:15 run
lrwxrwxrwx.   1 root root     8 Oct  6 19:14 sbin -> usr/sbin
drwxr-xr-x.   2 root root     6 Apr 11  2018 srv
dr-xr-xr-x.  13 root root     0 Nov 25 06:17 sys
drwxrwxrwt.   7 root root   132 Oct  6 19:15 tmp
drwxr-xr-x.  13 root root   155 Oct  6 19:14 usr
drwxr-xr-x.  18 root root   238 Oct  6 19:14 var

通過上述3個終端,驗證了:根目錄下沒有 dataVolumerContainer目錄,並且也沒有容器在執行。

下面的步驟是建立並執行一個帶資料卷的容器,並再次檢視目錄
終端container :

[email protected] java]# docker run -it -v /myDataVolume:/dataVolumerContainer centos
[[email protected] /]# ls -l
total 12
-rw-r--r--.   1 root root 12030 Oct  6 19:15 anaconda-post.log
lrwxrwxrwx.   1 root root     7 Oct  6 19:14 bin -> usr/bin
drwxr-xr-x.   2 root root     6 Nov 25 07:01 dataVolumerContainer
drwxr-xr-x.   5 root root   360 Nov 25 07:01 dev
drwxr-xr-x.   1 root root    66 Nov 25 07:01 etc
drwxr-xr-x.   2 root root     6 Apr 11  2018 home
lrwxrwxrwx.   1 root root     7 Oct  6 19:14 lib -> usr/lib
lrwxrwxrwx.   1 root root     9 Oct  6 19:14 lib64 -> usr/lib64
drwxr-xr-x.   2 root root     6 Apr 11  2018 media
drwxr-xr-x.   2 root root     6 Apr 11  2018 mnt
drwxr-xr-x.   2 root root     6 Apr 11  2018 opt
dr-xr-xr-x. 277 root root     0 Nov 25 07:01 proc
dr-xr-x---.   2 root root   114 Oct  6 19:15 root
drwxr-xr-x.  10 root root   130 Oct  6 19:15 run
lrwxrwxrwx.   1 root root     8 Oct  6 19:14 sbin -> usr/sbin
drwxr-xr-x.   2 root root     6 Apr 11  2018 srv
dr-xr-xr-x.  13 root root     0 Nov 25 06:17 sys
drwxrwxrwt.   7 root root   132 Oct  6 19:15 tmp
drwxr-xr-x.  13 root root   155 Oct  6 19:14 usr
drwxr-xr-x.  18 root root   238 Oct  6 19:14 var
[[email protected] dataVolumerContainer]# ls
[[email protected] dataVolumerContainer]# ll
total 0
[[email protected] dataVolumerContainer]# pwd
/dataVolumerContainer

建立並執行一個端資料卷的容器,並且根目錄中增加了 dataVolumerContainer目錄。

終端host:

[[email protected] /]# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  myDataVolume  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[[email protected] /]# cd myDataVolume/
[[email protected] myDataVolume]# ls

終端test:
驗證是否有容器執行

[[email protected] java]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[[email protected] java]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
68c12f6d555c        centos              "/bin/bash"         6 seconds ago       Up 5 seconds    

3.2、檢視資料卷是否掛載成功

終端test:

[[email protected] java]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
68c12f6d555c        centos              "/bin/bash"         6 seconds ago       Up 5 seconds                            vibrant_mclean
[[email protected] java]# docker inspect 68c12f6d555c
......內容省略......
[[email protected] java]# 

3.3、容器和宿主機之間資料共享

終端host:建立檔案 host.txt

[[email protected] myDataVolume]# touch host.txt
[[email protected] myDataVolume]# ll
總用量 0
-rw-r--r--. 1 root root 0 11月 25 15:19 host.txt

終端container,檢視並編輯 host.txt

[[email protected] dataVolumerContainer]# ll
total 0
-rw-r--r--. 1 root root 0 Nov 25 07:19 host.txt
[[email protected] dataVolumerContainer]# vi host.txt 

host.txt

coontainer add

終端host:

[[email protected] myDataVolume]# cat host.txt 
coontainer add

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

終端container,退出

[[email protected] dataVolumerContainer]# exit
exit
[[email protected] java]# 

終端test,驗證正在執行的容器

[[email protected] java]# 
[[email protected] java]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[[email protected] java]# 

終端host:
編輯新增內容host add

[[email protected] myDataVolume]# ll
總用量 1
-rw-r--r--. 1 root root 15 11月 25 15:20 host.txt
[[email protected] myDataVolume]# vi host.txt 

終端container:
重啟容器

[[email protected] java]# docker ps  -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
68c12f6d555c        centos              "/bin/bash"         About an hour ago   Exited (0) 6 minutes ago                       vibrant_mclean
[[email protected] java]# docker start 68c12f6d555c
68c12f6d555c
[[email protected] java]# 

終端test,驗證正在執行的容器

[[email protected] java]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
68c12f6d555c        centos              "/bin/bash"         About an hour ago   Up 4 seconds                            vibrant_mclean
[[email protected] java]# 

終端container:
重新進入容器中

[[email protected] java]# 
[[email protected] java]# docker attach 68c12f6d555c
[[email protected] /]# ll
total 12
-rw-r--r--.   1 root root 12030 Oct  6 19:15 anaconda-post.log
lrwxrwxrwx.   1 root root     7 Oct  6 19:14 bin -> usr/bin
drwxr-xr-x.   2 root root    43 Nov 25 07:55 dataVolumerContainer
drwxr-xr-x.   5 root root   360 Nov 25 07:59 dev
drwxr-xr-x.   1 root root    66 Nov 25 07:01 etc
drwxr-xr-x.   2 root root     6 Apr 11  2018 home
lrwxrwxrwx.   1 root root     7 Oct  6 19:14 lib -> usr/lib
lrwxrwxrwx.   1 root root     9 Oct  6 19:14 lib64 -> usr/lib64
drwxr-xr-x.   2 root root     6 Apr 11  2018 media
drwxr-xr-x.   2 root root     6 Apr 11  2018 mnt
drwxr-xr-x.   2 root root     6 Apr 11  2018 opt
dr-xr-xr-x. 281 root root     0 Nov 25 07:59 proc
dr-xr-x---.   1 root root    27 Nov 25 07:51 root
drwxr-xr-x.  10 root root   130 Oct  6 19:15 run
lrwxrwxrwx.   1 root root     8 Oct  6 19:14 sbin -> usr/sbin
drwxr-xr-x.   2 root root     6 Apr 11  2018 srv
dr-xr-xr-x.  13 root root     0 Nov 25 06:17 sys
drwxrwxrwt.   7 root root   132 Oct  6 19:15 tmp
drwxr-xr-x.  13 root root   155 Oct  6 19:14 usr
drwxr-xr-x.  18 root root   238 Oct  6 19:14 var
[[email protected] /]# 

進入容器卷,檢視是否有容器啟動前新增的內容。

[[email protected] /]# cd dataVolumerContainer/
[[email protected] dataVolumerContainer]# ls
host.txt
[[email protected] dataVolumerContainer]# cat host.txt 
coontainer add

host add
[[email protected] dataVolumerContainer]# 

host add是容器啟動前新增的內容。

3.4、命令(帶許可權)

格式:
docker run -it -v /宿主機絕對目錄:/容器內目錄:ro 映象名 容器內的檔案只讀,目錄無權建立
ro 表示ready only ,只讀。

示例:

docker run -it -v /myDataVolume:/dataVolumerContainer:ro centos

4、DockerFile 新增

4.1、根目錄下新建 mydocker 目錄並進入

終端container:

[[email protected] /]# mkdir /mydocker
[[email protected] /]# cd mydocker/
[[email protected] mydocker]# pwd
/mydocker
[[email protected] mydocker]#

4.2、在 Dockerfile 中使用 Volume 指令來給映象新增一個或多個數據卷

語法格式:

VOLUME["/dataVolumeContainer1","/dataVolumeContainer2"]

說明:
出於可移值和分享的考慮,用 -v 主機目錄:容器目錄 這種方法不能夠直接在Dockerfile中實現。
由於宿主機目錄是依賴於特定宿主機的,並不能保證在所有宿主機上都存在這樣的特定目錄。

4.3、File構建

終端container:

[[email protected] mydocker]# vi Dockerfile

Dockerfile :

# volume test
FROM centos
VOLUME ["/dataVolumeContainer1","/dataVolumeContainer2"]
CMD echo "finished,------success1"
CMD /bin/bash

說明:
上面的 Dockerfile 的內容 等價於

docker run -it -v /host1:/dataVolumeContainer1 -v /host2:/dataVolumeContainer2 centos /bin/bash

4.4、build後生成映象

格式:

docker build -f /mydocker/Dockerfile -t zzyy/centos .

注意,最後一位是個點(.)

操作內容:

終端container:

[[email protected] mydocker]# vi Dockerfile
[[email protected] mydocker]# ls
Dockerfile
[[email protected] mydocker]# cat Dockerfile 
# volume test
FROM centos
VOLUME ["/dataVolumeContainer1","/dataVolumeContainer2"]
CMD echo "finished,------success1"
CMD /bin/bash
[[email protected] mydocker]# docker build -f /mydocker/Dockerfile -t zzyy/centos .
Sending build context to Docker daemon  2.048kB
Step 1/4 : FROM centos
 ---> 75835a67d134
Step 2/4 : VOLUME ["/dataVolumeContainer1","/dataVolumeContainer2"]
 ---> Running in 041cc7e7d58f
Removing intermediate container 041cc7e7d58f
 ---> b49f99cf2001
Step 3/4 : CMD echo "finished,------success1"
 ---> Running in 2e8ed8aedbff
Removing intermediate container 2e8ed8aedbff
 ---> 8ec90a7d9d43
Step 4/4 : CMD /bin/bash
 ---> Running in fb87df9d2666
Removing intermediate container fb87df9d2666
 ---> 1846c594f7f9
Successfully built 1846c594f7f9
Successfully tagged zzyy/centos:latest
[[email protected] mydocker]# 

檢視映象:

終端container:

[[email protected] mydocker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
zzyy/centos         latest              1846c594f7f9        42 seconds ago      200MB
atguigu/mytomcat    1.2                 b105c1f52eb5        8 hours ago         463MB
redis               latest              c188f257942c        9 days ago          94.9MB
tomcat              latest              ca9e2fccef98        3 weeks ago         463MB
centos              latest              75835a67d134        6 weeks ago         200MB
[[email protected] mydocker]# 

4.5、run容器

終端container:

[[email protected] mydocker]# docker run -it zzyy/centos
[[email protected] /]# pwd
/
[[email protected] /]# 
[[email protected] /]# ll
total 12
-rw-r--r--.   1 root root 12030 Oct  6 19:15 anaconda-post.log
lrwxrwxrwx.   1 root root     7 Oct  6 19:14 bin -> usr/bin
drwxr-xr-x.   2 root root     6 Nov 25 10:31 dataVolumeContainer1
drwxr-xr-x.   2 root root     6 Nov 25 10:31 dataVolumeContainer2
drwxr-xr-x.   5 root root   360 Nov 25 10:31 dev
drwxr-xr-x.   1 root root    66 Nov 25 10:31 etc
drwxr-xr-x.   2 root root     6 Apr 11  2018 home
lrwxrwxrwx.   1 root root     7 Oct  6 19:14 lib -> usr/lib
lrwxrwxrwx.   1 root root     9 Oct  6 19:14 lib64 -> usr/lib64
drwxr-xr-x.   2 root root     6 Apr 11  2018 media
drwxr-xr-x.   2 root root     6 Apr 11  2018 mnt
drwxr-xr-x.   2 root root     6 Apr 11  2018 opt
dr-xr-xr-x. 280 root root     0 Nov 25 10:31 proc
dr-xr-x---.   2 root root   114 Oct  6 19:15 root
drwxr-xr-x.  10 root root   130 Oct  6 19:15 run
lrwxrwxrwx.   1 root root     8 Oct  6 19:14 sbin -> usr/sbin
drwxr-xr-x.   2 root root     6 Apr 11  2018 srv
dr-xr-xr-x.  13 root root     0 Nov 25 06:17 sys
drwxrwxrwt.   7 root root   132 Oct  6 19:15 tmp
drwxr-xr-x.  13 root root   155 Oct  6 19:14 usr
drwxr-xr-x.  18 root root   238 Oct  6 19:14 var
[[email protected] /]# 

建立並執行容器後,發現自動建立了兩個資料卷

4.6、如何找對應的主機目錄地址

方法:docker inspect 容器id

終端test
查詢宿主機的目錄地址

[[email protected] /]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
299d9be66cf2        zzyy/centos         "/bin/sh -c /bin/bash"   3 minutes ago       Up 3 minutes                            gifted_fermat
[[email protected] /]# docker inspect 299d9be66cf2
....省略....
        "Mounts": [
            {
                "Type": "volume",
                "Name": "ebecb97f356f449783bd9898ebd2c6b31559fb58fff5981350dc7fd4fe1d0941",
                "Source": "/var/lib/docker/volumes/ebecb97f356f449783bd9898ebd2c6b31559fb58fff5981350dc7fd4fe1d0941/_data",
                "Destination": "/dataVolumeContainer1",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            },
            {
                "Type": "volume",
                "Name": "d668bc0dfbc64f722fae55f4986c3417eb88850ab62e2e50aef9b50f31bad37c",
                "Source": "/var/lib/docker/volumes/d668bc0dfbc64f722fae55f4986c3417eb88850ab62e2e50aef9b50f31bad37c/_data",
                "Destination": "/dataVolumeContainer2",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
....省略....
[[email protected] /]# 

在節點Mounts節點中已經給出了宿主機的目錄地址
/var/lib/docker/volumes/ebecb97f356f449783bd9898ebd2c6b31559fb58fff5981350dc7fd4fe1d0941/_data 對應的是 /dataVolumeContainer1
/var/lib/docker/volumes/d668bc0dfbc64f722fae55f4986c3417eb88850ab62e2e50aef9b50f31bad37c/_data 對應的是 /dataVolumeContainer2

4.7、主機對應預設地址

終端container:
進入 dataVolumeContainer1 中,編輯 container.txt

[[email protected] /]# ls
anaconda-post.log  dataVolumeContainer1  dev  home  lib64  mnt  proc  run   srv  tmp  var
bin                dataVolumeContainer2  etc  lib   media  opt  root  sbin  sys  usr
[[email protected] /]# cd dataVolumeContainer1/
[[email protected] dataVolumeContainer1]# ls
[[email protected] dataVolumeContainer1]# echo "container add"> container.txt
[[email protected] dataVolumeContainer1]# cat container.txt 
container add
[[email protected] dataVolumeContainer1]#

終端host:
進入/var/lib/docker/volumes/ebecb97f356f449783bd9898ebd2c6b31559fb58fff5981350dc7fd4fe1d0941/_data 目錄中:

[[email protected] /]# cd /var/lib/docker/volumes/ebecb97f356f449783bd9898ebd2c6b31559fb58fff5981350dc7fd4fe1d0941/_data
[[email protected] _data]# ls
container.txt
[[email protected] _data]# cat container.txt 
container add
[[email protected] _data]#

5、可能遇到的問題

1、如果Docker掛載主機目錄時,Docker訪問出現catnot open directory.:Permission denied

解決辦法:在掛載目錄後多加一個--privileged=true引數。如

docker run -it -v /myDataVolume:/dataVolumerContainer --privileged=true centos

2、Firefox 取消了該請求

FirFox瀏覽器時,彈出以下提示:
“此地址使用了一個通常用於網路瀏覽以外的埠。出於安全原因,Firefox 取消了該請求。”。

解決方法:

在Firefox位址列輸入about:config ,
然後在右鍵新建一個字串鍵 network.security.ports.banned.override ,
將需訪問網站的埠號新增到,值就是那個埠號即可,如7080
如有多個,就半形逗號隔開,例:7080,6666,8888

在能保證安全的前提下,還簡化成這樣寫0-65535 ,這樣可以瀏覽任意埠的網站了。