美文网首页docker云服务
Docker的持久化存储和数据共享

Docker的持久化存储和数据共享

作者: 青衣敖王侯 | 来源:发表于2019-11-23 11:17 被阅读0次

    Docker的Image是只能读的,Container是可以写数据的,但是呢,一旦容器不小心被删除了,我们在容器中写的数据就没了。

    1.Docker持久化数据方案

    1.1基于本地文件系统的Volume

      执行Docker create或Docker run时,通过-v参数将主机的目录作为容器的数据卷。这部分功能便是基于本地文件系统的Volume管理

    1.2基于plugin的Volume

      支持第三方的存储方案,比如NAS,aws

    2.Volume的类型

    • 受管理的data Volume,由docker后台自动创建
    • 绑定挂载的Volume,具体挂载位置可以由用户指定。

    3.用Mysql实战

    docker run -d -v mysql:/var/lib/mysql --name mysql1 -e MYSQL_ALLOW_EMPTY_PASSWORD=true mysql
    

    -v mysql:/var/lib/mysql表示用mysql来命名这个volume,且volume的位置在/var/lib/mysql

    docker volume ls
    

    我们验证一下这个mysql的数据会存储在我们的磁盘中,而不是容器中,进入这个容器创建一个数据库

    [root@master docker]# docker exec -it mysql1 /bin/bash
    root@cf35a60f6911:/# mysql -u root
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 8
    Server version: 8.0.18 MySQL Community Server - GPL
    
    Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    +--------------------+
    4 rows in set (0.01 sec)
    
    mysql> create database docker;
    Query OK, 1 row affected (0.01 sec)
    

    退出这个容器,然后删除掉这个容器

    [root@master docker]# docker rm -f mysql1
    mysql1
    

    创建一个新容器mysql2,使用刚才的volume,然后看看数据库是否存在

    [root@master docker]# docker run -d -v mysql:/var/lib/mysql --name mysql2 -e MYSQL_ALLOW_EMPTY_PASSWORD=true mysql
    39396d68893a651ed22abec7b57ff987b89e56f2227aceb519e3387bbb89a95c
    [root@master docker]# docker exec -it mysql2 /bin/bash
    root@39396d68893a:/# mysql -u root
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 8
    Server version: 8.0.18 MySQL Community Server - GPL
    
    Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | docker             |
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    +--------------------+
    5 rows in set (0.00 sec)
    

    4.数据持久化之bind Mouting

    相比上一个例子,这里就是说我们要自己指定目录来运行容器

    4.1创建一个Dockerfile

    # this same shows how we can extend/change an existing official image from Docker Hub
    
    FROM nginx:latest
    # highly recommend you always pin versions for anything beyond dev/learn
    
    WORKDIR /usr/share/nginx/html
    # change working directory to root of nginx webhost
    # using WORKDIR is prefered to using 'RUN cd /some/path'
    
    COPY index.html index.html
    
    # I don't have to specify EXPOSE or CMD because they're in my FROM
    

    4.2创建一个index.html

    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
    
      <title>hello</title>
    
    </head>
    
    <body>
      <h1>Hello Docker! </h1>
    </body>
    </html>
    

    4.3构建一个镜像

    [root@master docker-nginx]# docker build -t mikezzmeric/mynginx .
    Sending build context to Docker daemon  3.072kB
    Step 1/3 : FROM nginx:latest
     ---> 540a289bab6c
    Step 2/3 : WORKDIR /usr/share/nginx/html
     ---> Running in fd9d8524f1a6
    Removing intermediate container fd9d8524f1a6
     ---> 923275a5e9f4
    Step 3/3 : COPY index.html index.html
     ---> deb67ab6fc35
    Successfully built deb67ab6fc35
    Successfully tagged mikezzmeric/mynginx:latest
    

    4.4映射当前目录到docker中的/usr/share/nginx/html

    [root@master docker-nginx]# docker run -d -v $(pwd):/usr/share/nginx/html -p 7070:80 --name web mikezzmeric/mynginx
    903abb342088fb61158c4312caa9bfd05f1ae3c4dc633d80259ee6e5f2c43c11
    

    4.5进入到容器中然后创建一个test.txt文件,退出容器,能在本地看到该文件

    [root@master docker-nginx]# docker exec -it web /bin/bash
    root@903abb342088:/usr/share/nginx/html# ls
    Dockerfile  index.html
    root@903abb342088:/usr/share/nginx/html# touch test.txt
    root@903abb342088:/usr/share/nginx/html# exit
    exit
    [root@master docker-nginx]# ls
    Dockerfile  index.html  test.txt
    [root@master docker-nginx]#
    

    相关文章

      网友评论

        本文标题:Docker的持久化存储和数据共享

        本文链接:https://www.haomeiwen.com/subject/exyxwctx.html