I'm trying to save a file created in a docker image (docker_test.csv) to my local machine.
I have a image that looks like this
FROM python:3.9-alpine3.13
RUN echo hello >> 'docker_test.csv'
I create the image using this line
docker build --tag dockerfile .
I try to run using
docker run -v $(pwd): -p 8080:8080 -it dockerfile
But I get the below error
docker: Error response from daemon: invalid volume specification: '/host_mnt/Users/username/documents :'
I'm not sure where to go from here, I've tried different configurations of paths but everything seems to lead to an error.
CodePudding user response:
You are missing the other half of your volume mount.
The part before the colon is the path to the host system, and the part after the colon is the path inside the container. Well, there is no part after the colon.
-v $(pwd):
It should look something like this.
-v $(pwd):/path/in/container
Although, that would overwrite the data inside the container at that path with the data inside the volume, in that case the files from the host system.
As far as I know, you can specify a volume in such a way that it writes to the volume from the container when it's first mounted instead of overwriting the container's file system with the data inside the volume, which is the default behaviour. This is documented here https://docs.docker.com/storage/volumes/#populate-a-volume-using-a-container
docker run -d \
--name=nginxtest \
--mount source=nginx-vol,destination=/usr/share/nginx/html \
nginx:latest
That example is using a named volume, though. To do that with some arbitrary location on the host system is tricky.
In compose, this type of volume looks like this.
volumes:
myvolume:
driver: local
driver_opts:
type: none
o: bind
device: ./myvolume
To do that, with the CLI, is quite cumbersome. I am not entirely sure how this would look like.
Perhaps, something like the below. You would need to experiment with that and study the linked docs closely.
--mount 'source=myvolume,destination=/path/in/container,volume-driver=local,volume-opt=type=none,volume-opt=o=bind,volume-opt=device=./myvolume'
That said, it would be easier to mount an empty volume to an empty path inside the container, and create the file after it has been mounted, which would result in the file appearing on the host system.
For that, use a CMD instead of a RUN instruction.
FROM python:3.9-alpine3.13
CMD echo hello >> '/output/docker_test.csv'
Then build the image and run it, mounting a volume in standard fashion.
mkdir output
docker run -v "$PWD/output:/output" myimage