Docker Compose:不带冒号的卷(:) [英] Docker Compose: volumes without colon (:)

查看:251
本文介绍了Docker Compose:不带冒号的卷(:)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 docker-compose.yml 文件,其中包含以下内容:

I have a docker-compose.yml file with the following:

volumes:
  - .:/usr/app/
  - /usr/app/node_modules

第一个选项将当前主机目录映射到 / usr / app ,但是第二个选项做什么?

First option maps current host directory to /usr/app, but what does the second option do?

推荐答案

[刷新此答案,因为似乎其他人也有类似的问题]

[Refreshing this answer since it seems others have similar questions]

泊坞窗中有三种卷:


  • 主机卷:这些通过绑定安装将从主机到容器的路径映射。它们的简短语法为 / path / on / host:/ path / in / container 。主机上存在的是在​​容器中可见的东西,没有文件的合并或图像的初始化,并且uid / gid不会获得任何特殊的映射,因此您需要注意允许容器uid / gid读取和对此位置的写访问权限(带有OSXFS的Mac版Docker是一个例外)。如果主机上的路径不存在,则docker将以root身份创建一个空目录,如果它是文件,则可以通过这种方式将单个文件装入容器。

  • Host volumes: these map a path from the host into the container with a bind mount. They have the short syntax /path/on/host:/path/in/container. Whatever exists on the host is what will be visible in the container, there's no merging of files or initialization from the image, and uid/gid's do not get any special mapping so you need to take care to allow the container uid/gid read and write access to this location (an exception is Docker for Mac with OSXFS). If the path on the host does not exist, docker will create an empty directory as root, and if it is a file, you can mount a single file into the container this way.

命名卷:这些卷具有名称,而不是主机路径作为源。它们具有简短的语法 name:/ path / in / container ,在撰写文件中,您还需要在顶层容器中定义命名卷。默认情况下,这些也是绑定安装,但要绑定到 / var / lib / docker / volumes 下的Docker特定目录,应将其视为内部目录。但是,可以更改这些默认设置,以允许NFS挂载,挂载磁盘甚至您自己的绑定挂载到其他位置。命名卷在docker中也具有一项功能,当它们是新的或为空且首次使用时,docker会在挂载之前将映像中的内容复制到命名卷中。这包括文件,目录,uid / gid所有者和权限。之后,它们的行为与主机卷相同,无论卷中的任何内容覆盖图像位置。

Named volumes: these have a name, instead of a host path as the source. They have the short syntax name:/path/in/container and in a compose file, you also need to define the named volume used in containers at the top level. By default, these are also a bind mount, but to a docker specific directory under /var/lib/docker/volumes that should be considered internal. However these defaults can be changed to allow things like NFS mounts, mounting disks, or even your own bind mounts to other locations. Named volumes also have a feature in docker, when they are new or empty and first used, docker copies the contents from the image into named volume before mounting it. This includes files, directories, uid/gid owners, and permissions. After that, they behave identical to a host volume, whatever is inside the volume overlays the image location.

匿名卷:这些仅在容器内部具有路径。它们的格式为 / path / in / container ,并且docker将创建一个默认的命名卷,其中guid为名称。它们共享命名卷的行为,将文件存储在/ var / lib / docker / volumes下,并使用映像的内容进行初始化,除了它们具有随机生成的guid之外,该GUId不能指示使用方式或使用方式。您可以将卷安装在另一个容器中并检查其中的内容,或者可以通过检查每个容器以找到容器来使用该卷找到该容器。如果使用-rm 标志创建一个容器,匿名卷也将被自动删除。

Anonymous volumes: these only have a path inside the container. They are in the form /path/in/container and docker will create a default named volume with a guid as the name. They share the behaviors of named volumes, storing files under /var/lib/docker/volumes, initializing with the contents of the image, except they have a randomly generated guid that gives you no indication of how or even if they are being used. You can mount the volume in another container and inspect the contents, or you can find the container using the volume by inspecting each container to find the guid. If you create a container with the --rm flag, anonymous volumes will also be deleted automatically.

tmpfs :等等,我说3,这是4?这是因为tmpfs不被视为卷,因此挂载它的语法是不同的。结果是一个指向内存中文件系统为空的指针。如果您有不希望保存的临时文件,它们相对较小,并且您需要速度或要确保它们没有保存到磁盘上,则这很有用。

tmpfs: Wait, I said 3, and this is 4? That's because tmpfs isn't considered a volume, the syntax to mount it is different. The result is a pointer to an empty in memory filesystem. This is useful if you have temporary files you don't wish to save, they are relatively small, and you either need speed or want to be sure they aren't saved to disk.

在OP中,


  • / usr / app 是从主机装入的,通常用于开发

  • / usr / app / node_modules 是匿名卷从图片初始化

  • /usr/app is mounted from the host, commonly used for development
  • /usr/app/node_modules is an anonymous volume initialized from the image

为什么这样做?可能是因为您不想修改主机上的 node_modules 目录,尤其是在存在特定于平台的数据并且您在主机为Mac / Win的Docker桌面上运行的情况下和容器中的Linux。您可能还需要在其他卷装载的目录结构中访问映像中的数据。

Why do this? Likely because you do not want to modify the node_modules directory on the host, particularly if there's platform specific data and you're running on Docker desktop where it's Mac/Win on the host and Linux in the container. It's also possible there's data in the image you want to get access to within the directory structure of the other volume mount.

匿名卷是否有缺点?我能想到的两个:

Are there downsides to anonymous volumes? Two that I can think of:


  • 如果 / usr / app / node_modules 要在以后的容器中重用,则不太可能找到旧的卷。我倾向于认为写入这些数据的任何数据都可能丢失。

  • If there's anything in /usr/app/node_modules that you want to reuse in a future container, you're unlikely to find the old volume. I tend to consider any data written to these as likely lost.

随着时间的推移,您经常会发现主机上的卷充满了引导,目前尚不清楚哪些正在使用中以及哪些可以删除。未使用的匿名卷是docker中磁盘使用过多的几种原因之一。

You'll often find the volumes on the host full of guids over time, and it's unclear which are in use and which can be deleted. Unused anonymous volumes are one of several causes of excessive disk use in docker.

有关Docker卷的更多详细信息,请参阅: https://docs.docker.com/storage/

For more details on docker volumes, see: https://docs.docker.com/storage/

原始答案:

第二个创建一个匿名卷。它会在 docker volume ls 中列出,带有长的唯一ID而不是名称。如果您更新映像,则Docker-compose可以重用此文件,但是很容易忘记使用这些名称来区分哪个卷属于哪个卷,因此我建议您始终为卷命名。

The second one creates an anonymous volume. It will be listed in docker volume ls with a long unique id rather than a name. Docker-compose will be able to reuse this if you update your image, but it's easy to lose track of which volume belongs to what with those names, so I recommend always giving your volume a name.

这篇关于Docker Compose:不带冒号的卷(:)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