Docker Volumes相關知識介紹和應用

一、Volumes相關介紹

  要了解Docker Volume,首先我們需要知道Docker的文件系統是如何工作的。Docker鏡像是由多個文件系統(只讀層)疊加而成。當我們啓動一個容器的時候,Docker會加載只讀鏡像層並在其上(譯者注:鏡像棧頂部)添加一個讀寫層。如果運行中的容器修改了現有的一個已經存在的文件,那該文件將會從讀寫層下面的只讀層複製到讀寫層,該文件的只讀版本仍然存在,只是已經被讀寫層中該文件的副本所隱藏。當刪除Docker容器,並通過該鏡像重新啓動時,之前的更改將會丟失。在Docker中,只讀層及在頂部的讀寫層的組合被稱爲Union File System(聯合文件系統)。爲了能夠保存(持久化)數據以及共享容器間的數據,Docker提出了Volume的概念。簡單來說,Volume就是目錄或者文件,它可以繞過默認的聯合文件系統,而以正常的文件或者目錄的形式存在於宿主機上

  數據卷是一個可供一個或多個容器使用的特殊目錄,它繞過UFS,可以提供很多有用的特性:

1、數據卷可以在容器之間共享和重用

2、對數據卷的修改會立馬生效

3、對數據卷的更新,不會影響鏡像

4、數據卷默認會一直存在,即使容器被刪除

*注意:數據卷的使用,類似於Linux下對目錄或文件進行mount,鏡像中的被指定爲掛載點的目錄中的文件會隱藏掉,能顯示看的是掛載的數據卷。

二、創建數據卷

在用docker run命令的時候,使用 -v 參數來創建一個數據卷並掛載到容器裏,在一次run中多次使用可以掛載多個數據卷,下面是一個創建單個數據卷的實例:

1、查看鏡像

#docker p_w_picpaths -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
web                 new                 dcca36f7ba99        24 hours ago        269.2 MB
testweb             new                 890b0964f807        24 hours ago        194.6 MB
centos              centos6             a3c09d36ab4a        2 weeks ago         194.6 MB
centos              latest              970633036444        2 weeks ago         196.7 MB

2、創建一個名爲testvolume的容器,並掛載一個volume到容器的/data目錄

#docker run -d -it --privileged --name testvolume -v /data web:new /bin/bash
f06ef551dc400c555fa2cdb22ecfcd7427c7518c407fac65214d66f0fe69e315
查看開啓的容器
#docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                  NAMES
f06ef551dc40        web:new             "/bin/bash"         24 minutes ago      Up 24 minutes                              testvolume

3、進入容器查看

#docker attach f06ef551dc40
#df -Th
Filesystem           Type    Size  Used Avail Use% Mounted on
rootfs               rootfs   10G  312M  9.7G   4% /
/dev/mapper/docker-8:2-524322-bd8f6d0fb1297cce92266242cb0eedb715582995152f8c250d11390357be3cd8
                     xfs      10G  312M  9.7G   4% /
tmpfs                tmpfs   1.9G     0  1.9G   0% /dev
tmpfs                tmpfs   1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda2            ext4     30G  4.6G   24G  17% /data               ##可以看到/data目錄已經存在了
/dev/sda2            ext4     30G  4.6G   24G  17% /etc/resolv.conf
/dev/sda2            ext4     30G  4.6G   24G  17% /etc/hostname
/dev/sda2            ext4     30G  4.6G   24G  17% /etc/hosts
shm                  tmpfs    64M     0   64M   0% /dev/shm
使用mount查看,信息如下:
#mount | grep data
/dev/sda2 on /data type ext4 (rw,relatime,data=ordered)       #可以看到掛載信息
/dev/sda2 on /etc/resolv.conf type ext4 (rw,relatime,data=ordered)
/dev/sda2 on /etc/hostname type ext4 (rw,relatime,data=ordered)
/dev/sda2 on /etc/hosts type ext4 (rw,relatime,data=ordered)
到/data目錄創建一個文件
#cd /data
#echo "first volume test!" > ./test1.txt

4、在docker宿主機查看

