Bootstrap FreeKB - Docker - Mount a file or directory in a container using the docker run -v or --volume command
Docker - Mount a file or directory in a container using the docker run -v or --volume command

Updated:   |  Docker articles

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:

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.

  1. The name of a Docker volume or the absolute directory to where data will be stored on the Docker system.
  2. The path to where the volume will be mounted in the container.
  3. 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)
  • consistent
  • delegated
  • cached
  • rprivate
  • private
  • rshared
  • shared
  • rslave
  • slave
  • nocop
docker run --volume /usr/local/docker/foo:/var/log:ro foo:latest

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter f87ce1 in the box below so that we can be sure you are a human.