Centos7 docker基础4-数据处理

容器中管理数据主要有两种方式:
数据卷
数据卷容器

一、数据卷

在容器内创建一个数据卷:
在用docker run 命令的时候,使用-v 标记可以在容器内创建一个数据卷 支持多次使用-v参数
(可以想像成mount)

下载个webapp镜像

[root@centos7 ~]# docker pull docker.io/training/webapp
Using default tag: latest
Trying to pull repository docker.io/training/webapp ... 
latest: Pulling from docker.io/training/webapp
e190868d63f8: Pull complete 
909cd34c6fd7: Pull complete 
0b9bfabab7c1: Pull complete 
a3ed95caeb02: Pull complete 
10bbbc0fc0ff: Pull complete 
fca59b508e9f: Pull complete 
e7ae2541b15b: Pull complete 
9dd97ef58ce9: Pull complete 
a4c1b0cb7af7: Pull complete 
Digest: sha256:06e9c1983bd6d5db5fba376ccd63bfa529e8d02f23d5079b8f74a616308fb11d
Status: Downloaded newer image for docker.io/training/webapp:latest
[root@centos7 ~]# 
docker run -d -P --name web -v /webapp docker.io/training/webapp python app.py
-P:是允许外部访问容器需要暴露的端口
--name:
 -v /webapp :创建一个数据卷挂载到容器的/webapp目录

运行结果:

[root@centos7 ~]# docker run -d -P --name web -v /webapp docker.io/training/webapp python app.py
9e557abaabd4d50ab1c551d74390d83f9303fa2f6bf5483ac1dfe78626f15930
[root@centos7 ~]# docker ps -a
CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS              PORTS                     NAMES
9e557abaabd4        docker.io/training/webapp   "python app.py"          6 seconds ago       Up 5 seconds        0.0.0.0:32768->5000/tcp   web
[root@centos7 ~]# netstat -tuplan | grep 5000 #为什么5000没有呢????
[root@centos7 ~]# netstat -tuplan | grep 327
tcp6       0      0 :::32769                :::*                    LISTEN      11464/docker-proxy- 
[root@centos7 ~]# docker ps -a
CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS                     PORTS                     NAMES
9e557abaabd4        docker.io/training/webapp   "python app.py"          4 hours ago         Up 6 minutes               0.0.0.0:32769->5000/tcp   web
140c9a4ac310        registry                    "/entrypoint.sh /e..."   29 hours ago        Exited (2) 6 minutes ago                             awesome_booth
[root@centos7 ~]# 

那个docker ps -a 输入的5000端口到底是干什么用的?
答:5000是容器暴露的端口 32769是本地的映射端口

docker-4-1.png

现在我们进入容器中的/webapp中,去生成一个普通文件,看他最终是存放在本机的哪个目录下

[root@centos7 ~]# docker exec -it 9e557abaabd4 /bin/bash
root@9e557abaabd4:/opt/webapp# pwd
/opt/webapp
root@9e557abaabd4:/opt/webapp# ll
total 20
drwxr-xr-x 2 root root  94 May 15  2015 ./
drwxr-xr-x 1 root root  20 May 15  2015 ../
-rw-r--r-- 1 root root  11 May 15  2015 .gitignore
-rw-r--r-- 1 root root  19 May 15  2015 Procfile
-rw-r--r-- 1 root root 347 May 15  2015 app.py
-rw-r--r-- 1 root root  41 May 15  2015 requirements.txt
-rw-r--r-- 1 root root 285 May 15  2015 tests.py
root@9e557abaabd4:/# cd /webapp/
root@9e557abaabd4:/webapp# ll
total 0
drwxr-xr-x 2 root root  6 Jul 20 16:22 ./
drwxr-xr-x 1 root root 31 Jul 20 16:22 ../

root@9e557abaabd4:/webapp# touch testwebapp.txt
root@9e557abaabd4:/webapp# ll
total 0
drwxr-xr-x 2 root root 28 Jul 20 20:54 ./
drwxr-xr-x 1 root root 31 Jul 20 16:22 ../
-rw-r--r-- 1 root root  0 Jul 20 20:54 testwebapp.txt
root@9e557abaabd4:/webapp# exit
exit
[root@centos7 ~]# find / -name testwebapp.txt
/var/lib/docker/volumes/b38c83c6d45aeb1d14bebedb18ac6f697821066e3da02a5db2100fec74faab30/_data/testwebapp.txt 
#生成在这个里。原来这就是docker的数据卷默认生成的位置
[root@centos7 ~]# 

