99热99这里只有精品6国产,亚洲中文字幕在线天天更新,在线观看亚洲精品国产福利片 ,久久久久综合网

歡迎加入QQ討論群258996829
麥子學(xué)院 頭像
蘋果6袋
6
麥子學(xué)院

Docker進階:容器中的數(shù)據(jù)管理

發(fā)布時間:2016-11-16 16:10  回復(fù):0  查看:2191   最后回復(fù):2016-11-16 16:10  

本文和大家分享的是Docker進階中容器中的數(shù)據(jù)管理相關(guān)知識,希望可以幫助大家更好的學(xué)習(xí)Docker,一起來看看吧。

  先思考一些場景,如果利用Docker創(chuàng)建了一個N個容器,這些容器之間需要數(shù)據(jù)共享,此時該怎么辦?如果我們想在本機了解容器的運行狀態(tài)、命令歷史等,此時該怎么辦?

  按照Docker官方文檔的說明,容器中的數(shù)據(jù)管理有兩種形式: Manage data in containers

  數(shù)據(jù)卷(Data Volumes

  可以將數(shù)據(jù)卷理解為容器中的一個目錄,類似于Linuxmount的概念。創(chuàng)建容器時,可以一并創(chuàng)建數(shù)據(jù)卷,并且能夠掛載一個主機目錄為數(shù)據(jù)卷。有點繞口,實例說明一下。

 ?。?/span>1)創(chuàng)建mysql容器,不添加任何關(guān)于數(shù)據(jù)卷的參數(shù):

這里假設(shè)我們已經(jīng)pullmysql鏡像

[root@xx ~]# docker run -d -p 3306:3306 --name mysql01 -e MYSQL_ROOT_PASSWORD=123456 mysql # 創(chuàng)建一個名字為mysql01的容器

  此時并沒有指定關(guān)于任何數(shù)據(jù)卷的參數(shù)。-p 3306:3306是將本機的3306端口映射到容器的3306端口。此時利用命令查看容器的基本信息,其中一段為:

  [root@xx ~]# docker inspect mysql01

  ......"Mounts": [

  {

  "Name": "6cb3597e2da5......",

  "Source": "/var/lib/docker/volumes/6cb3597e2da5....../_data",

  "Destination": "/var/lib/mysql",

  "Driver": "local",

  "Mode": "",

  "RW": true,

  "Propagation": ""

  }

  ],

  ......

  這段數(shù)據(jù)說明該容器自動生成一個數(shù)據(jù)卷,在容器中的目錄為"/var/lib/mysql",用于存放Mysql數(shù)據(jù),同時在本機也有相對應(yīng)的目錄:"/var/lib/docker/volumes/....../_data"。當(dāng)你在Mysql中創(chuàng)建數(shù)據(jù)庫、表時,就會在本機目錄下生成相應(yīng)的數(shù)據(jù)。

Docker進階:容器中的數(shù)據(jù)管理


2)創(chuàng)建mysql容器,并添加自定義的數(shù)據(jù)卷:

這里假設(shè)我們已經(jīng)pullmysql鏡像

[root@xx ~]# docker run -d -p 3307:3306 --name mysql02 -e MYSQL_ROOT_PASSWORD=123456 -v /appdata mysql # 創(chuàng)建一個名字為mysql02的容器

  此時指定了一個-v /appdata的參數(shù),用于在容器中創(chuàng)建一個目錄為/appdata的數(shù)據(jù)卷。注意-p選項變?yōu)榱?/span>-p 3307:3306,這是因為上一次的容器將本機的3306端口占用了,需要選擇另外一個端口。此時利用inspect命令查看容器基本信息,其中一段為:

  [root@xx ~]# docker inspect mysql02"Mounts": [

  {

  "Name": "d95fd2d33.......",

  "Source": "/var/lib/docker/volumes/d95fd2d33....../_data",

  "Destination": "/appdata",

  ........

  },

  {

  "Name": "695e371973......",

  "Source": "/var/lib/docker/volumes/695e371973....../_data",

  "Destination": "/var/lib/mysql",

  ........

  }

  ],

  這個容器中有兩個數(shù)據(jù)卷,一個是Mysql自建的"/var/lib/mysql",用于存放mysql數(shù)據(jù)。另外一個是我們自己指定的"/appdata",類似于前者,這個數(shù)據(jù)卷在本機也有相對應(yīng)的目錄。這里的"appdata"數(shù)據(jù)卷并沒有什么用,只是作為例子說明一下。

 ?。?/span>3)創(chuàng)建mysql容器,并掛載本機目錄作為數(shù)據(jù)卷

