Mount current directory as a volume in Docker on Windows 10

后端 未结 10 969
滥情空心
滥情空心 2020-11-28 01:22

Description

I am using Docker version 1.12.5 on Windows 10 via Hyper-V and want to use container executables as commands in the current path. I buil

相关标签:
10条回答
  • 2020-11-28 01:23

    This works for me in PowerShell:

    docker run --rm -v ${PWD}:/data alpine ls /data
    
    0 讨论(0)
  • 2020-11-28 01:25

    Other solutions for Git Bash provided by others didn't work for me. Apparently there is currently a bug/limitation in Git for Windows. See this and this.

    I finally managed to get it working after finding this GitHub thread (which provides some additional solutions if you're interested, which might work for you, but didn't for me).

    I ended up using the following syntax:

    MSYS_NO_PATHCONV=1 docker run --rm -it -v $(pwd):/usr/src/project gcc:4.9
    

    Note the MSYS_NO_PATHCONV=1 in front of the docker command and $(pwd) - round brackets, lower-case pwd, no quotes, no backslashes.

    Also, I'm using Linux containers on Windows if that matters..

    I tested this in the new Windows Terminal, ConEmu and GitBash, and all of them worked for me.

    0 讨论(0)
  • 2020-11-28 01:29

    In Windows Command Line (cmd), you can mount the current directory like so:

    docker run --rm -it -v %cd%:/usr/src/project gcc:4.9
    

    In PowerShell, you use ${PWD}, which gives you the current directory:

    docker run --rm -it -v ${PWD}:/usr/src/project gcc:4.9
    

    On Linux:

    docker run --rm -it -v $(pwd):/usr/src/project gcc:4.9
    

    Cross Platform

    The following options will work on both PowerShell and on Linux (at least Ubuntu):

    docker run --rm -it -v ${PWD}:/usr/src/project gcc:4.9
    docker run --rm -it -v $(pwd):/usr/src/project gcc:4.9
    
    0 讨论(0)
  • 2020-11-28 01:29

    Here is mine which is compatible for both Win10 docker-ce & Win7 docker-toolbox. At las at the time I'm writing this :).

    You can notice I prefer use /host_mnt/c instead of c:/ because I sometimes encountered trouble on docker-ce Win 10 with c:/

    $WIN_PATH=Convert-Path .
    
    #Convert for docker mount to be OK on Windows10 and Windows 7 Powershell
    #Exact conversion is : remove the ":" symbol, replace all "\" by "/", remove last "/" and minor case only the disk letter
    #Then for Windows10, add a /host_mnt/" at the begin of string => this way : c:\Users is translated to /host_mnt/c/Users
    #For Windows7, add "//" => c:\Users is translated to //c/Users
    $MOUNT_PATH=(($WIN_PATH -replace "\\","/") -replace ":","").Trim("/")
    
    [regex]$regex='^[a-zA-Z]/'
    $MOUNT_PATH=$regex.Replace($MOUNT_PATH, {$args[0].Value.ToLower()})
    
    #Win 10
    if ([Environment]::OSVersion.Version -ge (new-object 'Version' 10,0)) {
    $MOUNT_PATH="/host_mnt/$MOUNT_PATH"
    }
    elseif ([Environment]::OSVersion.Version -ge (new-object 'Version' 6,1)) {
    $MOUNT_PATH="//$MOUNT_PATH"
    }
    
    docker run -it -v "${MOUNT_PATH}:/tmp/test" busybox ls /tmp/test
    
    0 讨论(0)
  • 2020-11-28 01:33

    You need to swap all the back slashes to forward slashes so change

    docker -v C:\my\folder:/mountlocation ...

    to

    docker -v C:/my/folder:/mountlocation ...

    I normally call docker from a cmd script where I want the folder to mount to be relative to the script i'm calling so in that script I do this...

    SETLOCAL
    
    REM capture the path to this file so we can call on relative scrips
    REM without having to be in this dir to do it.
    
    REM capture the path to $0 ie this script
    set mypath=%~dp0
    
    REM strip last char
    set PREFIXPATH=%mypath:~0,-1%
    
    echo "PREFIXPATH=%PREFIXPATH%"
    mkdir -p %PREFIXPATH%\my\folder\to\mount
    
    REM swap \ for / in the path
    REM because docker likes it that way in volume mounting
    set PPATH=%PREFIXPATH:\=/%
    echo "PPATH=%PPATH%"
    
    REM pass all args to this script to the docker command line with %*
    docker run --name mycontainername --rm -v %PPATH%/my/folder/to/mount:/some/mountpoint  myimage %*
    
    ENDLOCAL
    
    0 讨论(0)
  • 2020-11-28 01:39

    For Git Bash for Windows (in ConEmu), the following works for me (for Docker Windows containers):

    docker run --rm -it -v `pwd -W`:c:/api microsoft/dotnet:2-runtime
    

    Note the backticks/backquotes around pwd -W!

    With all other variations of PWD I've tried I've received: "Error response from daemon: invalid volume specification: ..."

    Update: The above was for Docker Windows containers, for Linux containers use:

    docker run --rm -it -v `pwd -W`:/api -p 8080:80 microsoft/aspnetcore:2
    
    0 讨论(0)
提交回复
热议问题