1

I want to mount a folder of the host system to the container and it need to be defined in the Dockerfile so that user doesn't need to do it manually by passing the argument in the command line to run the container. How to achieve this ?

Suneet Jain
  • 216
  • 1
  • 3
  • 17
  • 2
    While creating an image (using Dockerfile and `docker build`), you can only copy the content of your host file system to the container or declare there is a volume inside your container that will be mount later. But you cannot create the link between the container and the host at that step, because an image is not a running container and don't forget you can sent to another docker host the image without any modification. You should look for docker-compose if you want an easy way to declare there is a volume to mount. – Ser May 29 '18 at 13:46

2 Answers2

3

This simply cannot be done. Docker images are designed to be portable. Host mounts are host specific. Thus if you are able to specify a host mount at build time, it will make the image non-portable across machine that don't have this mount folder. Thus this is why this option is not available.

You can use docker compose to help the user not choose the mount folder. Take a look at How do I mount a host directory as a volume in docker compose

yamenk
  • 46,736
  • 10
  • 93
  • 87
  • Thanks for explaining the rationale. It makes sense as much as I wish I could mount host-specific dirs. This is a tradeoff to get the "it just works" behaviour that we all enjoy from docker. Time for me to edit my wrapper shell script. – Sridhar Sarnobat Jul 16 '22 at 19:48
0

Dockerfile is for create images not containers.

You can not define a volume on a image. The volume must be defined on execution time when the container is created.

docker run -v /host-folder:/root/containerfolder -i -t  imagename
Hemerson Varela
  • 24,034
  • 16
  • 68
  • 69