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/

  • How to use local docker images in convox?
  • How create MongoDB cluster on Kubernetes?
  • How to remove docker images based on name?
  • Changing path of docker containers in host system
  • Error while running a container using docker
  • How to backup MongoDB in a setup with multiple Docker containers?
  • How to pass parameters while creating consul client agent inside a Docker container?
  • Docker for Mac: Using Persistent Storage
  • How to use Docker link in Ansible when link var defined
  • Docker build, how to show progress?
  • How to change owner of Docker for Windows
  • Can a docker build use the url of a git branch?
  • 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.