I'd like to mount a file from a Docker's container to my docker host.
Data volumes is not the solution for me, as these are mounts from the docker host to docker containers, and I need the opposite way around.
Thanks
When docker mounts a volume, it overlays the directory inside the container with that of the volume. There is an exception where it will initialize a named volume with the content of that directory in the image. There's no other built in method to copy files out of the image to the volume.
Therefore, to go the other direction and copy the contents of the image directory out to the host with a host volume, you'll need to add your own copy command inside the container. That can be part of your entrypoint script that runs in the container.
An example of the entrypoint script is the volume caching scripts in my base image. One script moves the files to a cached location inside the image during the build, and a second script copies files from the image cached location to the volume mount in the entrypoint.
Related
I'd like to mount a whole docker image as a volume in the host. I could run the image, but I don't really need to, I just need the files to be visible as files within a directory/volume/mount point outside of docker.
(To be clear, I don't need to mount a host directory as a volume in a running docker instance which is what docker run -v does; I need the opposite, to mount a directory in the docker image or the whole image as a volume in the host; read-only is okay)
I want to mount my host path (or docker volume) to the path in the image from dockerfile.
Dockerfile can copy the host data or directory to the data or directory in the image.
But I want to bind or mount not copy!
It is similar to "docker run -v" option but I wonder that it is done by creating the image from dockerfile.
Is any solution for this issue?
No, as far as I am aware, a host path cannot be mounted via the Dockerfile due to the portability of docker images and the different host architectures/directory layouts etc. See dockerfile volumes note 4.
Using VOLUME within the dockerfile will create a docker volume on the host at run-time of the container, but it cannot be specified to be a host directory. This answer explains the use of dockerfile VOLUME quite well. To use a host directory, you will need to do it at run time.
I am creating a Docker Image from a base image that in one of its early layers mounts a volume to the current user home folder utilizing the VOLUME directive.
This mount leads to some unwanted file removals on my own image once it is run.
Is there a way to unmount this volume from within the Dockerfile of my image?
This experiment tries to build a container using this Docker file:
FROM lambdalinux/baseimage-amzn:2016.09-000
COPY ./bundle /opt/bundle/
VOLUME /bundle
Then inside the container, create a /opt/bundle/file.txt and put some text in it. But that file did not show up in the bundle directory on the host as I expected after reading Should I include my code with COPY/ADD or a volume last paragraph:
There may be cases where you’ll want to use both. You can have the image include the code using a COPY, and use a volume in your Compose file to include the code from the host during development. The volume overrides the directory contents of the image.
Doesn't Dockerfile VOLUME do the same as docker-compose.yml VOLUME? If so, how can this be done so that changes in the host directory is reflected inside the container directory in this case?
I also created a file on the host bundle/play.txt but that did not show up inside the container /opt/bundle/...
A VOLUME instruction in the dockerfile creates a mount point but initially only maps it to Docker's internal data directory.
In order to map the volume to the host filesystem, you need to specify which path on the host should be mapped to the volume. You can do this in the docker-compose file using the volumes parameter. (Note: you can create volumes using docker-compose without declaring them in the Dockerfile.)
Note that when mapping a directory from the host to a container, the directory contents on the host will replace the contents in the container, not vice versa.
Docker run command has option to mount host directory into container
-v=[]: Create a bind mount with: [host-dir]:[container-dir]:[rw|ro].
If "host-dir" is missing, then docker creates a new volume.
And Dockerfile has VOLUME instruction
VOLUME ["/data"] - The VOLUME instruction will add one or more new volumes
to any container created from the image.
From what I see, there is no way to specify host-dir or rw/ro status when using Dockerfile.
Is there any other use of VOLUME in docker file other than wanting to share it with some other container?
Dockerfiles are meant to be portable and shared. The host-dir volume is something 100% host dependent and will break on any other machine, which is a little bit off the Docker idea.
Because of this, it is only possible to use portable instructions within a Dockerfile. If you need a host-dir volume, you need to specify it at run-time.
A common usage of VOLUME from Dockerfile is to store configuration or website sources so that it can be updated later by another container.