0%

Docker|3.文件、数据共享

文件拷贝

docker cp 容器ID:/文件路径/文件名称 本地文件路径

**docker cp 本地文件路径 容器ID:/文件路径/文件名称 **

1
2
3
4
5
6
#从容器内拷贝文件到本地
[root@localhost ~]# docker cp cd7a272e8786:/etc/passwd /tmp
[root@localhost ~]# head -3 /tmp/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
1
2
3
4
5
#将本地文件拷贝到容器内
[root@localhost ~]# echo "123" > file1
[root@localhost ~]# docker cp file1 cd7a272e8786:/tmp
[root@localhost ~]# docker exec -it cd7a272e8786 cat /tmp/file1
123

数据卷

-v命令实现

docker run -it -v /宿主机的绝对路径目录:/容器内目录 镜像名 #如果不能写,需要添加–privileged=true

docker run -it -v /宿主机的绝对路径目录:/容器内目录:ro 镜像名 #容器内文件只读

1
2
3
4
5
6
7
8
9
[root@localhost ~]# docker run --name centos0211 -it -v /HostDataVolume:/ContainerDataVolume centos
[root@cde93366c404 /]# cd /ContainerDataVolume/
[root@cde93366c404 ContainerDataVolume]# ls
[root@cde93366c404 ContainerDataVolume]# echo "123" > file1
[root@cde93366c404 ContainerDataVolume]# exit
exit
[root@localhost ~]# cat /HostDataVolume/file1
123

dockerfile方式数据卷

