Docker 数据卷容器(实现两个或多个容器间数据同步)

一、多个mysql数据同步

Docker 数据卷容器(实现两个或多个容器间数据同步)

制作一个简单的Dockerfile

FROM centos

VOLUME ["volume1","volume2"]

CMD /bin/bash

构建镜像:

[root@yang docker]# docker build -t docker-volume  .
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM centos
 ---> 300e315adb2f
Step 2/3 : VOLUME ["volume1","volume2"]
 ---> Running in 9e5694642c1f
Removing intermediate container 9e5694642c1f
 ---> 3f15ce6c279b
Step 3/3 : CMD /bin/bash
 ---> Running in 22e9414e3283
Removing intermediate container 22e9414e3283
 ---> 3640a1790e5f
Successfully built 3640a1790e5f
Successfully tagged docker-volume:latest

查看镜像:

[root@yang docker]# docker images
REPOSITORY          TAG                   IMAGE ID            CREATED             SIZE
docker-volume       latest                3640a1790e5f        10 seconds ago      209MB

示例:

1.启动docker1,并进入容器

[root@yang docker]# docker run -it --name docker1 docker-volume
[root@922f59542774 /]# ls -la
total 0
drwxr-xr-x.   1 root root  36 Jan 27 09:20 .
drwxr-xr-x.   1 root root  36 Jan 27 09:20 ..
-rwxr-xr-x.   1 root root   0 Jan 27 09:20 .dockerenv
lrwxrwxrwx.   1 root root   7 Nov  3 15:22 bin -> usr/bin
drwxr-xr-x.   5 root root 360 Jan 27 09:20 dev
drwxr-xr-x.   1 root root  66 Jan 27 09:20 etc
drwxr-xr-x.   2 root root   6 Nov  3 15:22 home
lrwxrwxrwx.   1 root root   7 Nov  3 15:22 lib -> usr/lib
lrwxrwxrwx.   1 root root   9 Nov  3 15:22 lib64 -> usr/lib64
drwx------.   2 root root   6 Dec  4 17:37 lost+found
drwxr-xr-x.   2 root root   6 Nov  3 15:22 media
drwxr-xr-x.   2 root root   6 Nov  3 15:22 mnt
drwxr-xr-x.   2 root root   6 Nov  3 15:22 opt
dr-xr-xr-x. 135 root root   0 Jan 27 09:20 proc
dr-xr-x---.   2 root root 162 Dec  4 17:37 root
drwxr-xr-x.  11 root root 163 Dec  4 17:37 run
lrwxrwxrwx.   1 root root   8 Nov  3 15:22 sbin -> usr/sbin
drwxr-xr-x.   2 root root   6 Nov  3 15:22 srv
dr-xr-xr-x.  13 root root   0 Jan 26 10:35 sys
drwxrwxrwt.   7 root root 145 Dec  4 17:37 tmp
drwxr-xr-x.  12 root root 144 Dec  4 17:37 usr
drwxr-xr-x.  20 root root 262 Dec  4 17:37 var
drwxr-xr-x.   2 root root   6 Jan 27 09:20 volume1
drwxr-xr-x.   2 root root   6 Jan 27 09:20 volume2
[root@922f59542774 /]# cd volume1/
[root@922f59542774 volume1]# ls
[root@922f59542774 volume1]#

此时,进入卷volume1为空。

2.启动docker2,使用命令--volumes-from 挂载到docker1上,并进入容器

[root@yang docker]# docker run -it --name docker2 --volumes-from docker1 docker-volume
[root@bec4008354d7 /]# ls -la
total 0
drwxr-xr-x.   1 root root  36 Jan 27 09:58 .
drwxr-xr-x.   1 root root  36 Jan 27 09:58 ..
-rwxr-xr-x.   1 root root   0 Jan 27 09:58 .dockerenv
lrwxrwxrwx.   1 root root   7 Nov  3 15:22 bin -> usr/bin
drwxr-xr-x.   5 root root 360 Jan 27 09:58 dev
drwxr-xr-x.   1 root root  66 Jan 27 09:58 etc
drwxr-xr-x.   2 root root   6 Nov  3 15:22 home
lrwxrwxrwx.   1 root root   7 Nov  3 15:22 lib -> usr/lib
lrwxrwxrwx.   1 root root   9 Nov  3 15:22 lib64 -> usr/lib64
drwx------.   2 root root   6 Dec  4 17:37 lost+found
drwxr-xr-x.   2 root root   6 Nov  3 15:22 media
drwxr-xr-x.   2 root root   6 Nov  3 15:22 mnt
drwxr-xr-x.   2 root root   6 Nov  3 15:22 opt
dr-xr-xr-x. 132 root root   0 Jan 27 09:58 proc
dr-xr-x---.   2 root root 162 Dec  4 17:37 root
drwxr-xr-x.  11 root root 163 Dec  4 17:37 run
lrwxrwxrwx.   1 root root   8 Nov  3 15:22 sbin -> usr/sbin
drwxr-xr-x.   2 root root   6 Nov  3 15:22 srv
dr-xr-xr-x.  13 root root   0 Jan 26 10:35 sys
drwxrwxrwt.   7 root root 145 Dec  4 17:37 tmp
drwxr-xr-x.  12 root root 144 Dec  4 17:37 usr
drwxr-xr-x.  20 root root 262 Dec  4 17:37 var
drwxr-xr-x.   2 root root   6 Jan 27 09:20 volume1
drwxr-xr-x.   2 root root   6 Jan 27 09:20 volume2
[root@bec4008354d7 /]# cd volume1/
[root@bec4008354d7 volume1]# ls
[root@bec4008354d7 volume1]#

