To mount a local directory into a Docker container (i.e. bind mount), you can use the docker run command combined with the -v option flag (short for --volume) as follows:
$ docker run -v <host_directory>:<container_directory> <image>
Run in Warp
Where:
host_directory is the absolute or relative path of the bind mount on your local machine.
container_directory is the absolute path of the file or directory within the container.
image is the name of the Docker image the container will be launched from.
For example:
$ docker run -v ./app:/app node-server
Run in Warp
The above command mounts the app directory in the current directory into the container at the /app path using the -v 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 retrieve this command using Warp’s AI Command Search
If you’re using Warp as your terminal, you can easily retrieve this command using the Warp
AI Command Search feature
:
Entering docker run volume directory in the AI Command Search will prompt an docker run command that can then quickly be inserted into your shell by doing CMD+ENTER.
The --volume and --mount flags
The --volume and --mount 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 --volume flag
The -v (or --volume) flag consists of three fields, separated by colon characters:
$ docker run -v <source>:<destination>:<options> <image>
Run in Warp
Where:
The source field is the path of the file or directory on the host machine.
The destination field is the path where the file or directory is mounted into the container.
The options field is a comma separated list of options.
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
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>
Run in Warp
Using multiple bind mounts
To mount several files or directories into a container, you can repeat the -v flag multiple times as follows:
$ docker run -v ./dir_1:/app/dir_1 -v ./dir_2:/app/dir_2 <image>
Run in Warp
Use a 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 ro option when working with the -v flag:
Or the readonly option when working with the --mount flag:
$ docker run --mount type=bind,source=./app,target=/app,readonly alpine
Run in Warp
Using bind mounts on Windows
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>
Run in Warp