zoukankan      html  css  js  c++  java
  • Docker卷

    要解决的问题:在移除了现有容器或者添加了新容器时,之前容器的数据无法访问。

    为避免上述问题,Docker提供了以下策略来持久化数据

    1.tmpfs挂载

    2.绑定挂载

    将Docker宿主机上的文件/目录挂载到容器中(就是说将host文件系统中的数据借给容器用)。注意:数据流在Docker宿主机和容器之间是双向流动的,任何破坏性的操作(比如删除一个目录)也都会对宿主机产生负面影响

    host源(又称mount源)----------container目标(又称mount point)

    目录:如果container目录已存在,则源数据会被隐藏并被替换为host源中的数据

    文件:向容器中添加文件,同时也保留了容器原有的数据

    # 将宿主机的主目录挂载到容器中一个名为host-name的目录
    [root@localhost chrome]# docker run -it --name mount-test -v $HOME:/host-name ubuntu bash root@e7b70f5681cf:/# ll total 8 drwxr-xr-x. 1 root root 23 Sep 22 08:39 ./ drwxr-xr-x. 1 root root 23 Sep 22 08:39 ../ -rwxr-xr-x. 1 root root 0 Sep 22 08:39 .dockerenv* drwxr-xr-x. 2 root root 4096 Sep 12 20:54 bin/ drwxr-xr-x. 2 root root 6 Apr 24 2018 boot/ drwxr-xr-x. 5 root root 360 Sep 22 08:39 dev/ drwxr-xr-x. 1 root root 66 Sep 22 08:39 etc/ drwxr-xr-x. 2 root root 6 Apr 24 2018 home/ dr-xr-x---. 19 root root 4096 Sep 19 10:39 host-name/ drwxr-xr-x. 8 root root 96 May 23 2017 lib/ drwxr-xr-x. 2 root root 34 Sep 12 20:53 lib64/ drwxr-xr-x. 2 root root 6 Sep 12 20:53 media/ drwxr-xr-x. 2 root root 6 Sep 12 20:53 mnt/ drwxr-xr-x. 2 root root 6 Sep 12 20:53 opt/ dr-xr-xr-x. 186 root root 0 Sep 22 08:39 proc/ drwx------. 2 root root 37 Sep 12 20:54 root/ drwxr-xr-x. 1 root root 21 Sep 18 23:20 run/ drwxr-xr-x. 1 root root 21 Sep 18 23:20 sbin/ drwxr-xr-x. 2 root root 6 Sep 12 20:53 srv/ dr-xr-xr-x. 13 root root 0 Sep 19 08:21 sys/ drwxrwxrwt. 2 root root 6 Sep 12 20:54 tmp/ drwxr-xr-x. 1 root root 18 Sep 12 20:53 usr/ drwxr-xr-x. 1 root root 17 Sep 12 20:54 var/ root@e7b70f5681cf:/# pwd / root@e7b70f5681cf:/# cd host-name/ root@e7b70f5681cf:/host-name# touch a.txt # 进入容器内再host-name目录下创建文件a.txt root@e7b70f5681cf:/host-name# ll total 1348 dr-xr-x---. 19 root root 4096 Sep 22 08:40 ./ drwxr-xr-x. 1 root root 23 Sep 22 08:39 ../ -rw-------. 1 root root 2481 Sep 19 07:56 .bash_history -rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout -rw-r--r--. 1 root root 176 Dec 29 2013 .bash_profile -rw-r--r--. 1 root root 176 Dec 29 2013 .bashrc drwx------. 7 root root 76 Sep 19 06:32 .cache/ drwxr-xr-x. 9 root root 175 Sep 19 08:23 .config/ -rw-r--r--. 1 root root 100 Dec 29 2013 .cshrc drwx------. 3 root root 25 Sep 18 17:33 .dbus/ drwx------. 2 root root 26 Sep 19 05:47 .docker/ -rw-------. 1 root root 16 Sep 19 03:42 .esd_auth drwx------. 2 root root 79 Sep 22 07:48 .gnupg/ drwxr-xr-x. 2 root root 33 Sep 19 05:49 .gstreamer-0.10/ -rw-r--r--. 1 root root 113 Mar 8 2011 .gtkrc-2.0-kde4 drwxr-xr-x. 4 root root 140 Sep 19 03:42 .kde/ drwxr-xr-x. 3 root root 19 Sep 19 03:41 .local/ drwxr-----. 3 root root 19 Sep 19 04:11 .pki/ -rw-r--r--. 1 root root 129 Dec 29 2013 .tcshrc -rw-------. 1 root root 688 Sep 19 10:39 .viminfo drwxr-xr-x. 2 root root 6 Sep 19 03:42 Desktop/ drwxr-xr-x. 2 root root 6 Sep 19 03:42 Documents/ drwxr-xr-x. 2 root root 6 Sep 19 03:42 Downloads/ drwxr-xr-x. 2 root root 6 Sep 19 03:42 Music/ drwxr-xr-x. 2 root root 6 Sep 19 03:42 Pictures/ drwxr-xr-x. 2 root root 6 Sep 19 03:42 Public/ drwxr-xr-x. 2 root root 6 Sep 19 03:42 Templates/ drwxr-xr-x. 2 root root 6 Sep 19 03:42 Videos/ -rw-r--r--. 1 root root 0 Sep 22 08:40 a.txt -rw-------. 1 root root 1599 Sep 18 17:33 anaconda-ks.cfg -rw-r--r--. 1 root root 1329209 Sep 19 05:37 ch.exe.part -rw-r--r--. 1 root root 1793 Sep 18 09:37 initial-setup-ks.cfg # 再Docker宿主机打开一个新的终端窗口并输入以下命令,可发现宿主机上存在a.txt [root@localhost chrome]# cd ~ [root@localhost ~]# ll total 1308 -rw-------. 1 root root 1599 Sep 18 13:33 anaconda-ks.cfg -rw-r--r--. 1 root root 0 Sep 22 04:40 a.txt -rw-r--r--. 1 root root 1329209 Sep 19 01:37 ch.exe.part drwxr-xr-x. 2 root root 6 Sep 18 23:42 Desktop drwxr-xr-x. 2 root root 6 Sep 18 23:42 Documents drwxr-xr-x. 2 root root 6 Sep 18 23:42 Downloads -rw-r--r--. 1 root root 1793 Sep 18 05:37 initial-setup-ks.cfg drwxr-xr-x. 2 root root 6 Sep 18 23:42 Music drwxr-xr-x. 2 root root 6 Sep 18 23:42 Pictures drwxr-xr-x. 2 root root 6 Sep 18 23:42 Public drwxr-xr-x. 2 root root 6 Sep 18 23:42 Templates drwxr-xr-x. 2 root root 6 Sep 18 23:42 Videos
    # 再Docker宿主机中终端输入以下命令,以停止这个容器
    [root@localhost ~]# docker stop mount-test 
    mount-test
    [root@localhost ~]# docker rm mount-test 
    mount-test
    
    # 确认Docker宿主机上a.txt文件依旧存在
    
    [root@localhost ~]# cd ~
    [root@localhost ~]# ll
    total 1308
    -rw-------. 1 root root    1599 Sep 18 13:33 anaconda-ks.cfg
    -rw-r--r--. 1 root root       0 Sep 22 04:40 a.txt
    -rw-r--r--. 1 root root 1329209 Sep 19 01:37 ch.exe.part
    drwxr-xr-x. 2 root root       6 Sep 18 23:42 Desktop
    drwxr-xr-x. 2 root root       6 Sep 18 23:42 Documents
    drwxr-xr-x. 2 root root       6 Sep 18 23:42 Downloads
    -rw-r--r--. 1 root root    1793 Sep 18 05:37 initial-setup-ks.cfg
    drwxr-xr-x. 2 root root       6 Sep 18 23:42 Music
    drwxr-xr-x. 2 root root       6 Sep 18 23:42 Pictures
    drwxr-xr-x. 2 root root       6 Sep 18 23:42 Public
    drwxr-xr-x. 2 root root       6 Sep 18 23:42 Templates
    drwxr-xr-x. 2 root root       6 Sep 18 23:42 Videos

    3.卷

    Docker卷是目前最为推荐的将容器中存储的数据持久化的方法,卷完全是由docker托管的。

    与绑定挂载不同的是在容器中写入的文件,在移除容器后,再次启动具有附加卷的新容器时,容器中的文件依然存在,但是不会显示写进任何Docker宿主机目录

    [root@localhost ~]# docker volume create --name=nginx-volume
    nginx-volume
    [root@localhost ~]# docker volume inspect nginx-volume
    [
        {
            "CreatedAt": "2019-09-22T04:58:44-04:00",
            "Driver": "local",
            "Labels": {},
            "Mountpoint": "/var/lib/docker/volumes/nginx-volume/_data",
            "Name": "nginx-volume",
            "Options": {},
            "Scope": "local"
        }
    ]
    [root@localhost ~]# docker volume ls
    DRIVER              VOLUME NAME
    local               nginx-volume

    Docker中的VOLUME命令

    VOLUME指令会将附加在该指令之后的路径标记为外部的由Docker托管的数据存储卷。语法如下:

    VOLUME ["/data-volume"]    

  • 相关阅读:
    P4365 [九省联考2018]秘密袭击coat
    P3705 [SDOI2017]新生舞会 01分数规划+费用流
    P4313 文理分科 最小割
    P1707 刷题比赛
    P3994 高速公路 树形DP+斜率优化+二分
    P3384 【模板】树链剖分
    P4915 帕秋莉的魔导书
    P3690 【模板】Link Cut Tree (动态树)
    P3615 如厕计划
    loj #2538. 「PKUWC2018」Slay the Spire
  • 原文地址:https://www.cnblogs.com/wang-mengmeng/p/11567918.html
Copyright © 2011-2022 走看看