[root@localhost /]# docker run --name mysql -p 3306:3306 -v /mysql:/var/lib/mysql -d hub.c.163.com/public/centos:6.7-tools b6045231a2963976472024969f2d5a16541fbc900c1532dfc892013824c23f0c [root@localhost /]# cd mysql/ [root@localhost mysql]# ls [root@localhost mysql]# docker run --name web1 -p 80:80 -v /web1:/var/www/html -d hub.c.163.com/public/centos:6.7-tools 20e93e34080badf563e3abacae7d61cddc5496c1ec8d21aa401bd4b4646d6308 [root@localhost mysql]# cd /web1/ [root@localhost web1]# ls
[root@localhost web1]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 20e93e34080b hub.c.163.com/public/centos:6.7-tools "/usr/bin/supervisord" 36 seconds ago Up 33 seconds 22/tcp, 0.0.0.0:80->80/tcp web1 b6045231a296 hub.c.163.com/public/centos:6.7-tools "/usr/bin/supervisord" 2 minutes ago Up 2 minutes 22/tcp, 0.0.0.0:3306->3306/tcp mysql
[root@localhost web1]# docker run --name web2 -p 81:80 -v /web1:/var/www/html -d hub.c.163.com/public/centos:6.7-tools 8ad966f1b220103068a740a823b895932e67160c83450c1811a4fe69adf5ee9f [root@localhost ~]# cd /web1/ [root@localhost web1]# echo "test docker volume" > index.html
进入第一个容器
[root@localhost web1]# docker exec -it web1 /bin/bash [root@20e93e34080b /]# yum -y install httpd [root@20e93e34080b /]# service httpd start [root@20e93e34080b /]# curl localhost test docker volume
进入第二个容器
[root@localhost web1]# docker exec -it web2 /bin/bash [root@8ad966f1b220 /]# yum -y install httpd [root@8ad966f1b220 /]# service httpd start [root@8ad966f1b220 /]# curl localhost test docker volume
对于多个容器来说我可以把他挂载到同一个目录下,实现他们的数据一至,如果这是一个web集群的话,本地磁盘足够大,就可以做到目录共享的作用。在小集群下可以这么做,但是如果web服务器很多的话不建议这么做,这样本地的磁盘io会成为一个瓶颈,可以把根下的web目录挂载成远程文件系统,容器挂载在这个远程文件系统上时操作的就是远程目录的权限,并且操作的是同一个远程文件共享。这样就可以实现数据的一至性,
数据管理 [root@localhost ~]# docker run --name mysql -p 3306:3306 -v /mysql:/var/lib/mysql -d hub.c.163.com/public/centos:6.7-tools 9f9d53b1f15270a9a35702c6c32e03517d38e37791f7ab776263d3bf2de0c300 [root@localhost ~]# docker exec -it mysql /bin/bash [root@9f9d53b1f152 /]# yum -y install mysql msyql-server [root@9f9d53b1f152 /]# service mysqld start [root@9f9d53b1f152 /]# mysqladmin -uroot password 123 [root@9f9d53b1f152 /]# mysql -uroot -p123 mysql> create database aa; Query OK, 1 row affected (0.01 sec) mysql> use aa; Database changed mysql> create table a1 (id int,name char(30)); Query OK, 0 rows affected (0.00 sec) mysql> insert into a1 values(1,"lishi") -> ; Query OK, 1 row affected (0.01 sec) mysql> select * from a1; +------+-------+ | id | name | +------+-------+ | 1 | lishi | +------+-------+ 1 row in set (0.00 sec) [root@localhost ~]# cd /mysql/ [root@localhost mysql]# ls aa ibdata1 ib_logfile0 ib_logfile1 mysql mysql.sock test [root@localhost mysql]# cd aa [root@localhost aa]# ls a1.frm a1.MYD a1.MYI db.opt [root@localhost aa]# docker rm -f mysql [root@localhost /]# cd /mysql [root@localhost mysql]# ls #数据还存在了。 aa ibdata1 ib_logfile0 ib_logfile1 mysql mysql.sock test
[root@localhost mysql]# docker run --name mysql -p 3306:3306 -v /mysql:/var/lib/mysql -d hub.c.163.com/public/centos:6.7-tools 9c2e1a7437a7d3406ee3fdb2ca6d14dde61ee853bab36e4e8b64431d4116a105 [root@localhost mysql]# docker exec -it mysql /bin/bash [root@9c2e1a7437a7 /]# yum -y install mysql mysql-server [root@9c2e1a7437a7 /]# service mysqld start [root@9c2e1a7437a7 /]# mysql -uroot -p123 mysql> show databases; #数据库又回来了。 +--------------------+ | Database | +--------------------+ | information_schema | | aa | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec)
[root@localhost aa]# docker pull wordpress [root@localhost aa]# docker run --name blog -d 1b1624b63467 13dfa8e0169527ac5547540529dded631435459b2fba42d0c6d2cea94fcd6775 [root@localhost aa]# docker exec -it blog /bin/bash root@13dfa8e01695:/var/www/html# root@13dfa8e01695:/var/www/html# touch ljj root@13dfa8e01695:/var/www/html# exit exit [root@localhost aa]# docker inspect blog |grep "Mounts" -A 11 "Mounts": [ { "Type": "volume", "Name": "4ec446e5dbc4e82aaf3f5086e746a0b54ddddcc8b1074ee34324001ef8e19938", "Source": "/var/lib/docker/volumes/4ec446e5dbc4e82aaf3f5086e746a0b54ddddcc8b1074ee34324001ef8e19938/_data", "Destination": "/var/www/html", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ], [root@localhost volumes]# cd /var/lib/docker/volumes/4ec446e5dbc4e82aaf3f5086e746a0b54ddddcc8b1074ee34324001ef8e19938/_data/ [root@localhost _data]# ls ljj ljj [root@localhost _data]# docker run --name blog -v /var/lib/docker/volumes/4ec446e5dbc4e82aaf3f5086e746a0b54ddddcc8b1074ee34324001ef8e19938/_data:/var/www/html -d wordpress:latest 7bb09ababad43c8b2a433c0425783ecab9d5d7f8ea8c47b55e943dc56d27914e [root@localhost _data]# docker exec -it blog /bin/bash root@7bb09ababad4:/var/www/html# ls ljj ljj [root@localhost _data]# docker run --name blog1 --volumes-from="blog" -d wordpress:latest 8bb66204ec2376a59d7eb78233e544b7b6f0a9dd36a200c198a9c8743cb88546 [root@localhost _data]# docker exec -it blog1 /bin/bash root@8bb66204ec23:/var/www/html# ls ljj ljj [root@localhost _data]# docker rm -f -v blog1 #连持久化目录一起删除了。 blog1
数据管理[root@localhost ~]# docker run --name mysql -p 3306:3306 -v /mysql:/var/lib/mysql -d hub.c.163.com/public/centos:6.7-tools9f9d53b1f15270a9a35702c6c32e03517d38e37791f7ab776263d3bf2de0c300
[root@localhost ~]# docker exec -it mysql /bin/bash[root@9f9d53b1f152 /]# yum -y install mysql msyql-server[root@9f9d53b1f152 /]# service mysqld start[root@9f9d53b1f152 /]# mysqladmin -uroot password 123[root@9f9d53b1f152 /]# mysql -uroot -p123mysql> create database aa;Query OK, 1 row affected (0.01 sec)
mysql> use aa;Database changedmysql> create table a1 (id int,name char(30));Query OK, 0 rows affected (0.00 sec)mysql> insert into a1 values(1,"lishi")-> ;Query OK, 1 row affected (0.01 sec)mysql> select * from a1;+------+-------+| id | name |+------+-------+| 1 | lishi |+------+-------+1 row in set (0.00 sec)
[root@localhost ~]# cd /mysql/[root@localhost mysql]# lsaa ibdata1 ib_logfile0 ib_logfile1 mysql mysql.sock test[root@localhost mysql]# cd aa[root@localhost aa]# lsa1.frm a1.MYD a1.MYI db.opt
[root@localhost aa]# docker rm -f mysql[root@localhost /]# cd /mysql[root@localhost mysql]# ls#数据还存在了。aa ibdata1 ib_logfile0 ib_logfile1 mysql mysql.sock test