The --mount or --volume option can be used to:
- mount a file or directory on your Docker system to a file or directory in the container
- mount an NFS share in the container
You can use:
- the -m or --mount option
- the -v or --volume option (this article)
For example, let's say you have created a PHP container on Docker. You could mount the /usr/local/docker/images directory on the Docker system to /var/www/images as a volume in the PHP container.
As an example, let's say you have a container named foo, and you want to store the data in the /usr/local/docker/foo/logs on your Docker server in the /var/log directory in the foo container. Let's break down this command.
- The docker run command is used
- The --volume option is used to define the directory on the Docker server (/usr/local/docker/foo) that will be mounted to a directory in the container (/var/log).
- The foo:latest image is used
docker run --volume /usr/local/docker/foo:/var/log foo:latest
Or, a file on the Docker system can be mounted to a file in the container.
docker run --volume /usr/local/docker/foo.txt:/tmp/foo.txt foo:latest
Or, the name of a Docker volume can be used.
docker run --volume volume001:/var/log foo:latest
Three colon separated values are used with the -v or --volume command line option.
- The name of a Docker volume or the absolute directory to where data will be stored on the Docker system.
- The path to where the volume will be mounted in the container.
- Optional - Comma separated permissions for the mount, such as ro (read only)
Here is an example of how you would mount as volume as ro (read only). The following mount options can be used.
- ro (read only)
- rw (read write)
docker run --volume /usr/local/docker/foo:/var/log:ro foo:latest