Terminus by Warp
Docker Bind Mounts (docker run --volume)

Docker Bind Mounts (docker run --volume)

Razvan Ludosanu
Razvan Ludosanu
Founder, learnbackend.dev

The short answer

To mount a local directory into a Docker container (i.e. bind mount), you can use the [.inline-code]docker run[.inline-code] command combined with the [.inline-code]-v[.inline-code] option flag (short for [.inline-code]--volume[.inline-code]) as follows:

$ docker run -v <host_directory>:<container_directory> <image>

Where:

  • [.inline-code]host_directory[.inline-code] is the absolute or relative path of the bind mount on your local machine.
  • [.inline-code]container_directory[.inline-code] is the absolute path of the file or directory within the container.
  • [.inline-code]image[.inline-code] is the name of the Docker image the container will be launched from.

For example:

$ docker run -v ./app:/app node-server

The above command mounts the [.inline-code]app[.inline-code] directory in the current directory into the container at the [.inline-code]/app[.inline-code] path using the [.inline-code]-v[.inline-code] flag.

Note that if the specified directory doesn't exist on your local machine, Docker will automatically create it before starting the container.

Also note that the use of relative local paths are only available as of Docker version 23.

If instead you are looking for persistent storage, you can read more about named volumes on the official Docker volumes page.

[#easily-recall-with-ai]Easily retrieve this command using Warp’s AI Command Search [#easily-recall-with-ai]

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Search feature:

Entering [.inline-code]docker run volume directory[.inline-code] in the AI Command Search will prompt an [.inline-code]docker run[.inline-code] command that can then quickly be inserted into your shell by doing [.inline-code]CMD+ENTER[.inline-code].

[#the-volume-flag]The [.inline-code]--volume[.inline-code] and [.inline-code]--mount[.inline-code] flags [#the-volume-flag]

The [.inline-code]--volume[.inline-code] and [.inline-code]--mount [.inline-code] flags are both used to mount a file or a directory into a container and essentially have the same behavior, only with a different syntax.

The [.inline-code]--volume[.inline-code] flag

The [.inline-code]-v[.inline-code] (or [.inline-code]--volume[.inline-code]) flag consists of three fields, separated by colon characters:

$ docker run -v <source>:<destination>:<options> <image>

Where:

  • The [.inline-code]source[.inline-code] field is the path of the file or directory on the host machine.
  • The [.inline-code]destination[.inline-code] field is the path where the file or directory is mounted into the container.
  • The [.inline-code]options[.inline-code] field is a comma separated list of options.

For example:

 $ docker run -v ./app:/app:ro alpine

[#the-mount-flag] The [.inline-code]--mount[.inline-code] flag[#the-mount-flag]

The [.inline-code]--mount[.inline-code] flag consists of multiple key-value pairs, separated by commas and each consisting of a [.inline-code]key=value[.inline-code] tuple:

 $ docker run --mount type=<type>,source=<source>,destination=destination>,<options> <image>

Where:

  • The [.inline-code]type[.inline-code] field is the type of the mount, which should be set to [.inline-code]bind[.inline-code] when using bind mounts.
  • The [.inline-code]source[.inline-code] field is the path of the file or directory on the host machine.
  • The [.inline-code]destination[.inline-code] field is the path where the file or directory is mounted into the container.
  • The [.inline-code]options[.inline-code] field is a comma separated list of options.

For example:

 $ docker run -v --mount type=bind,source=./app,destination=/app alpine,readonly

[#volume-vs-mount]Differences between [.inline-code]--volume[.inline-code] and [.inline-code]--mount[.inline-code][#volume-vs-mount]

Besides their syntax, the only difference between these flags is their behavior when it comes to creating bind-mounted directories that don't exist.

When using the [.inline-code]-v[.inline-code] or [.inline-code]--volume[.inline-code] flag, Docker will automatically create the bind-mounted directory on your local machine if it doesn't exist.

When using the [.inline-code]--mount[.inline-code] flag, Docker will not create the bind-mounted directory on your local machine if it doesn't exist and generate an error.

You can learn more about bind mounts on the official Docker documentation page.

Mounting files and directories into a container

As of Docker version 23, you can mount a local file or directory into a container using either its relative or absolute path.

[#mounting-the-current-directory] Mounting the current directory [#mounting-the-current-directory]

To mount the current directory into the container at a specified path, you can use the dot syntax as follows:

 $ docker run -v .:/path/to/directory <image>

To mount the current directory at the same path into the container, you can use the command substitution syntax with the [.inline-code]pwd[.inline-code] command as follows:

 $ docker run -v $(pwd):$(pwd) <image>

Note that when using this syntax, Docker will automatically create all the intermediary directories starting from the root in order to preserve the directory structure.

[#mounting-a-file] Mounting a file[#mounting-a-file]

To mount a single file into a container, you can use the same syntax as for directories:

 $ docker run -v /path/to/file:/path/to/file <image>

[#using-multiple-bind-mounts]Using multiple bind mounts[#using-multiple-bind-mounts]

To mount several files or directories into a container, you can repeat the [.inline-code]-v[.inline-code] flag multiple times as follows:

 $ docker run -v ./dir_1:/app/dir_1 -v ./dir_2:/app/dir_2 <image>

[#read-only-bind-mounts]Use a read-only bind mounts[#read-only-bind-mounts]

By default, any modifications made by a container to the files and directories of a bind mount will be propagated back to the local machine.

To prevent that, you can define a bind mount as read-only, either using the [.inline-code]ro[.inline-code] option when working with the [.inline-code]-v[.inline-code] flag:

 $ docker run -v ./app:/app:ro alpine

Or the [.inline-code]readonly[.inline-code] option when working with the [.inline-code]--mount[.inline-code] flag:

 $ docker run --mount type=bind,source=./app,target=/app,readonly alpine

[#windows-bind-mounts]Using bind mounts on Windows [#windows-bind-mounts]

When working with Docker for Windows, there are essentially two ways you can write the paths of the files and directories you want to mount into a container.

You can either use escaped Windows-like paths as follows:

$ docker run -v C:\\Users\\user\\work:/work <image>

Or you can use Unix-like paths as follows:

$ docker run -v //c/Users/user/work:/work <image>

Note that when using the [.inline-code]$(pwd)[.inline-code] command substitution, you will have to prepend a slash character to this expression in order for it to work:

$ docker run -v /$(pwd):/work <image>

Experience the power of Warp

  • Write with an IDE-style editor
  • Easily navigate through output
  • Save commands to reuse later
  • Ask Warp AI to explain or debug
  • Customize keybindings and launch configs
  • Pick from preloaded themes or design your own
brew install --cask warp
Copied!
Join the Windows waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.
Join the Linux waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.
Join the Linux waitlist or join the Windows waitlist
Join the Windows waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.