• Home
  • LLMs
  • Python
  • Docker
  • Kubernetes
  • Java
  • Maven
  • All
  • About
Docker | Mount Local Volumes
  1. Notes
  2. Docker volumes
  3. Bind Mount Directories and Files using '--volume' option
  4. Mount Directories and Files using '--mount' option
  5. Mount a Temporary Filesystem Using '--tmpfs' option

  1. Notes
    The "docker container run" command has two options that allow you to mount local files and directories into your containers:
    -v, --volume list                      Bind mount a volume
        --mount mount                      Attach a filesystem mount to the container
  2. Docker volumes
    The first option is to use Docker volumes, a special type of data storage managed by Docker that allows us to manage the files and directories used by containers

    Create a test volume:
    $ docker volume create volume1
    volume1
    Let's check the created volume:
    $ docker volume ls --format json | jq
    {
      "Availability": "N/A",
      "Driver": "local",
      "Group": "N/A",
      "Labels": "",
      "Links": "N/A",
      "Mountpoint": "/var/lib/docker/volumes/volume1/_data",
      "Name": "volume1",
      "Scope": "local",
      "Size": "N/A",
      "Status": "N/A"
    }
    $ docker volume inspect volume1 --format json | jq
    [
      {
        "CreatedAt": "2024-06-18T23:55:27Z",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/volume1/_data",
        "Name": "volume1",
        "Options": null,
        "Scope": "local"
      }
    ]
    Let's use the created volume:
    $ docker container run --rm --volume volume1:/cvdata ubuntu:latest \
    /bin/bash -c "echo 'hello docker volumes' > /cvdata/file1.txt; cat /cvdata/file1.txt"
    hello docker volumes
  3. Bind Mount Directories and Files using '--volume' option
    The second option is to bind mount a directory and a file from the Docker host.

    Let's bind mount a directory and a file using the -v (--volume) option:
    $ touch ./dockerhostfile.txt
    $ sudo chown root:root ./dockerhostfile.txt
    $ docker container run --rm \
    --volume /usr/local/bin:/tmp/ulb:ro \
    --volume ./dockerhostfile.txt:/tmp/containerfile.txt \
    ubuntu:latest \
    /bin/bash -c "ls -1 /tmp/ulb | wc -l; echo 'test' > /tmp/containerfile.txt"
    Notes:
    • We are mounting a directory (/usr/local/bin) and a file (./dockerhostfile.txt) from the Docker host into the container.
    • The files/directories from the source (docker host) and the target (container) are separated by the colon character (:).
    • The ro flag indicates that the source directory is mounted using the read-only mode (otherwise mounted read-write by default: rw).
    • The source file (docker host) needs to be created in advance (otherwise it will be mounted as a directory).
    • The user that run the container (in this example root) should have write access if needed.
    • If the directories don't exist, they will be automatically created if no permission issues.
    • The created directories will be owned by the user that run the container.
  4. Mount Directories and Files using '--mount' option
    The third option is to use the --mount option to mount a directory and a file from the Docker host.

    Let's do the same with the --mount option:
    $ docker container run --rm \
    --mount type=bind,source=/usr/local/bin,target=/tmp/ulb,readonly \
    --mount type=bind,source=./dockerhostfile.txt,target=/tmp/containerfile.txt \
    ubuntu:latest \
    /bin/bash -c "ls -1 /tmp/ulb | wc -l; echo 'test' > /tmp/containerfile.txt"
    The same notes discussed above apply to the --mount option, with the following exceptions:
    • The files/directories from the source (docker host) and the target (container) are separated by the comma character (,).
    • To indicate a read only mount, we need to use the flag readonly (otherwise mounted read-write by default).
    • The flag source is used for the source file/directory in the Docker host.
    • The flag target is used for the target file/directory in the container.
  5. Mount a Temporary Filesystem Using '--tmpfs' option
    The command "docker container run" provides the option --tmpfs to mount a temporary filesystem.
    The data of a tmpfs filesystem is stored in RAM and lost when the container is stopped.

    Let's check the default "/tmp" folder:
    $ docker container run --rm ubuntu:latest /bin/bash -c "df -h /tmp"
    Filesystem      Size  Used Avail Use% Mounted on
    overlay         507G  5.3G 451G   1% /
    By default the /tmp folder is mounted as the root filesystem. This can be an issue if the container's root filesystem is mounted read only (option --read-only).

    Let's use the the option --tmpfs:
    $ docker container run --rm --tmpfs /tmp ubuntu:latest /bin/bash -c "df -h /tmp"
    Filesystem      Size  Used Avail Use% Mounted on
    tmpfs           7.8G     0  7.8G   0% /tmp
    We can use the --mount option to mount a tmpfs filesystem and set it size:
    $ docker container run --rm --mount type=tmpfs,destination=/tmp,tmpfs-size=1G ubuntu:latest /bin/bash -c "df -h /tmp"
    Filesystem      Size  Used Avail Use% Mounted on
    tmpfs           1.0G     0  1.0G   0% /tmp
© 2025  mtitek