docker容器卷

命令-v来挂载

使用命令来挂载 -v
docker run -it -v 宿主机目录:容器内的目录 镜像名(如:tomcat)
测试:
#挂载容器后创文件,宿主机上的数据同步
[root@localhost ~]# docker run -it -v /home/ceshi:/home tomcat /bin/bash
root@d0248b1e46fe:/usr/local/tomcat# cd /home/
root@d0248b1e46fe:/home# ls
root@d0248b1e46fe:/home# touch test.txt
root@d0248b1e46fe:/home# ls
test.txt
root@d0248b1e46fe:/home# exit
exit
[root@localhost ~]# cd /home/
[root@localhost home]# ls
ceshi  sun
[root@localhost home]# cd ceshi/
[root@localhost ceshi]# ls
test.txt
#停止容器后宿主机创文件,容器内的数据依旧同步
[root@localhost ceshi]# vim sun.txt
[root@localhost ceshi]# cat sun.txt 
hello linux
[root@localhost ceshi]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                      PORTS     NAMES
d0248b1e46fe   tomcat    "/bin/bash"   43 seconds ago   Exited (0) 30 seconds ago             serene_panini
[root@localhost ceshi]# docker start d0248b1e46fe
d0248b1e46fe
[root@localhost ceshi]# docker attach d0248b1e46fe
root@d0248b1e46fe:/usr/local/tomcat# cd /home/
root@d0248b1e46fe:/home# ls
sun.txt  test.txt
root@d0248b1e46fe:/home# cat sun.txt 
hello linux
root@d0248b1e46fe:/home#  

匿名挂载 具名挂载 指定路径挂载

-v 容器内路径 #匿名挂载

-v 卷名:容器内路径 #具名挂载

