1

I was trying to mount host volume(windows 10) from dockerfile. I was using the command:

docker run -v <host-dir>:<container-dir> <image-name>

from docker terminal. It was working fine from docker terminal. But when I tried to do the same from dockerfile execution it's getting stopped because of this command. Please help how to mount host volume from dockerfile.

sf_
  • 1,138
  • 2
  • 13
  • 28
Rajesh
  • 41
  • 1
  • 1
    https://docs.docker.com/engine/reference/builder/#volume – Héctor Jul 25 '17 at 06:04
  • command which i was using is: docker run -v /: image-name – Rajesh Jul 25 '17 at 06:07
  • And your Dockefile and `docker build` command? – zigarn Jul 25 '17 at 06:08
  • The [answer](https://stackoverflow.com/a/26053710) seems to be that it is not possible to do this from `Dockerfile` , but see that page for other options. The [documentation](https://docs.docker.com/storage/volumes/) shows how to do it with `docker-compose` but I've not seen mention of how to achieve the same with `Dockerfile`, which is meant for building images. – Nagev Sep 14 '22 at 16:00

1 Answers1

0

What exactly have you written in your Dockerfile?

For Dockerfiles there exists a VOLUME instruction, which can probably help you.

I recommend reading the following 2 links for clarification on Docker volumes:

Official Docker documentation

Helpful blogpost

Edit: I may have found another post which could help you immensely: Docker volume and mount

samprog
  • 2,454
  • 1
  • 13
  • 18
  • i was trying to run this command from dockerfile: RUN -v /c/users/rajesh:/var/test imagename – Rajesh Jul 25 '17 at 06:44
  • Sadly this doesn't work, as the RUN-Instruction in the Dockerfile isn't the same as the command "docker run". The RUN-Instruction acts as if you were typing your commands on a shell, while the "docker run"-command starts up a new container. I highly recommend you reading the official guide on how to write Dockerfiles (https://docs.docker.com/engine/reference/builder/) PS: Sorry for spamming you with links, I think the current one will help you the fastest – samprog Jul 25 '17 at 06:56
  • is there a way to mount host dir from dockerfile? – Rajesh Jul 25 '17 at 07:12
  • If you use "VOLUME /data" for example, it will mount the /data directory inside the container to "/var/lib/docker/volumes/[random hash]/_data/" on your docker-host. So it is mounted on the host. You can't mount to a specific directory though, as every host can be different and the image might not work on other hosts (since the mounted directory could not exist). – samprog Jul 25 '17 at 07:40