1
2
3
4
5
6
vim /root/mydocker/Dockerfile
#volume test
FROM centos
VOLUME ["/DataVolumeContainer1","/DataVolumeContainer2"]
CMD echo "finished, ------success!"
CMD /bin/bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@localhost mydocker]# docker build -f /root/mydocker/Dockerfile -t test/centos .
Sending build context to Docker daemon 2.048kB
Step 1/4 : FROM centos
---> 470671670cac
Step 2/4 : VOLUME ["/DataVolumeContainer1","/DataVolumeContainer2"]
---> Running in ad5cdf37264a
Removing intermediate container ad5cdf37264a
---> 3e834cfa3923
Step 3/4 : CMD echo "finished, ------success!"
---> Running in 033f927a7220
Removing intermediate container 033f927a7220
---> d1b3cc53d482
Step 4/4 : CMD /bin/bash
---> Running in 47ebcfb99158
Removing intermediate container 47ebcfb99158
---> c8ed8caf62e2
Successfully built c8ed8caf62e2
Successfully tagged test/centos:latest
1
2
3
[root@localhost mydocker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test/centos latest c8ed8caf62e2 10 seconds ago 237MB
1
2
3
4
5
6
[root@localhost mydocker]# docker run -it test/centos
[root@7f54ea649e73 /]# ls -l
total 16
drwxr-xr-x 2 root root 6 Feb 11 23:18 DataVolumeContainer1
drwxr-xr-x 2 root root 6 Feb 11 23:18 DataVolumeContainer2
……
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
[root@localhost ~]# docker inspect 7f54ea649e73
……
……
"Mounts": [
{
"Type": "volume",
"Name": "db383db0ed0f634dca3b88ac7b1b5eb7aaf1eb45aa5e198c1dbb672717cbadc4",
"Source": "/var/lib/docker/volumes/db383db0ed0f634dca3b88ac7b1b5eb7aaf1eb45aa5e198c1dbb672717cbadc4/_data",
"Destination": "/DataVolumeContainer1",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
},
{
"Type": "volume",
"Name": "ed238e4c4a3bf80274f6bd6dd5d4a1aa00020e081a38d9ae668567d9a9301344",
"Source": "/var/lib/docker/volumes/ed238e4c4a3bf80274f6bd6dd5d4a1aa00020e081a38d9ae668567d9a9301344/_data",
"Destination": "/DataVolumeContainer2",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
……
……
1
2
3
4
[root@7f54ea649e73 /]# echo "123" > /DataVolumeContainer1/file1
[root@localhost ~]# cat /var/lib/docker/volumes/db383db0ed0f634dca3b88ac7b1b5eb7aaf1eb45aa5e198c1dbb672717cbadc4/_data/file1
123

数据卷容器

命名的容器挂载数据卷,其他容器通过挂载这个(父容器)实现数据共享,挂载数据卷的容器,称之为数据卷容器。

先启动一个父容器

1
2
3
4
5
[root@localhost ~]# docker run -it --name dc01 test/centos

[root@fe649066e76e /]# cd DataVolumeContainer1/

[root@fe649066e76e DataVolumeContainer1]# touch add_dc01.txt

新建dc02/dc03容器,继承dc01

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#新建dc02

[root@localhost ~]# docker run -it --name dc02 --volumes-from dc01 test/centos

[root@c14197144fe7 /]# cd /DataVolumeContainer1/

[root@c14197144fe7 DataVolumeContainer1]# touch add_dc02.txt

#新建dc03

[root@localhost ~]# docker run -it --name dc03 --volumes-from dc01 test/centos

[root@c409dacf9084 /]# cd /DataVolumeContainer1/

[root@c409dacf9084 DataVolumeContainer1]# touch add_dc03.txt

在dc01上查看数据

1
2
3
4
5
6
7
8
9
10
11
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c409dacf9084 test/centos "/bin/sh -c /bin/bash" 4 minutes ago Up 4 minutes dc03
c14197144fe7 test/centos "/bin/sh -c /bin/bash" 4 minutes ago Up 4 minutes dc02
fe649066e76e test/centos "/bin/sh -c /bin/bash" 9 minutes ago Up 9 minutes dc01
[root@localhost ~]# docker attach fe649066e76e
[root@fe649066e76e DataVolumeContainer1]# ls -l
total 0
-rw-r--r-- 1 root root 0 Feb 12 00:02 add_dc01.txt
-rw-r--r-- 1 root root 0 Feb 12 00:09 add_dc02.txt
-rw-r--r-- 1 root root 0 Feb 12 00:10 add_dc03.txt

删除dc01,在dc02上添加数据,在dc03上查看

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c409dacf9084 test/centos "/bin/sh -c /bin/bash" 5 minutes ago Up 5 minutes dc03
c14197144fe7 test/centos "/bin/sh -c /bin/bash" 5 minutes ago Up 5 minutes dc02
fe649066e76e test/centos "/bin/sh -c /bin/bash" 10 minutes ago Up 10 minutes dc01
[root@localhost ~]# docker rm -f fe649066e76e
fe649066e76e
[root@localhost ~]# docker attach c14197144fe7
[root@c14197144fe7 DataVolumeContainer1]# ls -l
total 0
-rw-r--r-- 1 root root 0 Feb 12 00:02 add_dc01.txt
-rw-r--r-- 1 root root 0 Feb 12 00:09 add_dc02.txt
-rw-r--r-- 1 root root 0 Feb 12 00:10 add_dc03.txt
[root@c14197144fe7 DataVolumeContainer1]# touch update_dc02.txt

[root@localhost ~]# docker attach dc03
[root@c409dacf9084 DataVolumeContainer1]# ls -l
total 0
-rw-r--r-- 1 root root 0 Feb 12 00:02 add_dc01.txt
-rw-r--r-- 1 root root 0 Feb 12 00:09 add_dc02.txt
-rw-r--r-- 1 root root 0 Feb 12 00:10 add_dc03.txt
-rw-r--r-- 1 root root 0 Feb 12 00:13 update_dc02.txt

删除dc02,测试dc03数据是否可以访问

1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost ~]# docker rm -f dc02
dc02
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c409dacf9084 test/centos "/bin/sh -c /bin/bash" 10 minutes ago Up 10 minutes dc03
[root@localhost ~]# docker attach dc03
[root@c409dacf9084 DataVolumeContainer1]# ls -l
total 0
-rw-r--r-- 1 root root 0 Feb 12 00:02 add_dc01.txt
-rw-r--r-- 1 root root 0 Feb 12 00:09 add_dc02.txt
-rw-r--r-- 1 root root 0 Feb 12 00:10 add_dc03.txt
-rw-r--r-- 1 root root 0 Feb 12 00:13 update_dc02.txt

新建dc04继承dc03,然后删除dc03

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@localhost ~]# docker run -it --name dc04 --volumes-from dc03 test/centos
[root@localhost ~]# docker rm -f dc03
dc03
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5156e4538e53 test/centos "/bin/sh -c /bin/bash" 59 seconds ago Up 58 seconds dc04
[root@localhost ~]# docker attach dc04
[root@5156e4538e53 DataVolumeContainer1]# cd /DataVolumeContainer1/
[root@5156e4538e53 DataVolumeContainer1]# ls -l
total 0
-rw-r--r-- 1 root root 0 Feb 12 00:02 add_dc01.txt
-rw-r--r-- 1 root root 0 Feb 12 00:09 add_dc02.txt
-rw-r--r-- 1 root root 0 Feb 12 00:10 add_dc03.txt
-rw-r--r-- 1 root root 0 Feb 12 00:13 update_dc02.txt

结论:容器之间配置信息的传递,数据卷的生命周期一直持续到没有容器使用它为止