• docker-管理应用程序数据


    1. docker-将数据从宿主机挂载到容器中的三种方式
      1) docker提供三种方式将数据从宿主机挂载到容器中:

    • volumes:docker管理宿主机文件系统的一部分(/var/lib/docker/volumes)。保存数据的最佳方式
    • bind mounts:将宿主机上的任意位置的文件或者目录挂载到容器中。
    • tmpfs:挂载存储在主机系统的内存中,而不会写入主机的文件系统,如果不希望将数据持久存储在任何位置,可以用tmpfs,同时避免写入容器可写层提高性能。

      

    2. docker的volumes管理宿主机管理本地文件
      1) docker volumes创建管理卷

    [root@test-1 ~]# docker volume create nginx_vol
    nginx_vol
    [root@test-1 ~]# docker volume ls
    DRIVER              VOLUME NAME
    local               nginx_vol
    [root@test-1 ~]# docker volume inspect nginx_vol       #查看nginx-vol详细信息
    [
        {
            "CreatedAt": "2018-11-22T01:10:06-05:00",
            "Driver": "local",
            "Labels": {},
            "Mountpoint": "/var/lib/docker/volumes/nginx_vol/_data",               #目录挂载点
            "Name": "nginx_vol",
            "Options": {},
            "Scope": "local"
        }
    ]
    

      2) docker用卷创建一个容器
        (1) 使用mount的方式启动

    [root@test-1 html]# docker run -d --name web02 --mount src=nginx_vol,dst=/usr/share/nginx/html nginx
    79afc418f70031478f098f8390ba1475563b1d9c52fa084e383f0113ce683d4d
    [root@test-1 html]# docker ps 
    CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
    1e380bf77ea4        nginx               "nginx -g 'daemon of…"   About a minute ago   Up About a minute   80/tcp              web02
    [root@test-1 html]# ll /var/lib/docker/volumes/nginx_vol/_data/
    total 8
    -rw-r--r-- 1 root root 494 Nov  6 08:32 50x.html
    -rw-r--r-- 1 root root 612 Nov  6 08:32 index.html
    

     (2) 使用-v的方式启动

    [root@test-1 html]# docker run -d --name=web03 -v nginx_vol:/usr/share/nginx/html nginx
    3a086829032e967657867298640d83a1212a4642f07245f0fab882315afd4e64
    [root@test-1 html]# docker ps -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
    3a086829032e        nginx               "nginx -g 'daemon of…"   3 minutes ago       Up 3 minutes               80/tcp              web03
    2f6d6db0c6a4        nginx               "nginx -g 'daemon of…"   11 minutes ago      Exited (0) 9 minutes ago                       web02
    

     (3) 清理

    [root@test-1 html]# docker stop web02
    web02
    [root@test-1 html]# docker rm web02
    web02
    [root@test-1 html]# docker stop web03
    web03
    [root@test-1 html]# docker rm web03
    web03
    [root@test-1 html]# docker volume rm nginx_vol 
    nginx_vol
    

     (4) 注意

      1) 如果没有指定卷,就自动创建

      2) 建议使用--mount,更通用。

    3. docker的bind Mounts管理宿主机管理本地文件
      1)docker Bind Mounts管理宿主机管理文件

    [root@test-1 html]# docker run -d  -p80:80 --name=web01 --mount type=bind,src=/var/www/html,dst=/usr/share/nginx/html nginx
    [root@test-1 html]# docker run -d  -p81:80 --name=web02 -v /var/www/html:/usr/share/nginx/html nginx
    
    提示:
    --mount 和 -v 效果一样的
    

      2)验证

    [root@test-1 html]# docker inspect web01 
    

      3)清理

    [root@test-1 html]# docker stop web01
    web01
    [root@test-1 html]# docker stop web02
    web02
    [root@test-1 html]# docker rm web01
    web01
    [root@test-1 html]# docker rm web02
    web02

      4) 注意
        (1) 如果源文件/目录没有存在,不会自动创建,会抛出一个错误
        (2)如果挂载目标在容器中非空目录。则该目录现有内容将被隐藏。

      5) 运行结果

      

    4. 总结
      1) volume特点
      • 多个运行容器之间共享数据
      • 当容器停止或被移除时,该卷依然存在
      • 多个容器可以同时挂载相同的卷
      • 当明确删除卷时,卷才会被删除
      • 将容器的数据存储在远程主机或其他存储上
      • 将数据从一台docker主机迁移到另一台时,先停止容器,然后备份卷的目录(/var/lib/docker/volumes/)
      2) bind Mounts特点
      • 从主机共享配置文件到容器。默认情况下,挂载主机/etc/resolv.conf到每个容器,提供DNS解析
      • 在docker主机上的开发环境和容器之间共享源代码。列如,可以将Maven target目录挂载到容器中,每次在docker主机上构建Maven项目时,容器都可以访问构建的项目包。
    当docker主机的文件或目录结构保证与容器所需的绑定挂载一直时。

     

  • 相关阅读:
    Linux 常用命令
    silky微服务简介
    okhttp中的Builder模式
    Assert in C#&Java
    Abstract类中使用@Autowire
    @Scope("prototype") bean scope not creating new bean
    【转】centos系统查看cpu内存等情况
    hdu 7013 String Mod 题解
    牛客 11259 H Scholomance Academy 题解
    bzoj 2151 种树 题解
  • 原文地址:https://www.cnblogs.com/scajy/p/12586500.html
Copyright © 2020-2023  润新知