How to mount a host directory in a Docker container

后端 未结 25 1602
庸人自扰
庸人自扰 2020-11-22 12:43

I am trying to mount a host directory into a Docker container so that any updates done on the host is reflected into the Docker containers.

Where am I doing somethin

25条回答
  •  無奈伤痛
    2020-11-22 13:00

    [UPDATE] As of ~June 2017, Docker for Mac takes care of all the annoying parts of this where you have to mess with VirtualBox. It lets you map basically everything on your local host using the /private prefix. More info here. [/UPDATE]

    All the current answers talk about Boot2docker. Since that's now deprecated in favor of docker-machine, this works for docker-machine:

    First, ssh into the docker-machine vm and create the folder we'll be mapping to:

    docker-machine ssh $MACHINE_NAME "sudo mkdir -p \"$VOL_DIR\""
    

    Now share the folder to VirtualBox:

    WORKDIR=$(basename "$VOL_DIR")
    vboxmanage sharedfolder add "$MACHINE_NAME" --name "$WORKDIR" --hostpath "$VOL_DIR" --transient
    

    Finally, ssh into the docker-machine again and mount the folder we just shared:

    docker-machine ssh $MACHINE_NAME "sudo mount -t vboxsf -o uid=\"$U\",gid=\"$G\" \"$WORKDIR\" \"$VOL_DIR\""
    

    Note: for UID and GID you can basically use whatever integers as long as they're not already taken.

    This is tested as of docker-machine 0.4.1 and docker 1.8.3 on OS X El Capitan.

提交回复
热议问题