mount a host volume to a container created through Dockerfile

New to docker, and as per the documentation about Dockerfile, due to portability, it is not allowed to specify a host volume mapping. That is fine, but is there a way to map a host volume (I am in MAC, so say, my home dir /Users/bsr to /data of ubuntu image) to a linux container. The documentation of docker volume is talking only about docker run, but not sure how to add a volume after creating it.

http://docs.docker.com/userguide/dockervolumes/

  • Docker image load failed on mac, error is 'invalid argument'
  • Gitlab CI: How to run tests in pipeline using docker and shell runner
  • nginx doesn't forward to django app in docker-compose in ubuntu but it all works in docker-machine in Mac
  • How to give my desktop Ubuntu machine's user full permissions to a docker volume, in addition to TWO users within the docker container?
  • docker ubuntu /bin/sh: 1: locale-gen: not found
  • Docker - Failed to connect to localhost port 4000: Connection refused
  • Docker + Kitematic NAT container
  • Docker can't connect containers
  • Use docker-compose with docker swarm
  • Not able to replace NGINX with NGINX Plus as reverse proxy for microservices on Google Cloud using Kubernetes
  • Serving Rails' precompiled assets using nginx in Docker
  • How to import mysql database using dokku
  • 2 Solutions collect form web for “mount a host volume to a container created through Dockerfile”

    On Linux you can simply mount a directory of your host system to a docker container by passing

    -v /path/to/host/directory:/path/to/container/directory
    

    to the docker run command.

    You can also see it here in the documentation: https://docs.docker.com/userguide/dockervolumes/#mount-a-host-directory-as-a-data-volume

    If you are using boot2docker things are more complicated. The problem ist that boot2docker runs a little linux vm to start docker. So if you mount the volume as described above you will mount the directory of the little linux vm.

    A workaround for this is described in the README of the boot2docker GitHub page using a samba share:

    https://github.com/boot2docker/boot2docker#folder-sharing

    the following worked, with the help of @sciutand.

    git clone https://github.com/boot2docker/boot2docker.git
     cd boot2docker/
     docker build -t my-boot2docker-img .
     docker run --rm my-boot2docker-img > boot2docker.iso
     boot2docker stop
     mv ~/.boot2docker/boot2docker.iso ~/.boot2docker/boot2docker.iso.backup
     mv boot2docker.iso ~/.boot2docker/boot2docker.iso
     VBoxManage sharedfolder add boot2docker-vm -name /Users -hostpath /Users
     boot2docker up
     docker run -d -P --name web ubuntu
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.