在宿主機通過inspect命令查看指定容器的信息
#docker inspect testvolume | grep -i volumes
            "VolumesFrom": null,
                "Source": "/var/lib/docker/volumes/cf61a7013b0675771c926a092b471d2f90f8a1ba5087f2251b9cb175a5eb7069/_data",
            "Volumes": {
可以看到volumes在/var/lib/docker目錄下,說明docker把/var/lib/docker目錄下的某個目錄(自動生成的)掛載到了容器的/data目錄下!
進入到這個路徑下面,會看到剛纔我們在容器中創建的test1.txt文件
#cd /var/lib/docker/volumes/
#ll 
total 28
drwxr-xr-x 3 root root  4096 Aug 18 16:52 cf61a7013b0675771c926a092b471d2f90f8a1ba5087f2251b9cb175a5eb7069
-rw------- 1 root root 32768 Aug 18 16:52 metadata.db
#cd cf61a7013b0675771c926a092b471d2f90f8a1ba5087f2251b9cb175a5eb7069/_data/
#ll
total 4
-rw-r--r-- 1 root root 19 Aug 18 17:12 test1.txt
#cat test1.txt
first volume test!  
同樣,在宿主機的此目錄下創建文件,在容器中的/data目錄下也是存在的!

5、停掉或者刪除容器時對volumes的處理

停掉容器:
#docker stop f06ef551dc40
#ll /var/lib/docker/volumes/cf61a7013b0675771c926a092b471d2f90f8a1ba5087f2251b9cb175a5eb7069/_data/
total 4
-rw-r--r-- 1 root root 19 Aug 18 17:12 test1.txt    #文件還在
刪除容器(刪除容器之前一定要先停掉該容器,否則會報錯)
#docker rm f06ef551dc40
#ll /var/lib/docker/volumes/cf61a7013b0675771c926a092b471d2f90f8a1ba5087f2251b9cb175a5eb7069/_data/
total 4
-rw-r--r-- 1 root root 19 Aug 18 17:12 test1.txt    #文件依然還在
如果想在刪除容器的同時數據卷volumes也一併刪除了,加上-v參數即可,如下:
#docker rm -v 容器ID     #這樣/var/lib/docker/volumes/目錄下的數據卷就會一起刪除!
注:通過這種方式掛載volume因爲沒有指定宿主機的目錄,所以docker會將數據卷volume存放在宿主機的默認路徑/var/lib/docker/volumes/目錄下面,
如果我們不想將數據卷volume存放在默認路徑,可以自定義路徑來進行掛載,後面會介紹。這種數據卷掛載的方式可以實現不少的功能,比如單純的數據
文件互傳,可以在宿主機和容器之間實現數據共享;還可以出於數據安全考慮,將一些重要數據(比如db)存放到容器的掛載目錄,萬一容器出現了問題
數據是不會丟失的。

三、掛載一個宿主機目錄作爲數據卷

1、創建一個名爲mysql的容器,並將宿主機/mydata/data掛載到容器的/var/lib/mysql目錄

說明:本地目錄的路徑必須是絕對路徑,如果目錄不存在Docker會自動創建它。
#docker run -d -it --privileged -p 3306:3306 --name mysql -v /mydata/data:/var/lib/mysql centos:centos6 /bin/bash
a72af613c7678b1f487cc52f527ae40318d7566711932f0ec9ec33ba87b56b43
#ll /mydata/data/   ##可以看到在宿主機已經創建了/mydata/data目錄
total 0 
進入容器
#docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS                    NAMES
a72af613c767        centos:centos6      "/bin/bash"         About a minute ago   Up About a minute   0.0.0.0:3306->3306/tcp   mysql
#docker attach a72af613c767
#df -Th
Filesystem           Type    Size  Used Avail Use% Mounted on
rootfs               rootfs   10G  237M  9.8G   3% /
/dev/mapper/docker-8:2-524322-0b75bb590a8fe384007e2fc32c5fdbc2995497bc26232d80f748f1bf88ae48cf
                     xfs      10G  237M  9.8G   3% /
tmpfs                tmpfs   1.9G     0  1.9G   0% /dev
tmpfs                tmpfs   1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda2            ext4     30G  4.6G   24G  17% /etc/resolv.conf
/dev/sda2            ext4     30G  4.6G   24G  17% /etc/hostname
/dev/sda2            ext4     30G  4.6G   24G  17% /etc/hosts
shm                  tmpfs    64M     0   64M   0% /dev/shm
/dev/sda2            ext4     30G  4.6G   24G  17% /var/lib/mysql    #掛載已經成功
# mount | grep data    #mount查看
/dev/sda2 on /etc/resolv.conf type ext4 (rw,relatime,data=ordered)
/dev/sda2 on /etc/hostname type ext4 (rw,relatime,data=ordered)
/dev/sda2 on /etc/hosts type ext4 (rw,relatime,data=ordered)
/dev/sda2 on /var/lib/mysql type ext4 (rw,relatime,data=ordered)     #已經掛載了

2、在創建的容器中安裝mysql

#yum install mysql mysql-server -y
啓動MySQL
# service mysqld start
Initializing MySQL database:  Installing MySQL system tables...
OK
Filling help tables...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h a72af613c767 password 'new-password'
Alternatively you can run:
/usr/bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd /usr/mysql-test ; perl mysql-test-run.pl
Please report any problems with the /usr/bin/mysqlbug script!

[  OK  ]
Starting mysqld:  [  OK  ]

進入mysql,查看版本
[root@a72af613c767]#mysql 
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.1.73    |
+-----------+
1 row in set (0.00 sec)
設置字符編碼爲utf8
#vi /etc/my.cnf
[client]
default-character-set = utf8
[mysqld]
character-set-server=utf8
重啓mysql
創建一個數據庫mydata
mysql> create database mydata;
Query OK, 1 row affected (0.00 sec)
創建一個表students
mysql> CREATE TABLE `students` (  
    ->        `id` int(11) NOT NULL AUTO_INCREMENT,  
    ->        `name` varchar(20) NOT NULL,  
    ->        `sex` varchar(6),  
    ->        `class`  varchar(30),  
    ->        `time`  date,   
    ->        PRIMARY KEY (`id`)  
    ->       ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.01 sec)、
插入2條數據
mysql> INSERT INTO students values(NULL,'james','man','nba-騎士','2003-08-10');
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO students values(NULL,'curry','man','nba-勇士','2009-08-10');
Query OK, 1 row affected (0.00 sec)
查看數據
mysql> select * from students;
+----+-------+------+------------+------------+
| id | name  | sex  | class      | time       |
+----+-------+------+------------+------------+
|  1 | james | man  | nba-騎士   | 2003-08-10 |
|  2 | curry | man  | nba-勇士   | 2009-08-10 |
+----+-------+------+------------+------------+
2 rows in set (0.00 sec)
創建一個admin賬號,供連接使用
mysql> grant all on *.* to 'admin'@'%' identified by 'xxxxx';
Query OK, 0 rows affected (0.00 sec)

3、在宿主機進入容器的mysql查看

#yum install mysql     #因爲是centos7.x系統所以實際安裝的是mariadb
查看安裝mysql的容器的ip地址
#docker exec mysql ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:03  
          inet addr:172.17.0.3  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::42:acff:fe11:3/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:28826 errors:0 dropped:0 overruns:0 frame:0
          TX packets:23024 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:53586120 (51.1 MiB)  TX bytes:1528950 (1.4 MiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:11 errors:0 dropped:0 overruns:0 frame:0
          TX packets:11 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:784 (784.0 b)  TX bytes:784 (784.0 b)
進入mysql
#mysql -uadmin -h172.17.0.3 -p
Enter password:        ##輸入密碼
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> show engines;
+------------+---------+------------------------------------------------------------+--------------+------+------------+
| Engine     | Support | Comment                                                    | Transactions | XA   | Savepoints |
+------------+---------+------------------------------------------------------------+--------------+------+------------+
| MRG_MYISAM | YES     | Collection of identical MyISAM tables                      | NO           | NO   | NO         |
| CSV        | YES     | CSV storage engine                                         | NO           | NO   | NO         |
| MyISAM     | DEFAULT | Default engine as of MySQL 3.23 with great performance     | NO           | NO   | NO         |
| InnoDB     | YES     | Supports transactions, row-level locking, and foreign keys | YES          | YES  | YES        |
| MEMORY     | YES     | Hash based, stored in memory, useful for temporary tables  | NO           | NO   | NO         |
+------------+---------+------------------------------------------------------------+--------------+------+------------+
5 rows in set (0.00 sec)

四、使用volumes恢復屬於應對特殊情況

說明:對業務而言,數據是最重要的,保證數據的安全是工作重中之重,如果使用docker容器來跑應用服務,最好可以通過volumes的方式掛載到容器,然後將數據單獨存放在掛載的目錄中,這樣可以確保不會因爲容器的損壞而導致數據也丟失。不過也有很多其他的用法,這裏只介紹mysql使用volumes數據捲來存放mysql數據的例子,接着第三步繼續,如下:

1、先查看在volumes目錄

在宿主機查看
#ll /mydata/data/
total 20492
-rw-rw---- 1 27 27 10485760 Aug 19 11:27 ibdata1
-rw-rw---- 1 27 27  5242880 Aug 19 11:27 ib_logfile0
-rw-rw---- 1 27 27  5242880 Aug 18 19:18 ib_logfile1
drwx------ 2 27 27     4096 Aug 19 11:07 mydata
drwx------ 2 27 27     4096 Aug 18 19:18 mysql
srwxrwxrwx 1 27 27        0 Aug 18 19:21 mysql.sock
drwx------ 2 27 27     4096 Aug 18 19:18 test
再查看mysql容器中的數據存放目錄
[root@docker ~]# docker exec mysql ls -l /var/lib/mysql   
total 20492
-rw-rw---- 1 mysql mysql  5242880 Aug 19 03:27 ib_logfile0
-rw-rw---- 1 mysql mysql  5242880 Aug 18 11:18 ib_logfile1
-rw-rw---- 1 mysql mysql 10485760 Aug 19 03:27 ibdata1
drwx------ 2 mysql mysql     4096 Aug 19 03:07 mydata
drwx------ 2 mysql mysql     4096 Aug 18 11:18 mysql
srwxrwxrwx 1 mysql mysql        0 Aug 18 11:21 mysql.sock
drwx------ 2 mysql mysql     4096 Aug 18 11:18 test
可以看到內容和容器中/var/lib/mysql中的數據是一樣的

2、在宿主機備份這個目錄,然後停掉或者刪除容器,重新使用volumes(/mydata/data/)掛載到一個新容器,如下:

#mkdir /root/mysql_back
#cp -r /mydata/data/* /root/mysql_back
#docker stop mysql   #停掉容器

3、重新掛載啓動一個容器mysql_new

#docker run -d -it --privileged -p 3306:3306 --name mysql_new -v /mydata/data:/var/lib/mysql centos:centos6 /bin/bash
748c3567ea0eff56c03777f516cc173b5d2ba29a9b0e5173e43d07e20447fc5d
進入該容器
#docker attach mysql_new
[root@748c3567ea0e ~]#ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:03  
          inet addr:172.17.0.3  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::42:acff:fe11:3/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:8 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:648 (648.0 b)  TX bytes:648 (648.0 b)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
安裝mysql
#yum install mysql mysql-server  -y
啓動mysql
# service mysqld start
Initializing MySQL database:  Installing MySQL system tables...
OK
Filling help tables...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h 748c3567ea0e password 'new-password'
Alternatively you can run:
/usr/bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd /usr/mysql-test ; perl mysql-test-run.pl
Please report any problems with the /usr/bin/mysqlbug script!
[  OK  ]
Starting mysqld:  [  OK  ]
進入mysql,查看
#mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2013, 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              |
| test               |
+--------------------+
3 rows in set (0.00 sec)

4、將之前備份的mysql數據恢復

#cp -r /root/mysql_backup/* /mydata/data/   #全部替換,或者先刪除,再直接move原來備份的數據到目錄
[root@docker ~]# ll /mydata/data/
total 20492
-rw-r----- 1 root root 10485760 Aug 22 10:21 ibdata1
-rw-r----- 1 root root  5242880 Aug 22 10:21 ib_logfile0
-rw-r----- 1 root root  5242880 Aug 22 10:21 ib_logfile1
drwx------ 2 root root     4096 Aug 22 10:21 mydata
drwx------ 2 root root     4096 Aug 22 10:21 mysql
srwxr-xr-x 1 root root        0 Aug 22 10:21 mysql.sock
drwx------ 2 root root     4096 Aug 22 10:21 test

5、進入docker容器mysql_new,重啓mysql

#docker attach mysql_new
重啓mysql
# service mysqld restart
Stopping mysqld:  [  OK  ]
MySQL Daemon failed to start.
Starting mysqld:  [FAILED]
報錯了,原因是/var/lib/mysql權限的問題
修改權限
#chown mysql.mysql /var/lib/mysql/ -R
設置字符編碼爲utf8
#vi /etc/my.cnf
[client]
default-character-set = utf8
[mysqld]
character-set-server=utf8
再次啓動就可以了
#service mysqld start
Starting mysqld:  [  OK  ]

進入mysql查看之前的數據

[root@748c3567ea0e ~]# mysql -uroot -p 
Enter password:         ##之前是設置了密碼的,輸入密碼
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2013, 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> use mydata             
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from students;
+----+-------+------+------------+------------+
| id | name  | sex  | class      | time       |
+----+-------+------+------------+------------+
|  1 | james | man  | nba-騎士   | 2003-08-10 |
|  2 | curry | man  | nba-勇士   | 2009-08-10 |
+----+-------+------+------------+------------+
2 rows in set (0.08 sec)
數據和之前是一樣!

PS:在生產環境,有時候情況比較複雜了,可能用到mysql主從或者集羣什麼的,採用這種volumes的方式需要充足的考慮和測試!

五、volumes數據卷實現容器之間數據共享

如果要授權一個容器訪問另一個容器的Volume,我們可以使用-volumes-from參數來執行docker run,如下:

查看鏡像
# docker p_w_picpaths
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
web                 new                 dcca36f7ba99        9 weeks ago         269.2 MB
testweb             new                 890b0964f807        9 weeks ago         194.6 MB
centos              centos6             a3c09d36ab4a        12 weeks ago        194.6 MB
centos              latest              970633036444        12 weeks ago        196.7 MB
查看容器
# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS                    NAMES
748c3567ea0e        centos:centos6      "/bin/bash"         8 weeks ago         Up 8 weeks                 0.0.0.0:3306->3306/tcp   mysql_new
a72af613c767        centos:centos6      "/bin/bash"         9 weeks ago         Exited (137) 8 weeks ago                            mysql
222140ba7bc5        testweb:new         "/bin/bash"         9 weeks ago         Up 9 weeks                 0.0.0.0:5000->80/tcp     web1
開啓一個名爲server1的容器,並且將mysql_new這個容器的volumes(/var/lib/mysql)共享給這個容器,如下:
[root@docker ~]# docker run -d -it --privileged -h volumetest --name server1 --volumes-from mysql_new centos:centos6 /bin/bash
43b66a52f16670466986d4efafb48a5a117f594beeed6601b780e8b358047616
[root@docker ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS                    NAMES
43b66a52f166        centos:centos6      "/bin/bash"         3 seconds ago       Up 2 seconds                                        server1
748c3567ea0e        centos:centos6      "/bin/bash"         8 weeks ago         Up 8 weeks                 0.0.0.0:3306->3306/tcp   mysql_new
a72af613c767        centos:centos6      "/bin/bash"         9 weeks ago         Exited (137) 8 weeks ago                            mysql
222140ba7bc5        testweb:new         "/bin/bash"         9 weeks ago         Up 9 weeks                 0.0.0.0:5000->80/tcp     web1
進入新容器查看:
[root@docker ~]# docker attach 43b66a52f166
查看共享目錄情況
[root@volumetest ~]# df -Th    
Filesystem           Type    Size  Used Avail Use% Mounted on
rootfs               rootfs   10G  237M  9.8G   3% /
/dev/mapper/docker-8:2-524322-1d036e7c063d8c3c66163a1cb0b415cb1bc592c4c9daa2a800239a5ac151dc52
                     xfs      10G  237M  9.8G   3% /
tmpfs                tmpfs   1.9G     0  1.9G   0% /dev
tmpfs                tmpfs   1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda2            ext4     30G  5.1G   23G  19% /etc/resolv.conf
/dev/sda2            ext4     30G  5.1G   23G  19% /etc/hostname
/dev/sda2            ext4     30G  5.1G   23G  19% /etc/hosts
shm                  tmpfs    64M     0   64M   0% /dev/shm
/dev/sda2            ext4     30G  5.1G   23G  19% /var/lib/mysql    #已經存在了
[root@volumetest /]# ll /var/lib/mysql/
total 20492
-rw-r----- 1 27 27  5242880 Oct 23 08:30 ib_logfile0
-rw-r----- 1 27 27  5242880 Aug 22 02:21 ib_logfile1
-rw-r----- 1 27 27 10485760 Oct 23 08:30 ibdata1
drwx------ 2 27 27     4096 Aug 22 02:21 mydata
drwx------ 2 27 27     4096 Aug 22 02:21 mysql
srwxrwxrwx 1 27 27        0 Oct 23 08:29 mysql.sock
drwx------ 2 27 27     4096 Aug 22 02:21 test
注:-h volumetest  #意思是設置容器的hostname 爲volumetest

初學docker,不足之處,請多多指出!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章