I know how local directories can be mounted as volumes with the -v
option in docker run
, i.e.
docker run -v /local/some_folder:/container/some_folder image_name
However, I want to be able to specify the above instruction (to mount the local /local/some_folder
as /container/some_folder
in the container within the Dockerfile.
I've tried using VOLUME /local/some_folder /container/some_folder
in the Dockerfile
, but that didn't seem to work: I am then able to access /container/some_folder
from within the container using docker exec -t container sh
, but the write changes of container to /container/some_folder
are not reflected in /local/some_folder
during container runtime AND after docker stop container
.
To mount a data volume to a container add the --mount flag to the docker run command. It adds the volume to the specified container, where it stores the data produced inside the virtual environment. Replace [path_in_container] with the path where you want to place the data volume in the container.
Use the following command to bind-mount the target/ directory into your container at /app/ . Run the command from within the source directory. The $(pwd) sub-command expands to the current working directory on Linux or macOS hosts. If you're on Windows, see also Path conversions on Windows.
You can manage volumes using Docker CLI commands or the Docker API. Volumes work on both Linux and Windows containers. Volumes can be more safely shared among multiple containers. Volume drivers let you store volumes on remote hosts or cloud providers, to encrypt the contents of volumes, or to add other functionality.
You do not have access to control things like host volume mounts inside the Dockerfile or image build process. Allowing this would allow malicious image creators to make an image that mounts directories on the host without the permission of the admin of that host. A security breach that allowed a popular base image to mount the filesystem could be used to send private data off-site and inject login credentials on countless machines. The only way to mount a volume is at run time at the explicit request of the admin running the container, and to the directory they provide.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With