SoFunction
Updated on 2025-03-09

A summary of the new syntax for docker directory mount

Is there any difference in writing these two types of mount directories and files?

--mount type=bind,src=/path-on-host-machine/,dst=/etc/

-v /path-on-host-machine/:/etc/

answer:

These two writing methods have functional similarities, both mount files or directories on the host into Docker containers, but there are some important differences between them. Specifically:

1. The difference between --mount and -v:

–mount syntax:

--mount type=bind,src=/path-on-host-machine/,dst=/etc/
  • –mount is a standard syntax introduced in Docker version 1.13 and later, providing more flexible, clear and powerful mount options.
  • Its syntax requires explicitly specifying the type of mount (type=bind), source path (src) and target path (dst).
  • The –mount syntax is more readable and is suitable for various types of mounts (bind, volume, tmpfs) of Docker.

-v Syntax:

-v /path-on-host-machine/:/etc/
  • -v (or --volume) is an early syntax of Docker. Although it can also be used to mount local files (bind type), it has some functional limitations compared to --mount.
  • -v is more concise, but not as flexible as the --mount syntax, because it does not require you to specify the mount type explicitly, the default is the volume type, and it is not as clear.

2. Key points of the difference:

  • Syntax and clarity:

  • The –mount syntax requires you to explicitly specify the mount type (bind or volume, etc.), and to specify the source and target paths through src and dst.

  • v The syntax is more concise, but the mount type is not displayed (the default is volume) and is less intuitive.

  • Functional flexibility:

  • –mount provides greater flexibility, supports bind, volume and tmpfs type mounts, and can better support different mount methods between containers.

  • -v only supports volume and bind type mounts, and cannot specify more details and configurations for mounts like --mount (such as read-only mounts, mount options, etc.).

  • consistency:

  • –mount is a more modern and consistent way, recommended in the new Docker version because it provides more options and clear syntax.

  • -v is still widely used, especially in old code or some simplified scenarios, but it will not be flexible enough compared to --mount in some complex scenarios.

3. -v supports bind mounts:

In fact, the -v syntax can also be used to bind mounts, just like type=bind of --mount, for example:

-v /path-on-host-machine/:/etc/

This command will mount the host path /path-on-host-machine/ to /etc/ of the container, which is equivalent to --mount type=bind,src=/path-on-host-machine/,dst=/etc/.

4. Other additional options:

  • Additional options are better supported when using --mount, for example:
--mount type=bind,src=/path-on-host-machine/,dst=/etc/,readonly

The above command will make the mounted file read-only, which cannot be specified directly in the -v syntax.

Summarize:

  • Functional:The functions of both are the same, both are to mount files on the host into the container.

  • Syntax: –mount provides a clearer and flexible syntax, recommended for complex mount scenarios, while -v is a simplified syntax for simple scenarios.

  • Recommendations for use: For more complex mount requirements (such as specifying mount type, read-only mount, etc.), it is recommended to use --mount. For simple file or directory mounts, -v is also concise and effective.

This is the end of this article about the new syntax summary of docker directory mount. For more related docker directory mount content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!