挂载一个主机目录作为数据卷

  • 如果主机目录不存在,docker会自己创建
[root@centos7 ~]# ll -d /data
ls: 无法访问/data: 没有那个文件或目录
先清除之前的容器
[root@centos7 ~]# docker ps -a
CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS                        PORTS               NAMES
9e557abaabd4        docker.io/training/webapp   "python app.py"          4 hours ago         Exited (137) 13 seconds ago                       web
140c9a4ac310        registry                    "/entrypoint.sh /e..."   30 hours ago        Exited (2) 29 minutes ago                         awesome_booth
[root@centos7 ~]# docker rm 9e557abaabd4
9e557abaabd4
docker run -d -P --name web -v /data/webapp:/webapp  docker.io/training/webapp  python app.py
# -v  本机上目录:容器目录

运行结果:

[root@centos7 ~]# docker run -d -P --name web -v /data/webapp:/webapp  docker.io/training/webapp  python app.py    
411315eaabffd80316874401a1e14f6dbb1e28c2a479354752ec55d0e2587b11
[root@centos7 ~]# docker ps -a
CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS                      PORTS                     NAMES
411315eaabff        docker.io/training/webapp   "python app.py"          4 seconds ago       Up 4 seconds                0.0.0.0:32774->5000/tcp   web
140c9a4ac310        registry                    "/entrypoint.sh /e..."   30 hours ago        Exited (2) 41 minutes ago                             awesome_booth
#如果-name web己存在,再次创建同名的会报错
[root@centos7 ~]# docker run -d -P --name web -v /data/webapp:/webapp  docker.io/training/webapp  python app.py
/usr/bin/docker-current: Error response from daemon: Conflict. The container name "/web" is already in use by container 411315eaabffd80316874401a1e14f6dbb1e28c2a479354752ec55d0e2587b11. You have to remove (or rename) that container to be able to reuse that name..
See '/usr/bin/docker-current run --help'.
[root@centos7 ~]# 
如果-name 不同,就可以创建
[root@centos7 ~]# docker run -d -P --name web2 -v /data/webapp:/webapp  docker.io/training/webapp  python app.py
368636480f93cb39a6529cbd8a763a31950753ecf0df5939ac2790b518b33991
[root@centos7 ~]# docker ps -a
CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS                      PORTS                     NAMES
368636480f93        docker.io/training/webapp   "python app.py"          6 seconds ago       Up 6 seconds                0.0.0.0:32775->5000/tcp   web2
411315eaabff        docker.io/training/webapp   "python app.py"          4 minutes ago       Up 4 minutes                0.0.0.0:32774->5000/tcp   web
140c9a4ac310        registry                    "/entrypoint.sh /e..."   30 hours ago        Exited (2) 45 minutes ago                             awesome_booth
[root@centos7 ~]# 

看下两个容器上如果生成文件是否 都可以看到

[root@centos7 ~]# docker exec -ti 368636480f93 /bin/bash
root@368636480f93:/opt/webapp# cd /webapp/
root@368636480f93:/webapp# ll
total 0
drwxr-xr-x 2 root root  6 Jul 20 21:07 ./
drwxr-xr-x 1 root root 31 Jul 20 21:18 ../
root@368636480f93:/webapp# touch 368636480f93.txt
root@368636480f93:/webapp# exit
exit
[root@centos7 ~]# docker exec -ti 411315eaabff  /bin/bash
root@411315eaabff:/opt/webapp# cd /webapp/
root@411315eaabff:/webapp# ll
total 0
drwxr-xr-x 2 root root 30 Jul 20 21:20 ./
drwxr-xr-x 1 root root 31 Jul 20 21:14 ../
-rw-r--r-- 1 root root  0 Jul 20 21:20 368636480f93.txt  #可以看到容器ID:368636480f93生成的文件
root@411315eaabff:/webapp# touch 411315eaabff.txt
root@411315eaabff:/webapp# ll
total 0
drwxr-xr-x 2 root root 54 Jul 20 21:21 ./
drwxr-xr-x 1 root root 31 Jul 20 21:14 ../
-rw-r--r-- 1 root root  0 Jul 20 21:20 368636480f93.txt
-rw-r--r-- 1 root root  0 Jul 20 21:21 411315eaabff.txt
root@411315eaabff:/webapp# exit
exit

