容器数据卷

指定路径挂载

-v 宿主机目录:容器目录
docker run -it -v 宿主机目录:容器目录 centos /bin/bash

#查看挂载信息
docker inspect 容器ID

"Mounts": [
    {
        "Type": "bind",
        "Source": "宿主机目录",
        "Destination": "容器目录",
        ...
    }
]

匿名挂载

-v 容器目录
docker run -d -P --name test1 -v /test  nginx

-P 随机开放端口

#查看所有volume信息
docker volume ls

DRIVER   VOLUME NAME
local    ccae06dda8374c37d25321b266e1d77eb786594f1aa9dfb861eb6097114d78da

#查看具体挂载
docker volume inspect ccae06dda8374c37d25321b266e1d77eb786594f1aa9dfb861eb6097114d78da

[
    {
        "CreatedAt": "2021-08-01T20:09:40+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/ccae06dda8374c37d25321b266e1d77eb786594f1aa9dfb861eb6097114d78da/_data",
        "Name": "ccae06dda8374c37d25321b266e1d77eb786594f1aa9dfb861eb6097114d78da",
        "Options": null,
        "Scope": "local"
    }
]

具名挂载

具名挂载可以方便的找到我们的卷,使用较多

-v 卷名:容器目录
docker run -d -P --name test2 -v test-name:/test  nginx
#查看所有volume信息
docker volume ls

DRIVER    VOLUME NAME
local     test-name

#查看具体挂载
docker volume inspect test-name

[
    {
        "CreatedAt": "2021-08-01T20:12:29+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/test-name/_data",
        "Name": "test-name",
        "Options": null,
        "Scope": "local"
    }
]

docker默认挂载目录:/var/lib/docker/volumes/...

拓展

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

ro readonly  #只读(容器内无法修改,只能通过宿主机操作)
rw readwrite #可读可写(默认)

Mysql数据挂载

官方文档:https://hub.docker.com/_/mysql

#获取镜像
docker pull mysql:5.7

#数据挂载
docker run -d -p 3310:3306 \
-v /home/mysql/conf:/etc/mysql/conf.d \
-v /home/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=密码 \
--name 容器名称 \
mysql:5.7

测试连接
image.png


IT小马
1.2k 声望166 粉丝

Php - Go - Vue - 云原生