Docker 数据管理-Volumes

本文详细介绍了Docker中用于数据持久化的Volumes特性。包括Volumes的优势、如何创建与管理Volumes、如何将Volumes挂载到容器中等操作。同时对比了-v与--mount两种挂载方式的区别。

Volumes是Docker最为推荐的数据持久化方法。

Volumes have several advantages over bind mounts:

  • Volumes are easier to back up or migrate than bind mounts.
  • You can manage volumes using Docker CLI commands or the Docker API.
  • Volumes work on both Linux and Windows containers.
  • Volumes can be more safely shared among multiple containers.
  • Volume drivers allow you to store volumes on remote hosts or cloud providers, to encrypt the contents of volumes, or to add other functionality.
  • A new volume’s contents can be pre-populated by a container.

Choose the -v or --mount flag

  起初, the -v or --volume flag was used for 单机版-Containers and the --mount flag was used for Swarm services. However, starting with Docker 17.06, you can also use --mount with 单机版-Containers. In general, --mount 更清晰 and 更冗长. 最大的不同是 -v 语法将所有的选项组合在一起,--mount语法将他们进行了分离。 

Tip: 新永不应该使用--mount语法,有经验的用户更熟悉-v or --volume语法。 但是更鼓励使用--mount,因为调查发现它更容易被使用。

If you need to specify volume driver options, you must use --mount.

  • --mount: 由多个<key>=<value>对组成,使用逗号进行分割。The --mount syntax is more verbose than -v or --volume, but the order of the keys is not significant, and the value of the flag is easier to understand.
    • The type of the mount, which can be bind, volume, or tmpfs. This topic discusses volumes, so the type is always volume.
    • The source of the mount. For named volumes, this is the name of the volume. For anonymous volumes, this field is omitted. May be specified as source or src.
    • The destination takes as its value the path where the file or directory is mounted in the container. May be specified as destination, dst, or target.
    • The readonly option, if present, causes the bind mount to be mounted into the container as read-only.
    • The volume-opt option, which can be specified more than once, takes a key-value pair consisting of the option name and its value.

Create and manage volumes

Unlike a bind mount, you can create and manage volumes outside the scope of any container.

Create a volume:

$ docker volume create my-vol

List volumes:

$ docker volume ls

local               my-vol

Inspect a volume:

$ docker volume inspect my-vol
[
    {
        "Driver": "local", "Labels": {}, "Mountpoint": "/var/lib/docker/volumes/my-vol/_data", "Name": "my-vol", "Options": {}, "Scope": "local" } ] 

Remove a volume:

$ docker volume rm my-vol

Start a container with a volume

If you start a container with a volume that does not yet exist, Docker creates the volume for you. The following example mounts the volume myvol2 into /app/ in the container.

The -v and --mount examples below produce the same result. You can’t run them both unless you remove the devtest container and the myvol2 volume after running the first one.

$ docker run -d \
  --name devtest \ --mount source=myvol2,target=/app \ nginx:latest

$ docker run -d \
  --name devtest \ -v myvol2:/app \ nginx:latest

  • 可以为多个Container mount同一个volume,以达到数据在多个Container之间共享的目的;
  • 如果 mount target指向的是已有目录,原有数据会被复制到source volume 中。

参考文档: https://docs.docker.com/

### Docker `--volumes-from` 参数详解 #### 功能描述 `--volumes-from` 参数用于让新创建的容器挂载另一个已存在容器中的卷。这使得多个容器可以共享同一个数据存储位置,方便实现跨容器的数据同步和共享[^1]。 #### 基本语法 ```bash docker run -it --name new_container_name --volumes-from source_container_id image_name command ``` 其中: - `-it`: 交互模式启动容器- `new_container_name`: 新建容器的名字; - `source_container_id`: 已有容器ID或名称,作为源容器提供要挂载的卷; - `image_name`: 要使用的镜像名; - `command`: 启动后的执行指令。 #### 示例演示 假设有一个名为 `data-container` 的容器已经包含了某些重要文件夹 `/var/lib/mysql` ,现在希望另外两个新的容器也能够访问这个路径下的内容: ```bash # 创建第一个依赖于 data-container 卷的新容器 docker run -d --name db_backup_01 --volumes-from data-container mysql:latest mysqldump ... # 再次创建第二个同样依赖该卷的新容器 docker run -ti --rm --volumes-from data-container centos bash ``` 上述例子展示了如何通过指定 `--volumes-from data-container` 来使其他容器继承来自 `data-container` 中定义好的任何卷设置。 #### 只读权限控制 如果只想让目标容器以只读方式访问某个特定卷,则可以在命令后面加上 `:ro` 标志位: ```bash docker run --volumes-from existing_container:ro ... ``` 这样做的好处是可以防止意外修改原始数据的同时保持必要的资源共享能力[^2]。 #### 解决方案与注意事项 当遇到无法正常工作的情况时,可尝试以下方法排查并解决问题: - **确认源容器状态**:确保被引用的源容器处于运行状态;停止状态下仍能成功加载卷但可能影响性能。 - **检查命名冲突**:避免不同容器间出现相同名字引起混淆错误。 - **清理残留资源**:删除不再需要的老化容器以免干扰最新实例间的通信连接。 - **更新客户端版本**:对于较老版Docker CLI来说部分特性支持有限,建议升级到稳定发行版再试一次操作流程。 #### 替代工具推荐 考虑到复杂场景下手动管理多层嵌套关系容易出错,官方推出了更高级别的组合式应用编排框架——Docker Compose 。它允许用户在一个YAML配置文档里清晰地表达整个应用程序所需的所有组件和服务,并简化了一键部署过程[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值