-v, --volume list Bind mount a volume --mount mount Attach a filesystem mount to the container
$ docker volume create volume1
volume1Let'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
$ 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:
$ 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:
$ 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).
$ 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% /tmpWe 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