#在本地的目录上可以看到两个容器生成的文件

[root@centos7 ~]# ll /data/webapp/   
总用量 0
-rw-r--r-- 1 root root 0 7月  21 05:20 368636480f93.txt
-rw-r--r-- 1 root root 0 7月  21 05:21 411315eaabff.txt
[root@centos7 ~]# 

#Docker挂载数据卷的默认权限是读写(rw)
可以指定权限

[root@centos7 ~]# docker run -d -P --name web2 -v /data/webapp:/webapp:ro  docker.io/training/webapp  python app.py

但不能指定rwx,听说可以用:–privileged=true 使container内的root拥有真正的root权限 没有尝试过,后面再来

  • -v 也可以从主机直接挂载文件到容器中
docker run -d -P --privileged=true --name web4 -v ~/.bash_history:/.bash_history  docker.io/training/webapp  python app.py
#教程说如果文件的inode改变,会引起docker报错
#所以还是挂载文件目录 最好

运行结果:

[root@centos7 ~]# docker run -d -P --privileged=true --name web4 -v ~/.bash_history:/.bash_history  docker.io/training/webapp  python app.py
d852b32ee28c8d35e1e71cdd95ad2c0d898c99f292d70405d096ac9cb24c024b
[root@centos7 ~]# docker ps -a
CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS                   PORTS                     NAMES
d852b32ee28c        docker.io/training/webapp   "python app.py"          4 seconds ago       Up 4 seconds             0.0.0.0:32777->5000/tcp   web4
e8be34033a6b        docker.io/training/webapp   "python app.py"          43 minutes ago      Up 43 minutes            0.0.0.0:32776->5000/tcp   web3
368636480f93        docker.io/training/webapp   "python app.py"          About an hour ago   Up About an hour         0.0.0.0:32775->5000/tcp   web2
411315eaabff        docker.io/training/webapp   "python app.py"          About an hour ago   Up About an hour         0.0.0.0:32774->5000/tcp   web
140c9a4ac310        registry                    "/entrypoint.sh /e..."   32 hours ago        Exited (2) 2 hours ago                             awesome_booth
[root@centos7 ~]# tail ~/.bash_history   #本机的文件内容
docker logs bd14a6ab787b
ping docker001
docker ps
docker stop
docker stop bd14a6ab787b
docker ps
docker ps -a
docker ps -a -q
ll /
exit
[root@centos7 ~]# docker exec -it d852b32ee28c /bin/bash #进入容器查看文件
root@d852b32ee28c:/opt/webapp# cd ~
root@d852b32ee28c:~# tail /.bash_history 
docker logs bd14a6ab787b
ping docker001
docker ps
docker stop
docker stop bd14a6ab787b
docker ps
docker ps -a
docker ps -a -q
ll /
exit
root@d852b32ee28c:~# 

二、数据卷容器

数据卷容器:其实就是一个普通的容器,专门用它提供数据卷供给其他窗口挂载使用
使用 –volumes-from 参数时 , 不用“提供容器”自身保持运行状态的
可以多次使用–volumes-from

操作前清理之前的容器,便于查看:

[root@centos7 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@centos7 ~]# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
centos1                       v1.0                f1420ae8ab97        32 hours ago        202 MB
ubuntu                        14.04               40446eeefc1a        40 hours ago        215 MB
docker.io/rdocker8/test       latest              8e861c8118a3        41 hours ago        64.2 MB
192.168.174.102:5000/ubuntu   latest              4c108a37151f        4 weeks ago         64.2 MB
docker.io/ubuntu              latest              4c108a37151f        4 weeks ago         64.2 MB
docker.io/centos              latest              9f38484d220f        4 months ago        202 MB
docker.io/registry            latest              f32a97de94e1        4 months ago        25.8 MB
docker.io/training/webapp     latest              6fae60ef3446        4 years ago         349 MB
[root@centos7 ~]# docker ps -a #清理之前的容器
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

#创建一个dbdata的容器 且带有/dbdata数据卷

