设为首页 收藏本站
查看: 1620|回复: 0

[经验分享] Docker Volumes相关知识介绍和应用

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-8-23 09:26:05 | 显示全部楼层 |阅读模式
一、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、查看镜像
1
2
3
4
5
6
#docker images -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目录
1
2
3
4
5
6
#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、进入容器查看
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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宿主机查看
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
在宿主机通过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的处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
停掉容器:
#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目录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
说明:本地目录的路径必须是绝对路径,如果目录不存在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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#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,查看版本
[iyunv@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查看

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#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目录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
在宿主机查看
#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容器中的数据存放目录
[iyunv@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/)挂载到一个新容器,如下:
1
2
3
#mkdir /root/mysql_back
#cp -r /mydata/data/* /root/mysql_back
#docker stop mysql   #停掉容器



3、重新挂载启动一个容器mysql_new
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#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
[iyunv@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数据恢复
1
2
3
4
5
6
7
8
9
10
#cp -r /root/mysql_backup/* /mydata/data/   #全部替换,或者先删除,再直接move原来备份的数据到目录
[iyunv@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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#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查看之前的数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[iyunv@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,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
查看镜像
# docker images
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)共享给这个容器,如下:
[iyunv@docker ~]# docker run -d -it --privileged -h volumetest --name server1 --volumes-from mysql_new centos:centos6 /bin/bash
43b66a52f16670466986d4efafb48a5a117f594beeed6601b780e8b358047616
[iyunv@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
进入新容器查看:
[iyunv@docker ~]# docker attach 43b66a52f166
查看共享目录情况
[iyunv@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    #已经存在了
[iyunv@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,不足之处,请多多指出!

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-261718-1-1.html 上篇帖子: docker基础操作 下篇帖子: docker批量操作命令 知识
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表