• 宿主机与容器共享数据卷-数据持久化


    docker run -it -v /宿主机绝对路径目录:/容器内目录 镜像名 #-v:数据卷 ,默认可读写

    [root@weblamp /]# docker run -it -v /myDataVolume:/dataVolumeContainer centos
    [root@4770fe12d568 /]#
    [root@4770fe12d568 /]# ll
    total 60
    -rw-r--r-- 1 root root 12076 Dec 5 01:37 anaconda-post.log
    lrwxrwxrwx 1 root root 7 Dec 5 01:36 bin -> usr/bin
    drwxr-xr-x 2 root root 4096 Jan 11 09:21 dataVolumeContainer
    drwxr-xr-x 5 root root 380 Jan 11 09:21 dev
    drwxr-xr-x 47 root root 4096 Jan 11 09:21 etc
    drwxr-xr-x 2 root root 4096 Apr 11 2018 home
    lrwxrwxrwx 1 root root 7 Dec 5 01:36 lib -> usr/lib
    lrwxrwxrwx 1 root root 9 Dec 5 01:36 lib64 -> usr/lib64
    drwxr-xr-x 2 root root 4096 Apr 11 2018 media
    drwxr-xr-x 2 root root 4096 Apr 11 2018 mnt
    drwxr-xr-x 2 root root 4096 Apr 11 2018 opt
    dr-xr-xr-x 101 root root 0 Jan 11 09:21 proc
    dr-xr-x--- 2 root root 4096 Dec 5 01:37 root
    drwxr-xr-x 11 root root 4096 Dec 5 01:37 run
    lrwxrwxrwx 1 root root 8 Dec 5 01:36 sbin -> usr/sbin
    drwxr-xr-x 2 root root 4096 Apr 11 2018 srv
    drwxr-xr-x 13 root root 0 Jan 11 09:16 sys
    drwxrwxrwt 7 root root 4096 Dec 5 01:37 tmp
    drwxr-xr-x 13 root root 4096 Dec 5 01:36 usr
    drwxr-xr-x 18 root root 4096 Dec 5 01:36 var
    [root@4770fe12d568 /]#

    #宿主机里查看
    [root@weblamp /]# ls
    bin cgroup dev home lib64 media myDataVolume proc sbin srv tmp var
    boot data etc lib lost+found mnt opt root selinux sys usr

    #测试容器停止后,修改主机myDataVolume目录里面的数据,验证数据同步功能
    [root@weblamp /]# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
    NAMES
    4770fe12d568 centos "/bin/bash" 17 hours ago Up 7 minutes
    adoring_poitras

    #使用inspect 命令查看是否绑定数据卷

    [root@weblamp /]# docker inspect 4770fe12d568 

    "Driver": "devicemapper",
    "ExecDriver": "native-0.2",
    "MountLabel": "",
    "ProcessLabel": "",
    "Volumes": {
    "/dataVolumeContainer": "/myDataVolume"
    },
    "VolumesRW": {
    "/dataVolumeContainer": true
    },
    "AppArmorProfile": "",
    "ExecIDs": null,
    "HostConfig": {
    "Binds": [
    "/myDataVolume:/dataVolumeContainer"
    ],
    "ContainerIDFile": "",
    "LxcConf": [],
    "Memory": 0,
    "MemorySwap": 0,
    ------------------------------
    docker run -it -v /宿主机绝对路径目录:/容器内目录:ro 镜像名 #容器里面的目录只读
    #使用dockerFile实现数据的持久化存储

    [root@weblamp ~]# mkdir /mydocker
    [root@weblamp ~]# cd /mydocker/
    [root@weblamp mydocker]# pwd
    /mydocker
    [root@weblamp mydocker]# vim Dockerfile
    [root@weblamp mydocker]# cat Dockerfile
    # volume test
    FROM centos
    VOLUME ["/dataVolumeContainer1","/dataVolumeContainer2"]
    CMD echo "finished,-----------successl"
    CMD /bin/bash
    # . 表示当前目录
    [root@weblamp mydocker]# docker build -f /mydocker/Dockerfile -t alvin/centos .
    Sending build context to Docker daemon 2.048 kB
    Sending build context to Docker daemon
    Step 0 : FROM centos
    ---> ab9a80ab07d0
    Step 1 : VOLUME /dataVolumeContainer1 /dataVolumeContainer2
    ---> Running in 1df5eaf31283
    ---> 6ce40a69a0fc
    Removing intermediate container 1df5eaf31283
    Step 2 : CMD echo "finished,-----------successl"
    ---> Running in 6fe6f3cc0f5d
    ---> 6c9583d31017
    Removing intermediate container 6fe6f3cc0f5d
    Step 3 : CMD /bin/bash
    ---> Running in 831931db7316
    ---> d7609a14297c
    Removing intermediate container 831931db7316
    Successfully built d7609a14297c

    [root@weblamp mydocker]# docker images
    REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
    alvin/centos latest d7609a14297c About a minute ago 201.8 MB
    atguigu/mytomcat 1.1 4a262f07577c 25 hours ago 462.5 MB
    hello-world latest 9f5834b25059 11 days ago 1.84 kB
    tomcat latest 0260919306ba 13 days ago 462.4 MB
    nginx latest 62d0e0b67f32 2 weeks ago 109.2 MB
    centos latest ab9a80ab07d0 5 weeks ago 201.8 MB

    [root@weblamp mydocker]# docker images alvin/centos
    REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
    alvin/centos latest d7609a14297c 2 minutes ago 201.8 MB
    [root@weblamp mydocker]# docker run -it alvin/centos
    [root@801c9707dbc5 /]# pwd
    /
    [root@801c9707dbc5 /]# ll
    total 64
    -rw-r--r-- 1 root root 12076 Dec 5 01:37 anaconda-post.log
    lrwxrwxrwx 1 root root 7 Dec 5 01:36 bin -> usr/bin
    drwxr-xr-x 2 root root 4096 Jan 12 07:50 dataVolumeContainer1
    drwxr-xr-x 2 root root 4096 Jan 12 07:50 dataVolumeContainer2
    drwxr-xr-x 5 root root 380 Jan 12 07:50 dev
    drwxr-xr-x 47 root root 4096 Jan 12 07:50 etc
    drwxr-xr-x 2 root root 4096 Apr 11 2018 home
    lrwxrwxrwx 1 root root 7 Dec 5 01:36 lib -> usr/lib
    lrwxrwxrwx 1 root root 9 Dec 5 01:36 lib64 -> usr/lib64
    drwxr-xr-x 2 root root 4096 Apr 11 2018 media
    drwxr-xr-x 2 root root 4096 Apr 11 2018 mnt
    drwxr-xr-x 2 root root 4096 Apr 11 2018 opt
    dr-xr-xr-x 99 root root 0 Jan 12 07:50 proc
    dr-xr-x--- 2 root root 4096 Dec 5 01:37 root
    drwxr-xr-x 11 root root 4096 Dec 5 01:37 run

    [root@801c9707dbc5 /]# cd dataVolumeContainer1
    [root@801c9707dbc5 dataVolumeContainer1]# ll
    total 0
    [root@801c9707dbc5 dataVolumeContainer1]# touch aa.txt

    #去宿主机里查看对应的目录
    [root@weblamp /]# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
    NAMES
    801c9707dbc5 alvin/centos "/bin/sh -c /bin/bas 50 minutes ago Up 50 minutes
    hopeful_tesla
    [root@weblamp /]# docker inspect 801c9707dbc5

    "Name": "/hopeful_tesla",
    "RestartCount": 0,
    "Driver": "devicemapper",
    "ExecDriver": "native-0.2",
    "MountLabel": "",
    "ProcessLabel": "",
    "Volumes": {
    "/dataVolumeContainer1":
    "/var/lib/docker/volumes/273ea5a64df76b1bc4197bedc88750dfc9228fcec7e5f12981d515c845fcb72c/_data",
    "/dataVolumeContainer2":
    "/var/lib/docker/volumes/5b528cf1db7f80bb0be92021dd60cd73426c0dc28b19ca343507e37cff6a0182/_data"
    },
    "VolumesRW": {
    "/dataVolumeContainer1": true,
    "/dataVolumeContainer2": true
    },
    "AppArmorProfile": "",


    [root@weblamp /]# cd /var/lib/docker/volumes/273ea5a64df76b1bc4197bedc88750dfc9228fcec7e5f12981d515c845fcb72c/_data
    [root@weblamp _data]# ll
    total 0
    -rw-r--r-- 1 root root 0 Jan 12 16:41 aa.txt
    [root@weblamp _data]#

    #容器数据卷
    [root@weblamp /]# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
    NAMES
    [root@weblamp /]# docker run -it --name dc01 alvin/centos
    [root@d739c8114ccb /]# ll
    total 64
    -rw-r--r-- 1 root root 12076 Dec 5 01:37 anaconda-post.log
    lrwxrwxrwx 1 root root 7 Dec 5 01:36 bin -> usr/bin
    drwxr-xr-x 2 root root 4096 Jan 12 08:49 dataVolumeContainer1
    drwxr-xr-x 2 root root 4096 Jan 12 08:49 dataVolumeContainer2
    drwxr-xr-x 5 root root 380 Jan 12 08:49 dev

    [root@d739c8114ccb /]# cd dataVolumeContainer2
    [root@d739c8114ccb dataVolumeContainer2]# pwd
    /dataVolumeContainer2

    [root@d739c8114ccb dataVolumeContainer2]# touch dc01add.txt
    [root@d739c8114ccb dataVolumeContainer2]# ll
    total 0
    -rw-r--r-- 1 root root 0 Jan 12 08:50 dc01add.txt

    ctrl+p+q
    [root@d739c8114ccb dataVolumeContainer2]# [root@weblamp /]#
    [root@weblamp /]# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
    NAMES
    d739c8114ccb alvin/centos "/bin/sh -c /bin/bas 3 minutes ago Up 3 minutes
    dc01

    #--volumes-from 容器间传递数据共享
    [root@weblamp /]# docker run -it --name dc02 --volumes-from dc01 alvin/centos 
    [root@319fc3eac74c /]#
    [root@319fc3eac74c /]# ll
    total 64
    -rw-r--r-- 1 root root 12076 Dec 5 01:37 anaconda-post.log
    lrwxrwxrwx 1 root root 7 Dec 5 01:36 bin -> usr/bin
    drwxr-xr-x 2 root root 4096 Jan 12 08:49 dataVolumeContainer1
    drwxr-xr-x 2 root root 4096 Jan 12 08:50 dataVolumeContainer2
    drwxr-xr-x 5 root root 380 Jan 12 08:54 dev
    drwxr-xr-x 47 root root 4096 Jan 12 08:54 etc

    #容器之间配置信息的传递,数据卷的生命周期一直持续到没有容器使用它为止。删除其中一个共享不受影响

    # dockerfile 就是镜像的文件构建文件
    # dockerfile编写 --->docker buile --->docker run

    [root@319fc3eac74c /]# cd dataVolumeContainer2
    [root@319fc3eac74c dataVolumeContainer2]# pwd
    /dataVolumeContainer2
    [root@319fc3eac74c dataVolumeContainer2]# ll
    total 0
    -rw-r--r-- 1 root root 0 Jan 12 08:50 dc01add.txt
    [root@319fc3eac74c dataVolumeContainer2]#

  • 相关阅读:
    Java 9 揭秘(9. 打破模块封装)
    Java 9 揭秘(8. JDK 9重大改变)
    好书分享 ——《深度工作》
    Java 9 揭秘(7. 创建自定义运行时映像)
    Java 9 揭秘(6. 封装模块)
    如何更好地管理你的精力,时间和专注力实现最佳表现
    这是您一直期待的所有iOS 11功能的屏幕截图
    我为什么不敢也不想写自己的经验和想法?
    无聊? 现在你知道为什么了!
    Java 9 揭秘(5. 实现服务)
  • 原文地址:https://www.cnblogs.com/ahtornado/p/10299983.html
Copyright © 2020-2023  润新知