创建完docker2后,在docker2卷volume1中创建一个文件,docker1中的volume1中也会实时同步到这个文件。

测试:

docker2容器内操作:

[root@bec4008354d7 volume1]# touch centos7
[root@bec4008354d7 volume1]# ls
centos7
[root@bec4008354d7 volume1]# 

docker1容器内查看:

[root@922f59542774 /]# cd volume1/
[root@922f59542774 volume1]# ls
[root@922f59542774 volume1]# ls
centos7
[root@922f59542774 volume1]# 

以上即可实现了,两个容器间的数据实时同步!

3.启动docker3,使用命令--volumes-from 挂载到docker1上,并进入容器

[root@yang ~]# docker run -it --name docker3 --volumes-from docker1 docker-volume
[root@aa509e04c7b4 /]# ls -l 
total 0
lrwxrwxrwx.   1 root root   7 Nov  3 15:22 bin -> usr/bin
drwxr-xr-x.   5 root root 360 Jan 27 10:10 dev
drwxr-xr-x.   1 root root  66 Jan 27 10:10 etc
drwxr-xr-x.   2 root root   6 Nov  3 15:22 home
lrwxrwxrwx.   1 root root   7 Nov  3 15:22 lib -> usr/lib
lrwxrwxrwx.   1 root root   9 Nov  3 15:22 lib64 -> usr/lib64
drwx------.   2 root root   6 Dec  4 17:37 lost+found
drwxr-xr-x.   2 root root   6 Nov  3 15:22 media
drwxr-xr-x.   2 root root   6 Nov  3 15:22 mnt
drwxr-xr-x.   2 root root   6 Nov  3 15:22 opt
dr-xr-xr-x. 139 root root   0 Jan 27 10:10 proc
dr-xr-x---.   2 root root 162 Dec  4 17:37 root
drwxr-xr-x.  11 root root 163 Dec  4 17:37 run
lrwxrwxrwx.   1 root root   8 Nov  3 15:22 sbin -> usr/sbin
drwxr-xr-x.   2 root root   6 Nov  3 15:22 srv
dr-xr-xr-x.  13 root root   0 Jan 26 10:35 sys
drwxrwxrwt.   7 root root 145 Dec  4 17:37 tmp
drwxr-xr-x.  12 root root 144 Dec  4 17:37 usr
drwxr-xr-x.  20 root root 262 Dec  4 17:37 var
drwxr-xr-x.   2 root root  21 Jan 27 10:05 volume1
drwxr-xr-x.   2 root root   6 Jan 27 09:20 volume2

docker3容器内查看:

[root@aa509e04c7b4 /]# cd volume1/
[root@aa509e04c7b4 volume1]# ls
centos7
[root@aa509e04c7b4 volume1]# 

同理,docker3也实时同步了docker1中数据

在docker3中创建文件nginx,docker1中也能实现同步。如下:

docker3容器内创建:

[root@aa509e04c7b4 volume1]# touch nginx
[root@aa509e04c7b4 volume1]# ls
centos7  nginx
[root@aa509e04c7b4 volume1]# 

docker1容器内查看:

 

[root@922f59542774 /]# cd volume1/
[root@922f59542774 volume1]# ls
centos7
[root@922f59542774 volume1]# ls
centos7  nginx
[root@922f59542774 volume1]#

 

二、删除数据卷容器,数据是否存在?

1.如果删除了docker1,docker2和docker3中的数据是否存在?

实验:

# 查看现所有容器,docker1已经删除

[root@yang docker]# docker ps -a
CONTAINER ID        IMAGE                                 COMMAND                  CREATED             STATUS                      PORTS                               NAMES
aa509e04c7b4        docker-volume                         "/bin/sh -c /bin/bash"   24 minutes ago      Up 24 minutes                                                   docker3
bec4008354d7        docker-volume                         "/bin/sh -c /bin/bash"   36 minutes ago      Exited (0) 18 minutes ago                                       docker2
00a30275a6db        nginx                                 "/docker-entrypoint.…"   3 hours ago         Up 3 hours                  0.0.0.0:32769->80/tcp               nginx02
328790a86f3b        nginx                                 "/docker-entrypoint.…"   3 hours ago         Up 3 hours                  0.0.0.0:32768->80/tcp               nginx01
8302f9872e0b        mysql:8.0.20                          "docker-entrypoint.s…"   25 hours ago        Up 24 hours                 33060/tcp, 0.0.0.0:3310->3306/tcp   mysql01
b48ebcd4e34a        jenkins/jenkins:2.249.3-lts-centos7   "/sbin/tini -- /usr/…"   6 weeks ago         Up 24 hours                 50000/tcp, 0.0.0.0:8081->8080/tcp   jenkins

# 查看docker2 ,docker3容器内数据,是否存在:

docker2数据:

[root@bec4008354d7 /]# cd volume1/
[root@bec4008354d7 volume1]# ls
centos7  nginx
[root@bec4008354d7 volume1]#

docker3数据:

[root@aa509e04c7b4 volume1]# touch nginx
[root@aa509e04c7b4 volume1]# ls
centos7  nginx
[root@aa509e04c7b4 volume1]# ls
centos7  nginx
[root@aa509e04c7b4 volume1]# 

实时证明,容器docker2,docker3中的数据是存在的,并没有受到任何影响

三、总结

数据卷同步,就相当于拷贝,将数据实时拷贝到其他容器中,

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

但是你持久化到了本地,如果删除了容器,本地数据会一直存在!

 

上一篇:拍美食技巧:如何制造光线通透感效果?


下一篇:二进制安装MySQL 5.6