Docker笔记-容器数据卷

Docker笔记-容器数据卷

docker的理念将运行的环境打包形成容器运行,运行可以伴随容器,但是我们对数据的要求是希望持久化,容器

之间可以共享数据,Docker容器产生的数据,如果不通过docker commit生成新的镜像,使得数据作为容器的一

部分保存下来,那么当容器被删除之后,数据也就没了,为了能够保存数据,在docker容器中使用卷。卷就是目

录或者文件,存在于一个或者多个容器中,但是不属于联合文件系统,因此能够绕过Union File System提供一些

用于持久化数据或共享数据的特点。

添加数据卷的方式有两种,第一种是直接通过命令行挂载,第二种是通过dockerFile添加。

1、通过命令挂载

shell 复制代码
$ docker run -it -v /宿主机绝对路径目录:/容器内目录  镜像名

1.1 指定路径挂载

shell 复制代码
# 获取镜像
$ docker pull mysql:5.7
shell 复制代码
# 启动MySQL
# -d 后台运行
# -p 端口映射
# -v 卷挂载
# -e 环境配置
# --name 容器名字
$ docker run -d -p 3306:3306 -v /home/zhangshixing/work/linuxmysql/conf:/etc/mysql/conf.d -v /home/zhangshixing/work/linuxmysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name zsxmysq1 mysql:5.7

1.2 匿名挂载

shell 复制代码
$ docker pull nginx
shell 复制代码
[root@zsx linuxmysql]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
eff15d958d66: Pull complete 
1e5351450a59: Pull complete 
2df63e6ce2be: Pull complete 
9171c7ae368c: Pull complete 
020f975acd28: Pull complete 
266f639b35ad: Pull complete 
Digest: sha256:097c3a0913d7e3a5b01b6c685a60c03632fc7a2b50bc8e35bcaa3691d788226e
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
shell 复制代码
# -v容器内路径
$ docker run -d -P --name mynginx -v /ect/nginx nginx
shell 复制代码
# 查看所有的volume的情况
$ docker volume ls
shell 复制代码
[root@zsx linuxmysql]# docker volume ls
DRIVER    VOLUME NAME
local     9c4a5b5d2489bfed1f8d247349444362c96dd54d2b30454e2bb62eb370a8fd1b
local     50ea91e78556e99ee7a0ae4fb62e97b83c92e70aec54bb3b5a1038fa093c4555
local     629aa14a8191229b0b52b17198400ca69c6391285b6917a9a447bb8f58fd2a75
local     a542052cdf3ee37e072281e2d5c66b512a6db5f286a2cc5c6a1c5b442c93eaa8
local     my_wordpress_db_data
local     my_wordpress_wordpress_data

1.3 具名挂载

shell 复制代码
$ docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx nginx
shell 复制代码
$ docker volume ls
shell 复制代码
root@zsx linuxmysql]# docker volume ls
DRIVER    VOLUME NAME
local     9c4a5b5d2489bfed1f8d247349444362c96dd54d2b30454e2bb62eb370a8fd1b
local     50ea91e78556e99ee7a0ae4fb62e97b83c92e70aec54bb3b5a1038fa093c4555
local     629aa14a8191229b0b52b17198400ca69c6391285b6917a9a447bb8f58fd2a75
local     a542052cdf3ee37e072281e2d5c66b512a6db5f286a2cc5c6a1c5b442c93eaa8
local     juming-nginx
local     my_wordpress_db_data
local     my_wordpress_wordpress_data
shell 复制代码
# 可以得到在宿主机上的卷的位置
$ docker volume inspect juming-nginx
shell 复制代码
[root@zsx linuxmysql]# docker volume inspect juming-nginx
[
    {
        "CreatedAt": "2021-11-18T17:44:10+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/juming-nginx/_data",
        "Name": "juming-nginx",
        "Options": null,
        "Scope": "local"
    }
]
shell 复制代码
[root@zsx linuxmysql]# docker volume inspect 9c4a5b5d2489bfed1f8d247349444362c96dd54d2b30454e2bb62eb370a8fd1b
[
    {
        "CreatedAt": "2021-11-18T17:39:09+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/9c4a5b5d2489bfed1f8d247349444362c96dd54d2b30454e2bb62eb370a8fd1b/_data",
        "Name": "9c4a5b5d2489bfed1f8d247349444362c96dd54d2b30454e2bb62eb370a8fd1b",
        "Options": null,
        "Scope": "local"
    }
]