[root@centos7 ~]# docker run -it -v /dbdata --name dbdata docker.io/centos 
[root@a20a8f71b587 /]# ll -d /dbdata/ 
drwxr-xr-x 2 root root 6 Jul 20 23:15 /dbdata/
[root@a20a8f71b587 /]# exit
exit

#创建db1、db2两个容器,并从dbdata容器挂载数据卷

#使用 –volumes-from

[root@centos7 ~]# docker run -it --volumes-from dbdata --name db1 docker.io/centos             
[root@75238caf083e /]# exit
exit
[root@centos7 ~]# docker run -it --volumes-from dbdata --name db2 docker.io/centos
[root@a0183698be06 /]# exit
exit

查看容器:

[root@centos7 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                      PORTS               NAMES
a0183698be06        docker.io/centos    "/bin/bash"         16 seconds ago       Exited (0) 5 seconds ago                        db2
75238caf083e        docker.io/centos    "/bin/bash"         32 seconds ago       Exited (0) 20 seconds ago                       db1
a20a8f71b587        docker.io/centos    "/bin/bash"         About a minute ago   Exited (0) 58 seconds ago                       dbdata
[root@centos7 ~]# 
  • 查看一个容器是挂载了哪些数据卷
docker inspect 9557a29349ed  | grep Volumes -A 2
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
--
            "Volumes": {
                "/dbdata": {}   #看这个
            },
[root@centos7 ~]# 

测试下三个容器中的/dbdata是否共享:
在dbdata中新建一个普通文件

[root@centos7 ~]# docker exec -it a20a8f71b587 /bin/bash
Error response from daemon: Container a20a8f71b5879212a67d00976621b15f6ae71804f1768301e78b336262391748 is not running 
#刚才没有加后台参数 所以exit就退出,开启下
#也说明了使用 --volumes-from 参数是不用“提供容器”自身保持运行状态的
[root@centos7 ~]# docker start a20a8f71b587
a20a8f71b587
[root@centos7 ~]# docker exec -it a20a8f71b587 /bin/bash
[root@a20a8f71b587 /]# touch /dbdata/name_dbdata.txt
[root@a20a8f71b587 /]# exit

在db1查看:

[root@centos7 ~]# docker exec -it 75238caf083e /bin/bash
[root@75238caf083e /]# ll /dbdata/
total 0
-rw-r--r-- 1 root root 0 Jul 20 23:23 name_dbdata.txt
[root@75238caf083e /]# touch /dbdata/name_db1.txt   #再新建一个文件 看db2能不能都看到
[root@75238caf083e /]# ll /dbdata/
total 0
-rw-r--r-- 1 root root 0 Jul 20 23:33 name_db1.txt
-rw-r--r-- 1 root root 0 Jul 20 23:23 name_dbdata.txt
[root@75238caf083e /]# 

在db2查看:

[root@centos7 ~]# docker start a0183698be06 
a0183698be06
[root@centos7 ~]# docker exec -ti  a0183698be06  /bin/bash  
[root@a0183698be06 /]# ll /dbdata/
total 0
-rw-r--r-- 1 root root 0 Jul 20 23:33 name_db1.txt
-rw-r--r-- 1 root root 0 Jul 20 23:23 name_dbdata.txt

#都可以看到 说明共享了

问题:如果对己存在的容器 再次挂载数据卷呢?
答:????????

删除数据卷容器
如果删除了挂载的容器(包括dbdata、db1、db2),数据卷并没有自动删除;
如果要删除一个数据卷,必须在删除最后一个还挂载着它的容器时,显示使用dock rm -v 命令来指定同时删除关联的容器

#-v: 删除与容器关联的卷

[root@centos7 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
a0183698be06        docker.io/centos    "/bin/bash"         17 hours ago        Up 17 hours                             db2
75238caf083e        docker.io/centos    "/bin/bash"         17 hours ago        Up 17 hours                             db1
a20a8f71b587        docker.io/centos    "/bin/bash"         18 hours ago        Up 17 hours                             dbdata
#停掉所有容器
[root@centos7 ~]# docker stop a0183698be06 75238caf083e a20a8f71b587
a0183698be06
75238caf083e
a20a8f71b587

#删除dbdata容器

[root@centos7 ~]# docker rm a20a8f71b587 
a20a8f71b587

#开启db 且看下数据卷dbdata是否还在

[root@centos7 ~]# docker start 75238caf083e
75238caf083e
[root@centos7 ~]# docker exec -it 75238caf083e  /bin/bash   
[root@75238caf083e /]# cd /dbdata/
[root@75238caf083e dbdata]# ll
total 0
-rw-r--r-- 1 root root 0 Jul 20 23:33 name_db1.txt
-rw-r--r-- 1 root root 0 Jul 20 23:23 name_dbdata.txt

#说明还是存在的

[root@75238caf083e dbdata]# exit
exit
#删除db2容器
[root@centos7 ~]# docker rm a0183698be06
a0183698be06

#查找数据卷在本机上的存储路径

[root@centos7 ~]# find / -name name_dbdata.txt
/var/lib/docker/volumes/fa138eac7e5f7519052a7b47e8158938493424c50f5ef62f84537f556cf10818/_data/name_dbdata.txt

#关闭db1 且删除db1

[root@centos7 ~]# docker stop 75238caf083e
[root@centos7 ~]# docker rm -v 75238caf083e
75238caf083e

#再次查找文件,发现己没有了。说明数据卷己删除了。

[root@centos7 ~]# find / -name name_dbdata.txt
[root@centos7 ~]# 

三、备份数据卷

新建一个dbdata容器

[root@centos7 ~]# docker run -it -d -v /dbdata --name dbdata docker.io/centos

#生成一个backup.txt文件 来验证等下的备份是否成功

[root@centos7 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
9557a29349ed        docker.io/centos    "/bin/bash"         10 minutes ago      Exited (0) 10 minutes ago                       dbdata
[root@centos7 ~]# docker start 9557a29349ed
9557a29349ed
[root@centos7 ~]# docker exec -it  9557a29349ed /bin/bash  
[root@9557a29349ed /]# touch /dbdata/backup.txt
[root@9557a29349ed /]# exit
exit
[root@centos7 ~]# 
docker run --volumes-from dbdata -v $(pwd):/backup --name worker centos tar -zcvf /backup/backup.tar /dbdata
-v $(pwd):/backup:将当前路径挂载到/backup
tar -zcvf /backup/backup.tar /dbdata : 对/dbdata 进行打包 且放在/backup下

运行结果:

[root@9557a29349ed /]# exit
exit
[root@centos7 ~]# docker run --volumes-from dbdata -v $(pwd):/backup --name worker centos tar -zcvf /backup/backup.tar /dbdata
/dbdata/
/dbdata/backup.txt
tar: Removing leading `/' from member names
[root@centos7 ~]# ll backup.tar 
-rw-r--r-- 1 root root 147 7月  22 02:04 backup.tar #在本机当前目录就可以看到了
[root@centos7 ~]# 

四、恢复数据卷(这个我不能理解)?

为什么我这次新建的容器都是退出的状态?
为什么执行tar就会恢复到/dbdata吗?

[root@centos7 ~]# docker run -v /dbdata --name  dbdata2 centos /bin/bash
[root@centos7 ~]# docker ps a
"docker ps" accepts no argument(s).
See 'docker ps --help'.

Usage:  docker ps [OPTIONS]

List containers
[root@centos7 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
89d8e5d87dcd        centos              "/bin/bash"              9 seconds ago       Exited (0) 8 seconds ago                        dbdata2
175beafbeed0        centos              "tar -zcvf /backup..."   21 minutes ago      Exited (0) 21 minutes ago                       worker
9557a29349ed        docker.io/centos    "/bin/bash"              36 minutes ago      Up 23 minutes                                   dbdata
[root@centos7 ~]# docker run --volumes-from dbdata2 -v $(pwd):/backup busybox tar xvf /backup/backup.tar
Unable to find image 'busybox:latest' locally
Trying to pull repository docker.io/library/busybox ... 
latest: Pulling from docker.io/library/busybox
ee153a04d683: Pull complete 
Digest: sha256:9f1003c480699be56815db0f8146ad2e22efea85129b5b5983d0e0fb52d9ab70
Status: Downloaded newer image for docker.io/busybox:latest
dbdata/
dbdata/backup.txt

教程参考于:http://www.bdkyr.com/

# docker
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×