[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
進入第一個容器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
進入第二個容器mysql
[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目錄掛載成遠程文件系統,容器掛載在這個遠程文件系統上時操做的就是遠程目錄的權限,而且操做的是同一個遠程文件共享。這樣就能夠實現數據的一至性,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 testsql