如何确定是具名挂载还是匿名挂载,还是指定路径挂载:

shell 复制代码
-v 容器内路径              # 匿名挂载
-v 卷名:容器内路径          # 具名挂载
-v 宿主机路径::容器内路径    # 指定路径挂载

通过-v 容器内路径:ro rw 改变读写权限:

shell 复制代码
ro    readonly   # 只读
rw    readwrite  # 可读可写
shell 复制代码
# 只要看到ro就说明这个路径只能通过宿主机来操作,容器内部是无法操作
$ 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

2、通过DockerFile添加

Dockerfile是一个用来构建镜像的文本文件,文本内容包含了一条条构建镜像所需的指令和说明。

shell 复制代码
# 创建一个Dockerfile文件,名字可以随机,建议Dockerfile
# 文件中的指令(大写)
# 这里的每个命令,就是镜像的一层
FROM centos
VOLUME ["volume01","volume02"]
CMD echo "Hello World!!!"
CMD /bin/bash
shell 复制代码
# zsx242030/centos:1.0是登录的情况下
$ docker build -f /home/zhangshixing/work/volumetest/dockerfile -t zsx242030/centos:1.0 .
# 没有登录的情况下
$ docker build -f /home/zhangshixing/work/volumetest/dockerfile -t centos:1.0 .
shell 复制代码
[root@zsx volumetest]# docker build -f /home/zhangshixing/work/volumetest/dockerfile -t centos:1.0 .
Sending build context to Docker daemon  2.048kB
Step 1/4 : FROM centos
 ---> 5d0da3dc9764
Step 2/4 : VOLUME ["volume01","volume02"]
 ---> Running in e77e19d82b76
Removing intermediate container e77e19d82b76
 ---> 0f77fb23826b
Step 3/4 : CMD echo "Hello World!!!"
 ---> Running in 456e49681cf3
Removing intermediate container 456e49681cf3
 ---> 055a082db43e
Step 4/4 : CMD /bin/bash
 ---> Running in babb5220e85c
Removing intermediate container babb5220e85c
 ---> 02f263acec53
Successfully built 02f263acec53
Successfully tagged centos:1.0
shell 复制代码
[root@zsx volumetest]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
centos       1.0       02f263acec53   30 seconds ago   231MB
nginx        latest    ea335eea17ab   26 hours ago     141MB
mysql        5.7       8b43c6af2ad0   26 hours ago     448MB
centos       latest    5d0da3dc9764   2 months ago     231MB
ubuntu       15.10     9b9cb95443b5   5 years ago      137MB

3、数据卷容器进行数据的双向同步

shell 复制代码
# 启动之后会有两个容器卷
$ docker run -it --name docker01 centos:1.0
shell 复制代码
[root@zsx work]# docker run -it --name docker01 centos:1.0
[root@33acc58bfa50 /]# ls
bin  etc   lib	  lost+found  mnt  proc  run   srv  tmp  var	   volume02
dev  home  lib64  media       opt  root  sbin  sys  usr  volume01
shell 复制代码
$ docker run -it --name docker02 --volumes-from docker01 centos:1.0
shell 复制代码
[root@zsx work]# docker run -it --name docker02 --volumes-from docker01 centos:1.0
[root@3499fc3a4a5b /]# ls
bin  etc   lib	  lost+found  mnt  proc  run   srv  tmp  var	   volume02
dev  home  lib64  media       opt  root  sbin  sys  usr  volume01

在docker01中的volume01中新建一个a.txt文件

