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

前端 未结 5 1531
深忆病人
深忆病人 2020-12-02 08:51

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 cr

相关标签:
5条回答
  • 2020-12-02 08:56

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

    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:

    I have CentOS in my local Docker container.

    docker run -v c:/data:/data **centos** ls /data
    
    0 讨论(0)
  • 2020-12-02 08:59

    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.

    0 讨论(0)
  • 2020-12-02 09:05

    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:

    • What's the best way to share files from Windows to Boot2docker VM?
    • http://hessian.cn/p/1502.html
    • Windows + Boot2Docker, How to add D:\ drive to be accessible from within docker?
    0 讨论(0)
  • 2020-12-02 09:14

    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
    ...
    

    0 讨论(0)
  • 2020-12-02 09:19

    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
    
    0 讨论(0)
提交回复
热议问题