What's the best way to share files from Windows to Boot2docker VM?
Asked Answered
R

5

31

I have make my code ready on Windows, but I find it's not easy to share to boot2docker.

I also find that boot2docker can't persistent my changes. For example, I create a folder, /temp, after I restart boot2docker. This folder disappears, and it's very inconvenient.

What is your way when you have some code on Windows, but you need to dockerize them?

---update---

I try to update the setting in VirtualBox and restart boot2docker, but it's not working on my machine.

Enter image description here

docker@boot2docker:/$ ls -al /c
total 4
drwxr-xr-x    3 root     root            60 Jun 17 05:42 ./
drwxrwxr-x   17 root     root           400 Jun 17 05:42 ../
dr-xr-xr-x    1 docker   staff         4096 Jun 16 09:47 Users/
Randers answered 16/6, 2015 at 10:3 Comment(0)
C
63

Boot2Docker is a small Linux VM running on VirtualBox. So before you can use your files (from Windows) in Docker (which is running in this VM), you must first share your code with the Boot2Docker VM itself.

To do so, you mount your Windows folder to the VM when it is shutdown (here a VM name of default is assumed):

C:/Program Files/Oracle/VirtualBox/VBoxManage sharedfolder \
add default -name win_share -hostpath c:/work

(Alternatively you can also open the VirtualBox UI and mount the folder to your VM just as you did in your screenshot!)

Now ssh into the Boot2Docker VM for the Docker Quickstart Terminal:

docker-machine ssh default

Then perform the mount:

  1. Make a folder inside the VM: sudo mkdir /VM_share
  2. Mount the Windows folder to it: sudo mount -t vboxsf win_share /VM_share

After that, you can access C:/work inside your Boot2Docker VM:

cd /VM_share

Now that your code is present inside your VM, you can use it with Docker, either by mounting it as a volume to the container:

docker-machine ssh default
docker run --volume /VM_share:/folder/in/container some/image

Or by using it while building your Docker image:

...
ADD /my_windows_folder /folder
...

Circosta answered 16/6, 2015 at 10:50 Comment(7)
Ah, sorry, I forgot one step: So you have changed the settings of your VM like in the screenshot you posted. Now login to your VM via SSH and make a new directory: sudo mkdir /my_windows_folder. And now you mount your folder: sudo mount -t vboxsf mountedFolderNameInVirtualBox /my_windows_folder. I update my post ;)Circosta
It works after ran the mount command. I can't understand that why I need to manually run the mount command, since I have set Auto-mount in VM setting.Randers
didnt work for me. but the mount.vboxsf command worked for me.Aside
One way to avoid extra steps is to use Users or /Users as a share/folder name in VirtualBox. Boot2Docker automatically mounts some predefined VBox share names. c/Users is automatically mapped to C:\Users but Users (without c/) can still be mapped to C:\work in VBox GUI. Run docker run -v Users:/code ... and now /code in Docker container maps to C:\work.Daren
Does the mount steps (sudo mkdir... & sudo mount ...) need to be done everytime when the 'boot2docker' VM is run? the mount doesn't seem to stick. Will it possible to make it automatic.Expire
@Daren thanks for the tip on using Users for the folder name. the automount works wellConcord
I get mount: permission deniedAwildaawkward
S
9

See this answer.

I have Windows 10 Home edition with Docker toolbox 1.12.2 and VirtualBox 5.1.6.

I was able to mount a folder under C:\Users successfully in my container without doing any extra steps such as docker-machine ssh default.

Example:

docker run -it --rm -v /c/Users/antonyj/Documents/code:/mnt ubuntu /bin/bash

So having your files under C:\Users probably is the simplest thing to do.

If you do not want to have your files under C:\Users, then you have to follow the steps in the accepted answer.

Sherborne answered 22/11, 2016 at 20:18 Comment(0)
B
1

Using Docker Toolbox, the shared directory can only be /c/User:

Invalid directory. Volume directories must be under your Users directory

Enter image description here

Step1&Step2 Command in the "Docker Quickstart Terminal" in the implementation of Step1 & Step2 can be:

# Step 1. VirtualBox. Add the error in the command line, in the VirtualBox image interface manually add, as shown above.
"C:/Program Files/Oracle/VirtualBox/VBoxManage.exe" sharedfolder add default --name "E_DRIVE" --hostpath "e:\\" --automount

# Try 1. Only a temporary effect. Restart VM after sharing failure.
#docker-machine ssh default "sudo mkdir -p /e" # Create a directory identifier, consistent with the Windows drive letter
#docker-machine ssh default "sudo mount -t vboxsf -o uid=1000,gid=50 E_DRIVE /e"

# Try 2. Modify /etc/fstab. Do not use the permanent mount. Each restart /etc/fstab content will be reset
#docker-machine ssh default "sudo sed -i '$ a\E_DRIVE /e vboxsf uid=1000,gid=50 0 0' /etc/fstab"

# Step 2. `C:\Program Files\Docker Toolbox\start.sh` https://github.com/docker/machine/issues/1814#issuecomment-239957893
docker-machine ssh default "cat <<EOF | sudo tee /var/lib/boot2docker/bootlocal.sh && sudo chmod u+x /var/lib/boot2docker/bootlocal.sh
#!/bin/sh
mkdir -p /e
mount -t vboxsf -o uid=1000,gid=50 E_DRIVE /e
EOF
"

Then restart the VM. Try this: docker run --name php-fpm --rm -it -v /e:/var/www/html php:7.1.4-fpm /bin/bash

References:

Bedfellow answered 1/6, 2017 at 3:27 Comment(0)
W
1

In the System Tray, you should have the cute Docker whale swimming. Right click and select Settings.

Enter image description here

Click on Apply. This will bring up the Credentials dialog and you will need to provide your current Windows credentials. Ensure that you give it correctly. I also suspect that you might need to be an administrator.

To mount our host directory (C:\data) in a container, we are going to use the -v (volume) flag while running the container. A sample run is shown here:

Enter image description here

I have CentOS in my local Docker container.

docker run -v c:/data:/data **centos** ls /data
Willed answered 12/10, 2017 at 17:43 Comment(1)
I don't think this is available in current versions of Docker for Windows.Capitulum
K
1

Mount shared folder Windows guest with Linux host (vm name 'default'):

Shutdown 'default' VM:

cd "C:\Program Files\Oracle\VirtualBox"
VBoxManage controlvm default poweroff

Add shared folder command line:

./VBoxManage sharedfolder add default -name win_share -hostpath "C:\docker\volumes"

Start VM (headless only command line interface):

/VBoxManage startvm headless  default

Connect to ssh:

docker-machine ssh default

Create VM sharedfolder directory:

sudo mkdir /sharedcontent

Mount Windows folder to host VM:

sudo mount -t vboxsf win_share /sharedcontent
Krystakrystal answered 10/8, 2018 at 10:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.