#匿名挂载  只写了宿主机目录路径,没写容器内目录路径
[root@localhost ~]# docker run -d --name nginx01 -v /etc/nginx nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
f7a1c6dad281: Pull complete 
4d3e1d15534c: Pull complete 
9ebb164bd1d8: Pull complete 
59baa8b00c3c: Pull complete 
a41ae70ab6b4: Pull complete 
e3908122b958: Pull complete 
Digest: sha256:1c13bc6de5dfca749c377974146ac05256791ca2fe1979fc8e8278bf0121d285
Status: Downloaded newer image for nginx:latest
9c55db51920f706e52a53ad60067013d49ec15e726aa4ffbf7babdbebc1da76c
#查看所有volume的情况
[root@localhost ~]# docker volume ls
DRIVER    VOLUME NAME
local     e7178e5d5ab643baf94c4c728b8a6bd01a909b2f8be9af97228c0684ea37f0f8
#具名挂载	只写了主机名,没有写宿主机目录路径
[root@localhost ~]# docker run -d --name nginx02 -v juming-nginx:/etc/nginx nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
f7a1c6dad281: Pull complete 
4d3e1d15534c: Pull complete 
9ebb164bd1d8: Pull complete 
59baa8b00c3c: Pull complete 
a41ae70ab6b4: Pull complete 
e3908122b958: Pull complete 
Digest: sha256:1c13bc6de5dfca749c377974146ac05256791ca2fe1979fc8e8278bf0121d285
Status: Downloaded newer image for nginx:latest
f3862ec563d96ad66aa68cf96b7407d30e6d10efb0e3298b2d0a672832f199b2
#查看所有volume的情况
[root@localhost ~]# docker volume ls
DRIVER    VOLUME NAME
local     juming-nginx
#查看主机juming-nginx具体目录
[root@localhost ~]# docker volume inspect juming-nginx 
[
    {
        "CreatedAt": "2022-03-17T19:19:28+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容器内路径:ro  rw改变读写权限
ro		readonly #只读
rw		readwrite #可读可写
docker run -d --name nginx03 -v juming-nginx:/etc/nginx:ro nginx
docker run -d --name nginx03 -v juming-nginx:/etc/nginx:rw nginx

实战:安装MySQL

#下载mysql
[root@localhost ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
15115158dd02: Pull complete 
d733f6778b18: Pull complete 
1cc7a6c74a04: Pull complete 
c4364028a805: Pull complete 
82887163f0f6: Pull complete 
28abcb7f57e0: Pull complete 
46d27a431703: Pull complete 
146a7517cdca: Pull complete 
ac645a526e45: Pull complete 
a292dcc315cc: Pull complete 
ff70b7ef8a8b: Pull complete 
Digest: sha256:66d52e6baa8093820c09fec56992a5ee734f17e9fad8ef5ffc31597b231bd048
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
#运行容器,挂载
-d 后台运行
-v 卷挂载
-e 环境配置
--name 容器名字
[root@localhost ~]# docker run -d -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7
03758b6283f8d2bae798a8224118f14acb410afe31fbab6410f1e8a43270cdfa
#查看运行的容器
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED         STATUS         PORTS                 NAMES
03758b6283f8   mysql:5.7   "docker-entrypoint.s…"   4 minutes ago   Up 4 minutes   3306/tcp, 33060/tcp   mysql01
#查看data数据
[root@localhost ~]# cd /home/mysql/data/
[root@localhost data]# ls
auto.cnf    ca.pem           client-key.pem  ibdata1      ib_logfile1  mysql               private_key.pem  server-cert.pem  sys
ca-key.pem  client-cert.pem  ib_buffer_pool  ib_logfile0  ibtmp1       performance_schema  public_key.pem   server-key.pem

#删除mysql01容器
[root@localhost ~]# docker rm -f mysql01
mysql01
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
#查看data数据,没有丢失
[root@localhost ~]# cd /home/mysql/data/
[root@localhost data]# ls
auto.cnf    ca.pem           client-key.pem  ibdata1      ib_logfile1  mysql               private_key.pem  server-cert.pem  sys
ca-key.pem  client-cert.pem  ib_buffer_pool  ib_logfile0  ibtmp1       performance_schema  public_key.pem   server-key.pem

Dockerfile来挂载

[root@sunrui ~]# mkdir /home/docker-test-volume
[root@sunrui ~]# cd /home/docker-test-volume/
[root@sunrui docker-test-volume]# vim dockerfile1
[root@sunrui docker-test-volume]# cat dockerfile1 
FROM centos
VOLUME ["volume01,"volume02"]
CMD echo "----end----"
CMD /bin/bash
[root@sunrui docker-test-volume]# docker build -f /home/docker-test-volume/dockerfile1 -t sunrui/centos .
Sending build context to Docker daemon  2.048kB
Step 1/4 : FROM centos
latest: Pulling from library/centos
a1d0c7532777: Pull complete 
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
 ---> 5d0da3dc9764
Step 2/4 : VOLUME ["volume01","volume02"]
 ---> Running in 1bc26abb451c
Removing intermediate container 1bc26abb451c
 ---> 3139db119a06
Step 3/4 : CMD echo "----end----"
 ---> Running in 9321b999c707
Removing intermediate container 9321b999c707
 ---> 242dd8c4f551
Step 4/4 : CMD /bin/bash
 ---> Running in 96c4cf78cb71
Removing intermediate container 96c4cf78cb71
 ---> 341797207d87
Successfully built 341797207d87
Successfully tagged sunrui/centos:latest
[root@sunrui docker-test-volume]# docker images
REPOSITORY      TAG       IMAGE ID       CREATED          SIZE
sunrui/centos   latest    341797207d87   28 seconds ago   231MB
centos          latest    5d0da3dc9764   6 months ago     231MB
[root@sunrui docker-test-volume]# docker run -it 341797207d87 /bin/bash
[root@fd23613e984e /]# ls -l
total 56
lrwxrwxrwx  1 root root    7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x  5 root root  360 Mar 16 13:00 dev
drwxr-xr-x  1 root root 4096 Mar 16 13:00 etc
drwxr-xr-x  2 root root 4096 Nov  3  2020 home
lrwxrwxrwx  1 root root    7 Nov  3  2020 lib -> usr/lib
lrwxrwxrwx  1 root root    9 Nov  3  2020 lib64 -> usr/lib64
drwx------  2 root root 4096 Sep 15 14:17 lost+found
drwxr-xr-x  2 root root 4096 Nov  3  2020 media
drwxr-xr-x  2 root root 4096 Nov  3  2020 mnt
drwxr-xr-x  2 root root 4096 Nov  3  2020 opt
dr-xr-xr-x 95 root root    0 Mar 16 13:00 proc
dr-xr-x---  2 root root 4096 Sep 15 14:17 root
drwxr-xr-x 11 root root 4096 Sep 15 14:17 run
lrwxrwxrwx  1 root root    8 Nov  3  2020 sbin -> usr/sbin
drwxr-xr-x  2 root root 4096 Nov  3  2020 srv
dr-xr-xr-x 13 root root    0 Mar 16 13:00 sys
drwxrwxrwt  7 root root 4096 Sep 15 14:17 tmp
drwxr-xr-x 12 root root 4096 Sep 15 14:17 usr
drwxr-xr-x 20 root root 4096 Sep 15 14:17 var
drwxr-xr-x  2 root root 4096 Mar 16 13:00 volume01   #脚本匿名挂载
drwxr-xr-x  2 root root 4096 Mar 16 13:00 volume02	 #脚本匿名挂载
[root@fd23613e984e /]# ls
bin  etc   lib    lost+found  mnt  proc  run   srv  tmp  var       volume02
dev  home  lib64  media       opt  root  sbin  sys  usr  volume01
[root@fd23613e984e /]# cd volume01
[root@fd23613e984e volume01]# ls
[root@fd23613e984e volume01]# touch test.txt
[root@fd23613e984e volume01]# ls
test.txt
[root@fd23613e984e volume01]# exit
exit
[root@sunrui docker-test-volume]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS                     PORTS     NAMES
fd23613e984e   341797207d87   "/bin/bash"   24 seconds ago   Exited (0) 3 seconds ago             determined_wu
[root@sunrui docker-test-volume]# docker inspect fd23613e984e
 "Mounts": [
 {
                "Type": "volume",
                "Name": "055f08866375cba36b6ce657c88afbdb25830191bf2a1d16bee6aeea70d14aac",
                "Source": "/var/lib/docker/volumes/055f08866375cba36b6ce657c88afbdb25830191bf2a1d16bee6aeea70d14aac/_data",
                "Destination": "volume01",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
[root@sunrui ~]# cd /var/lib/docker/volumes/055f08866375cba36b6ce657c88afbdb25830191bf2a1d16bee6aeea70d14aac/_data
[root@sunrui _data]# ls
test.txt
posted @   kongshuo  阅读(53)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示