docker数据管理2

时间:2023-02-10 17:39:38

3. 定义数据卷容器: 只是为了共享数据


docker run -itd -v /data/ --name centeos_testv centos bash

/data/ 就是虚拟机内的目录,和宿主机没有任何关系


--name centeos_testv 容器名字


docker:/root# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
192.168.32.150:5000/zjzc_centos6.5_template latest f01c69d919d1 2 days ago 975.4 MB


docker:/root# docker run -itd -v /data/ --name centeos_test02 192.168.32.150:5000/zjzc_centos6.5_template bash
623d933a9f3b42415f9d56bbc214860868895926b3a62092225076321aee1d97

docker:/root# docker run -itd -v /data/ --name centeos_test02 192.168.32.150:5000/zjzc_centos6.5_template bash
Error response from daemon: Conflict. The name "centeos_test02" is already in use by container 623d933a9f3b. You have to delete (or rename) that container to be able to reuse that name.

不能创建同名的容器:

docker:/root# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
623d933a9f3b 192.168.32.150:5000/zjzc_centos6.5_template "bash" 29 seconds ago Up 29 seconds centeos_test02
77bb193db651 192.168.32.150:5000/zjzc_centos6.5_template:latest "bash" 8 minutes ago Up 8 minutes test01
docker:/root# docker exec -it 623d933a9f3b bash
bash-4.1# df -h
Filesystem Size Used Avail Use% Mounted on
rootfs 9.9G 955M 8.5G 10% /
/dev/mapper/docker-253:0-131547-623d933a9f3b42415f9d56bbc214860868895926b3a62092225076321aee1d97
9.9G 955M 8.5G 10% /
tmpfs 3.9G 0 3.9G 0% /dev
shm 64M 0 64M 0% /dev/shm
/dev/mapper/vg00-lv_root
490G 3.6G 462G 1% /data



虚拟机里的data:

bash-4.1# ls -ltr
total 0
-rw-r--r-- 1 root root 0 Mar 21 02:43 b.txt
bash-4.1#


宿主机里的data:
docker:/data# ls -ltr
total 4
-rw-r--r-- 1 root root 12 Mar 21 10:42 aa
-rw-r--r-- 1 root root 0 Mar 21 10:43 b.tx

-v /data/ 指的是容器里的data和宿主机每关系





新的容器使用旧的容器的数据卷:



docker:/root# docker run -itd --name zjzc_z1 --volumes-from zjzc_awp centos bash


docker:/root# docker exec -it 623d933a9f3b bash



docker:/root# docker run -itd -v /data/ --name centeos_test3 --volumes-from centeos_test02 192.168.32.150:5000/zjzc_centos6.5_template bash
4c087d518affba00f846c79d3c5b23e3f38018ec9bbd332d473fdde4fab50ef2



--volumes-from centeos_test02 旧的容器的数据卷


创建新的容器 挂载老的容器的数据卷:


实际应用:



第一个容器使用:
docker run -itd -v /data/:/data1 centos bash // -v 用来指定挂载目录,

后面的容器使用之前的容器数据卷


docker:/root# mkdir /data
docker:/root#

1.创建第一个容器:

docker:/root# docker run -itd -v /data/:/data1 192.168.32.150:5000/zjzc_centos6.5_template bash
f63b2633d146844b4b1c6db90e5dbaf8fb3247ca5b85e608919d4bea612255e0


docker:/root# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f63b2633d146 192.168.32.150:5000/zjzc_centos6.5_template "bash" About a minute ago Up About a minute distracted_perlman
docker:/root# docker rename distracted_perlman zjtest01
docker:/root# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f63b2633d146 192.168.32.150:5000/zjzc_centos6.5_template "bash" About a minute ago Up About a minute zjtest01


2.后面创建的容器使用第一个容器的数据卷:

docker:/root# docker run -itd -v /data1 --name zjtest02 --volumes-from zjtest01 192.168.32.150:5000/zjzc_centos6.5_template bash
8c47da09300ffc1d180746055586b2a1932fa011b398ec5e0a50ff05a6289c1c


docker:/root# docker exec -it 8c47da09300f bash
bash-4.1# cd /data1
bash-4.1# ls
1 2 3 4 a b c