這里假設(shè)我們已經(jīng)pullmysql鏡像

[root@xx ~]# docker run -d -p 3308:3306 --name mysql03 -e MYSQL_ROOT_PASSWORD=123456 -v /root/mysqldata/:/var/lib/mysql mysql

  此時的-v參數(shù)將本機的"/root/mysqldata"目錄掛載到"/var/lib/mysql"。此時查看容器的基本信息,其中一段為:

  [root@xx ~]# docker inspect mysql03"Mounts": [

  {

  "Source": "/root/mysqldata",

  "Destination": "/var/lib/mysql",

  "Mode": "",

  "RW": true,

  "Propagation": "rprivate"

  }

  ],

  這里只有一個數(shù)據(jù)卷,應(yīng)該比較容易理解。注意這里的"Propagation"參數(shù),官方的解釋為:

  By default bind mounted volumes are private. That means any mounts done inside container will not be visible on host and vice-a-versa. One can change this behavior by specifying a volume mount propagation property. Making a volume shared mounts done under that volume inside container will be visible on host and vice-a-versa. Making a volume slave enables only one way mount propagation and that is mounts done on host under that volume will be visible inside container but not the other way around.To control mount propagation property of volume one can use :[r]shared, :[r]slave or :[r]private propagation flag. Propagation property can be specified only for bind mounted volumes and not for internal volumes or named volumes. For mount propagation to work source mount point (mount point where source dir is mounted on) has to have right propagation properties. For shared volumes, source mount point has to be shared. And for slave volumes, source mount has to be either shared or slave.

 ?。?/span>4)創(chuàng)建容器,并掛載本機文件作為數(shù)據(jù)卷。

  除了能掛載本機的目錄外,同樣可以掛載本機文件。官網(wǎng)上給出的例子是:

  $ docker run --rm -it -v ~/.bash_history:/root/.bash_history ubuntu /bin/bash

  這里將本機的.bash_history文件掛載到容器的.bash_history文件上。根據(jù)數(shù)據(jù)卷的特性,這樣在本機就能看到容器中的操作歷史了。

 ?。?/span>5)刪除數(shù)據(jù)卷。數(shù)據(jù)卷是被用來持久化數(shù)據(jù)的,它獨立于容器存在。當(dāng)我們刪除容器時,并不會自動刪除數(shù)據(jù)卷,同時Docker也沒有垃圾回收機制可以回收無用的數(shù)據(jù)卷。但是我們可以在刪除容器時,連同數(shù)據(jù)卷一并刪除,比如:docker rm -v mysql02。

  數(shù)據(jù)卷容器(Data Volumes Container

  如果你有一些需要持續(xù)更新,并且需要在容器之間共享的數(shù)據(jù),建議創(chuàng)建數(shù)據(jù)卷容器。數(shù)據(jù)卷容器其實就是一個容器,專門用來提供數(shù)據(jù)卷供其他容器掛載。

 ?。?/span>1)首先建立一個數(shù)據(jù)卷容器,用于存放數(shù)據(jù)。

  [root@xx ~]# docker run -d -p 3306:3306 --name store -v /data/ -e MYSQL_ROOT_PASSWORD=123456 mysql

 ?。?/span>2)接著你就能使用--volumes-from選項在其他容器中掛載/data/目錄。

[root@xx ~]# docker run -d -p 3307:3306 --name u1 --volumes-from store -e MYSQL_ROOT_PASSWORD=123456 mysql

[root@xx ~]# docker run -d -p 3308:3306 --name u2 --volumes-from store -e MYSQL_ROOT_PASSWORD=123456 mysql

  此時容器u1u2中都有/data/目錄,并且和容器store中的該目錄共用。即當(dāng)我們在容器u1中新建、更改、刪除文件時,容器u2同樣能得到對應(yīng)的操作。

  注意,這里的數(shù)據(jù)卷容器store并不需要一直處于運行的狀態(tài)。

 ?。?/span>3)刪除數(shù)據(jù)卷容器。這里刪除容器u1、u2store都不會刪除數(shù)據(jù)卷,需要在刪除最后一個容器時使用-v選項來刪除數(shù)據(jù)卷。這里建議刪除每一個時都帶有-v選項。

[root@xx ~]# docker rm -v u1

[root@xx ~]# docker rm -v u2

[root@xx ~]# docker rm -v store

 

 

文章來源:擼代碼,學(xué)知識

您還未登錄,請先登錄

熱門帖子

最新帖子

?