大网数据广告活动
服务器运维问答

How to decide between a docker volume container and a docker volume?

· 52CE 我爱测

After reading the docs I found myself somewhat confused as to how best to manage productive application/service data. There seem to be 3 options: Simply map volume to host directory (i.e. -v argument for docker run ) Create a doc…

问题原文

原帖链接 · dukeofgaming · CC BY-SA 3.0

After reading the docs I found myself somewhat confused as to how best to manage productive application/service data.

There seem to be 3 options:

  1. Simply map volume to host directory (i.e. -v argument for docker run)
  2. Create a docker container image for data (i.e. separate container and --volumes-from)
  3. Creating a docker volume (i.e. docker volume create)

Now, it seems that the accepted practice is option #2, but then I wonder what is the purpose of #3.

Especially how do you correctly handle these scenarios with docker volume and is it better to use a data volume container or this for each situation?

  • You need application data in a separate volume and/or storage tier in your server
  • Backing up
  • Restoring data

已采纳解答

原帖链接 · Andy Shinn · CC BY-SA 3.0

I think #2 and #3 are pretty much the same thing, the main difference is that there is no stopped container with #3 (it is literally, just a named volume). For example, you can create a named volume and do similarly what you would do with #2 with -v instead.

Create a named volume:

$ docker volume create --name test

Mount and write some data to that volume from a container:

$ docker run -v test:/opt/test alpine touch /opt/test/hello

You can then mount that same test volume in another container and read the data:

$ docker run -v test:/opt/test alpine ls -al /opt/test     
total 8
drwxr-xr-x    2 root     root          4096 Jan 23 22:28 .
drwxr-xr-x    3 root     root          4096 Jan 23 22:29 ..
-rw-r--r--    1 root     root             0 Jan 23 22:28 hello

The advantage here is that the volume won't accidentally disappear if you remove the data-only container. You now manage it with the docker volume sub-command.

$ d volume ls
DRIVER              VOLUME NAME
local               test

It also opens the possibilities for volume drivers down the road so you might be able to do shared volumes between hosts (ie. named volumes over NFS). Examples of this might be Flocker and Convoy. To your point specifically about moving or backing up data, Convoy has specific sub-commands for backing up data and allows for storage on NFS or EBS external to your host.

For this reason, I think the more new-school way (Docker 1.9+) is to use a named volume rather than a data-only container.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-01-21)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。