shell 复制代码
[root@33acc58bfa50 /]# cd volume01/
[root@33acc58bfa50 volume01]# touch a.txt
[root@33acc58bfa50 volume01]# ls
a.txt

在docker02中的volume01中查看

shell 复制代码
[root@3499fc3a4a5b /]# cd volume01/
[root@3499fc3a4a5b volume01]# ls
a.txt
[root@3499fc3a4a5b volume01]# 

在docker02中的volume02中新建b.txt文件

shell 复制代码
[root@3499fc3a4a5b volume01]# cd ../volume02
[root@3499fc3a4a5b volume02]# touch b.txt
[root@3499fc3a4a5b volume02]# ls
b.txt

在docker01中的volume02中查看

shell 复制代码
[root@33acc58bfa50 volume01]# cd ../volume02/
[root@33acc58bfa50 volume02]# ls
b.txt

只要有一个容器在运行,数据就会在。

shell 复制代码
[root@zsx test]# docker inspect 02f263acec53
[
    {
        "Id": "sha256:02f263acec53fd4b71a01dc0b194e91ba90069fc1b98c825758c6995e7d4fb3e",
        "RepoTags": [
            "centos:1.0"
        ],
        "RepoDigests": [],
        "Parent": "sha256:055a082db43e9f890e5abd27847eb0a950e9ee76f54eff0d54d8b19e5c2d9f56",
        "Comment": "",
        "Created": "2021-11-18T12:28:21.778839806Z",
        "Container": "babb5220e85cd98c6b7267d99b0b8d4372012218f6276d65b9b994e63872e7a2",
        "ContainerConfig": {
            "Hostname": "babb5220e85c",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"/bin/sh\" \"-c\" \"/bin/bash\"]"
            ],
            "Image": "sha256:055a082db43e9f890e5abd27847eb0a950e9ee76f54eff0d54d8b19e5c2d9f56",
            "Volumes": {
                "volume01": {},
                "volume02": {}
            },
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20210915",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS"
            }
        },
        "DockerVersion": "20.10.10",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "/bin/bash"
            ],
            "Image": "sha256:055a082db43e9f890e5abd27847eb0a950e9ee76f54eff0d54d8b19e5c2d9f56",
            "Volumes": {
                "volume01": {},
                "volume02": {}
            },
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20210915",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS"
            }
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 231268856,
        "VirtualSize": 231268856,
        "GraphDriver": {
            "Data": {
                "MergedDir": "/var/lib/docker/overlay2/38db170a6092df8b72f4c87a12fc9936436f6fb360e19ad69a96a7e084b9bd3a/merged",
                "UpperDir": "/var/lib/docker/overlay2/38db170a6092df8b72f4c87a12fc9936436f6fb360e19ad69a96a7e084b9bd3a/diff",
                "WorkDir": "/var/lib/docker/overlay2/38db170a6092df8b72f4c87a12fc9936436f6fb360e19ad69a96a7e084b9bd3a/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59"
            ]
        },
        "Metadata": {
            "LastTagTime": "2021-11-18T20:28:21.791051431+08:00"
        }
    }
]
相关推荐
明 庭3 小时前
Ubuntu下通过Docker部署NGINX服务器
服务器·ubuntu·docker
dessler5 小时前
Docker-run命令详细讲解
linux·运维·后端·docker
陌北v16 小时前
Docker Compose 配置指南
运维·docker·容器·docker-compose
阿里嘎多学长7 小时前
docker怎么部署高斯数据库
运维·数据库·docker·容器
明 庭7 小时前
Ubuntu下通过Docker部署Caddy服务器
服务器·ubuntu·docker
G_whang8 小时前
windos 安装docker
运维·docker·容器
Mitch3119 小时前
【漏洞复现】CVE-2021-45788 SQL Injection
sql·web安全·docker·prometheus·metersphere
运维小文9 小时前
K8S中的PV、PVC介绍和使用
docker·云原生·容器·kubernetes·存储
CYX_cheng9 小时前
Docker挂载
docker
奉孝12 小时前
docker基础
后端·docker