docker数据卷

docker数据卷笔记

方式一:直接使用命令挂载 -v

docker run -it -v 主机目录:容器内目录   镜像ID   #以交互模式运行并monut挂载数据
#启动后通过docker inspect 容器ID               #查下挂载信息
[root@172-0-0-2 home]# docker inspect de4a5eed57a0 (容器ID)
"Mounts": [                                   #挂载 v 卷
            {
                "Type": "bind",
                "Source": "/home/centos",     #主机内地址
                "Destination": "/home",       #docker容器内的地址
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
          ]  

docker volume #帮助命令

[root@172-0-0-2 ~]# docker volume --help 

Usage:  docker volume COMMAND

Manage volumes

Commands:
  create      Create a volume
  inspect     Display detailed information on one or more volumes
  ls          List volumes
  prune       Remove all unused local volumes
  rm          Remove one or more volumes

Run 'docker volume COMMAND --help' for more information on a command.

具名和匿名挂载

#匿名挂载
-v 容器内路径
docker run -d -P --name nginx01 -v /ect/nginx nginx      #

#查看所有的 volume (卷)的情况
[root@172-0-0-2 ~]# docker volume ls
DRIVER              VOLUME NAME
local               64f9ed9e8cec6235f6fef9b55d1defceeae41e4e140c183088431b737a58a4c7
local               72500843b8258f8bc0118b53db52346e09a682e3713fcaf31b383b741f96a7f6

#这里发现,这种就是匿名挂载,我们在 -v只写了容器内的路径,没有写容器外的路径

#具名挂载
[root@172-0-0-2 ~]# docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx nginx
3d9ed150e71e75a7c35be4bc9002141609485facf9c40111f63a5f66caa1d282
[root@172-0-0-2 ~]# docker volume ls
DRIVER              VOLUME NAME
local               juming-nginx

#通过 -v   卷名:容器内路径
#查看这个卷
[root@172-0-0-2 ~]# docker volume inspect juming-nginx
[
    {
        "CreatedAt": "2020-06-30T06:13:13+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/juming-nginx/_data",
        "Name": "juming-nginx",
        "Options": null,
        "Scope": "local"
    }
]
#所有的docker容器内的卷,没有指定目录的情况下都是在   /var/lib/docker/volumes/xxxx/_data
#我们通过具名挂载可以方便找到我们的一个卷,大多数情况在使用的  具名挂载

判断挂载方式

#如何确定是具名挂载还是匿名挂载,还是指定路径挂载
-v 容器内路径            #匿名挂载
-v 卷名:容器内路径       #具名挂载
-v /宿主机路径:容器内路径   #指定路径挂载

拓展:

#通过 -v 容器内路径:ro   rw 改变读写权限
ro   readonly      #只读
rw   readwrite     #可读可写

#一旦这个设置了容器权限,容器对我们挂载出来的内容就有限定了
docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx:ro  nginx
docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx:rw  nginx

# ro 只要看到ro就说明这个路径只能通过宿主机来操作,容器内部是无法操作的

方式二:dockerfile
mkdir docker-test-volume #创建目录
cd docker-test-volume #切换目录下
vim dockerfile01 #创建dockerfile并编辑脚本命令

#创建dockerfile文件
   #文件中内容 指令(大写) 参数
FROM centos 

VOLUME [“volume01”,“volume02”]

CMD echo----end-----”

CMD /bin/bash
#这里的每行命令就是镜像的一层.

docker build #构建镜像

[root@172-0-0-2 ~]# docker build --help 

Usage:  docker build [OPTIONS] PATH | URL | -

Build an image from a Dockerfile

Options:
      --add-host list           Add a custom host-to-IP mapping (host:ip)
      --build-arg list          Set build-time variables
      --cache-from strings      Images to consider as cache sources
      --cgroup-parent string    Optional parent cgroup for the container
      --compress                Compress the build context using gzip
      --cpu-period int          Limit the CPU CFS (Completely Fair Scheduler) period
      --cpu-quota int           Limit the CPU CFS (Completely Fair Scheduler) quota
  -c, --cpu-shares int          CPU shares (relative weight)
      --cpuset-cpus string      CPUs in which to allow execution (0-3, 0,1)
      --cpuset-mems string      MEMs in which to allow execution (0-3, 0,1)
      --disable-content-trust   Skip image verification (default true)
  -f, --file string             Name of the Dockerfile (Default is 'PATH/Dockerfile')
      --force-rm                Always remove intermediate containers
      --iidfile string          Write the image ID to the file
      --isolation string        Container isolation technology
      --label list              Set metadata for an image
  -m, --memory bytes            Memory limit
      --memory-swap bytes       Swap limit equal to memory plus swap: '-1' to enable unlimited swap
      --network string          Set the networking mode for the RUN instructions during build (default "default")
      --no-cache                Do not use cache when building the image
  -o, --output stringArray      Output destination (format: type=local,dest=path)
      --platform string         Set platform if server is multi-platform capable
      --progress string         Set type of progress output (auto, plain, tty). Use plain to show container
                                output (default "auto")
      --pull                    Always attempt to pull a newer version of the image
  -q, --quiet                   Suppress the build output and print image ID on success
      --rm                      Remove intermediate containers after a successful build (default true)
      --secret stringArray      Secret file to expose to the build (only if BuildKit enabled):
                                id=mysecret,src=/local/secret
      --security-opt strings    Security options
      --shm-size bytes          Size of /dev/shm
      --squash                  Squash newly built layers into a single new layer
      --ssh stringArray         SSH agent socket or keys to expose to the build (only if BuildKit enabled)
                                (format: default|<id>[=<socket>|<key>[,<key>]])
      --stream                  Stream attaches to server to negotiate build context
  -t, --tag list                Name and optionally a tag in the 'name:tag' format
      --target string           Set the target build stage to build.
      --ulimit ulimit           Ulimit options (default [])

生成镜像文件

#语法:docker build -f dockerfile宿主机路径 -t[tag] .当前路径
[root@172-0-0-2 ~]# docker build -f docker-test-volume/dockerfile01 -t ceshicentos:1.0 .
Sending build context to Docker daemon  8.143MB
Step 1/4 : FROM centos
 ---> 831691599b88
Step 2/4 : VOLUME [“volume03”,“volume04”]
 ---> Running in a2b42ce07c58
Removing intermediate container a2b42ce07c58
 ---> 84ce960bedf5
Step 3/4 : CMD echo “----end-----”
 ---> Running in 3bbe095db670
Removing intermediate container 3bbe095db670
 ---> c6b1f5d0ee86
Step 4/4 : CMD /bin/bash
 ---> Running in f8c3b5171afa
Removing intermediate container f8c3b5171afa
 ---> 36a5e743a612
Successfully built 36a5e743a612
Successfully tagged ceshicentos:1.0

运行镜像

[root@172-0-0-2 ~]# docker run -it 36a5e743a612 /bin/bash
[root@bf51a7e8b9d6 /]# ls -l
total 0
drwxr-xr-x.   2 root root   6 Jun 30 17:14 '['$'\342\200\234''volume03'$'\342\200\235\357\274\214\342\200\234''volume04'$'\342\200\235'']'   #匿名挂载
lrwxrwxrwx.   1 root root   7 May 11  2019  bin -> usr/bin
drwxr-xr-x.   5 root root 360 Jun 30 17:14  dev
drwxr-xr-x.   1 root root  66 Jun 30 17:14  etc
drwxr-xr-x.   2 root root   6 May 11  2019  home
lrwxrwxrwx.   1 root root   7 May 11  2019  lib -> usr/lib
lrwxrwxrwx.   1 root root   9 May 11  2019  lib64 -> usr/lib64
drwx------.   2 root root   6 Jun 11 02:35  lost+found
drwxr-xr-x.   2 root root   6 May 11  2019  media
drwxr-xr-x.   2 root root   6 May 11  2019  mnt
drwxr-xr-x.   2 root root   6 May 11  2019  opt
dr-xr-xr-x. 236 root root   0 Jun 30 17:14  proc
dr-xr-x---.   2 root root 162 Jun 11 02:35  root
drwxr-xr-x.  11 root root 163 Jun 11 02:35  run
lrwxrwxrwx.   1 root root   8 May 11  2019  sbin -> usr/sbin
drwxr-xr-x.   2 root root   6 May 11  2019  srv
dr-xr-xr-x.  13 root root   0 Jun 29 10:09  sys
drwxrwxrwt.   7 root root 145 Jun 11 02:35  tmp
drwxr-xr-x.  12 root root 144 Jun 11 02:35  usr
drwxr-xr-x.  20 root root 262 Jun 11 02:35  var

查看卷挂载的路径

[root@172-0-0-2 ~]# docker inspect bf51a7e8b9d6
        "Mounts": [
            {
                "Type": "volume",
                "Name": "38c1c010800291eeb7997dea10edf3792114ab9f83d4bc4ab2a34191f50eefa0",
                "Source": "/var/lib/docker/volumes/38c1c010800291eeb7997dea10edf3792114ab9f83d4bc4ab2a34191f50eefa0/_data",
                "Destination": "[“volume03”,“volume04”]",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],

这个方式使用较多,后期需要自己构建镜像
如果构建镜像的时候没有挂载,要手动镜像挂载使用-v卷名:容器内路径

docker容器卷

#查看镜像
[root@172-0-0-2 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ceshicentos         0.1                 c66c53777502        32 minutes ago      215MB
ceshicentos         1.0                 bfb2bc7a4d5a        About an hour ago   215MB

语法

#运行镜像并改名容器docker01
[root@172-0-0-2 ~]# docker run -it --name docker01 ceshicentos:0.1 
[root@ffa5661edb76 /]# ls -l
total 0
drwxr-xr-x.   2 root root   6 Jun 30 23:17 '['$'\342\200\234''volume01'$'\342\200\235\357\274\214\342\200\234''volume02'$'\342\200\235'']' #数据卷位置
lrwxrwxrwx.   1 root root   7 May 11  2019  bin -> usr/bin
drwxr-xr-x.   5 root root 360 Jun 30 23:17  dev
drwxr-xr-x.   1 root root  66 Jun 30 23:17  etc
drwxr-xr-x.   2 root root   6 May 11  2019  home
lrwxrwxrwx.   1 root root   7 May 11  2019  lib -> usr/lib
lrwxrwxrwx.   1 root root   9 May 11  2019  lib64 -> usr/lib64
drwx------.   2 root root   6 Jun 11 02:35  lost+found
drwxr-xr-x.   2 root root   6 May 11  2019  media
drwxr-xr-x.   2 root root   6 May 11  2019  mnt
drwxr-xr-x.   2 root root   6 May 11  2019  opt
dr-xr-xr-x. 229 root root   0 Jun 30 23:17  proc
dr-xr-x---.   2 root root 162 Jun 11 02:35  root
drwxr-xr-x.  11 root root 163 Jun 11 02:35  run
lrwxrwxrwx.   1 root root   8 May 11  2019  sbin -> usr/sbin
drwxr-xr-x.   2 root root   6 May 11  2019  srv
dr-xr-xr-x.  13 root root   0 Jun 29 10:09  sys
drwxrwxrwt.   7 root root 145 Jun 11 02:35  tmp
drwxr-xr-x.  12 root root 144 Jun 11 02:35  usr
drwxr-xr-x.  20 root root 262 Jun 11 02:35  var

语法

#运行镜像并改名容器Ubuntu02
[root@172-0-0-2 ~]# docker run -it --name ubuntu02 --volumes-from docker01 ubuntu  #将Ubuntu镜像启动并继承于docker01数据卷容器
root@2a15eaf2869b:/# ls -l
total 0
drwxr-xr-x.   2 root root   6 Jun 30 23:17 '['$'\342\200\234''volume01'$'\342\200\235\357\274\214\342\200\234''volume02'$'\342\200\235'']' #继承于docker01容器卷
lrwxrwxrwx.   1 root root   7 Jun  6 01:18  bin -> usr/bin
drwxr-xr-x.   2 root root   6 Apr 15 11:09  boot
drwxr-xr-x.   5 root root 360 Jun 30 23:29  dev
drwxr-xr-x.   1 root root  66 Jun 30 23:29  etc
drwxr-xr-x.   2 root root   6 Apr 15 11:09  home
lrwxrwxrwx.   1 root root   7 Jun  6 01:18  lib -> usr/lib
lrwxrwxrwx.   1 root root   9 Jun  6 01:18  lib32 -> usr/lib32
lrwxrwxrwx.   1 root root   9 Jun  6 01:18  lib64 -> usr/lib64
lrwxrwxrwx.   1 root root  10 Jun  6 01:18  libx32 -> usr/libx32
drwxr-xr-x.   2 root root   6 Jun  6 01:18  media
drwxr-xr-x.   2 root root   6 Jun  6 01:18  mnt
drwxr-xr-x.   2 root root   6 Jun  6 01:18  opt
dr-xr-xr-x. 233 root root   0 Jun 30 23:29  proc
drwx------.   2 root root  37 Jun  6 01:21  root
drwxr-xr-x.   1 root root  21 Jun 17 01:20  run
lrwxrwxrwx.   1 root root   8 Jun  6 01:18  sbin -> usr/sbin
drwxr-xr-x.   2 root root   6 Jun  6 01:18  srv
dr-xr-xr-x.  13 root root   0 Jun 29 10:09  sys
drwxrwxrwt.   2 root root   6 Jun  6 01:21  tmp
drwxr-xr-x.   1 root root  18 Jun  6 01:18  usr
drwxr-xr-x.   1 root root  17 Jun  6 01:21  var

结论:
容器之间配置信息的传递,数据卷容器的生命周期一直持续到没有容器使用为止。但是
一旦持久化到本地,这个时候,本地的数据是不会删除

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值