What is Bind Mount?

What is Bind Mount?

Learn about Bind Mounts in Docker: Directly link host files or directories to containers, enabling real-time data sharing and access.

Published Oct 5, 2024
Bind mounts in Docker allow you to mount a file or directory on the host machine into a container. This means that changes made to the files in the mounted directory within the container will be reflected on the host, and vice versa. Bind mounts are commonly used for development, debugging, or providing configuration files to containers.
Bind mounts have been around since the early days of Docker. Bind mounts have limited functionality compared to volumes. When you use a bind mount, a file or directory on the host machine is mounted into a container. The file or directory is referenced by its absolute path on the host machine. By contrast, when you use a volume, a new directory is created within Docker’s storage directory on the host machine, and Docker manages that directory’s contents.
The file or directory does not need to exist on the Docker host already. It is created on demand if it does not yet exist. Bind mounts are very performant, but they rely on the host machine’s filesystem having a specific directory structure available. If you are developing new Docker applications, consider using named volume instead. You can’t use Docker CLI commands to directly manage bind mounts.

👉 Choose the — mount flag

--mount: Consists of multiple key-value pairs, separated by commas and each consisting of a <key>=<value> tuple. 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 bind mounts, so the type is always bind.
  • The source of the mount. For bind mounts, this is the path to the file or directory on the Docker daemon host. 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 bind-propagation option, if present, changes the bind propagation. May be one of rprivate, private, rshared, shared, rslave, slave.
  • The --mount flag does not support z or Z options for modifying selinux labels.

👉 Differences between -v and — mount behavior

Because the -v and --volume flags have been a part of Docker for a long time, their behavior cannot be changed. This means that there is one behavior that is different between -v and --mount.
If you use -v or --volume to bind-mount a file or directory that does not yet exist on the Docker host, -v creates the endpoint for you. It is always created as a directory.
If you use --mount to bind-mount a file or directory that does not yet exist on the Docker host, Docker does not automatically create it for you, but generates an error.
Here’s how you can use bind mounts in Docker:
  • /host/path: Path on the host machine.
  • /container/path: Path in the container where the host path will be mounted.
  • my_image: The Docker image you're running.

👉 Start a Container with Bind Mount:

Using Docker bind mounts, you can mount a file or folder on the host machine inside the container. The file or directory is referenced by its absolute path on the host machine.
To demonstrate this, let’s create a directory bind on our host machine and create a file index.html in it.
Where contents of index.html file are as follows-
We will see how we can access this same file inside our container.
Create a container using Ubuntu image and binding the directory we just created to the /tmp/test/ directory inside the container.
Note: As we have seen in docker volumes if the /tmp/test/ is not present, docker will create it automatically and also, in case the path already exists and if there is some data already then, that data will get hidden but will still be accessible to the user.
Now verifying the output of the above command:
The container has the test directory and also index.html in place.
If we make any changes inside the container, it will be reflected in the host machine’s directory as well.
Make sure that while providing the path of the directory, you mention the entire path and not the relative path as docker will consider it a new volume-
Passing the current directory as bind volume.
In case you want to bind the present working directory, then pass the $(pwd) to the command as below:-
The above command can also be written in below manner:
 

Comments