Why docker volume needs absolute path instead of relative? - docker

See the below example
desktop:~/trash/sample/python/work$ echo "Hello world" > hello
desktop:~/trash/sample/python/work$ docker run --rm -it -v "hello":/home/hello alpine cat /home/hello
cat: read error: Is a directory
desktop:~/trash/sample/python/work$ docker run --rm -it -v "./hello":/home/hello alpine cat /home/hello
docker: Error response from daemon: create ./hello: "./hello" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended to pass a host directory, use absolute path.
See 'docker run --help'.
desktop:~/trash/sample/python/work$ docker run --rm -it -v "$PWD/hello":/home/hello alpine cat /home/hello
Hello world
As you can see only the last option i.e. absolute path works rest are failed.

Related

Got struck in docker run command in windows [duplicate]

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 built a Docker image that is running fine, but I have a problem to mount the current path. The idea is to create an alias and do a docker run --rm [...] command so that it could be used system-wide in the current directory.
Setup
I have a drive E with a folder "test" and in there a folder called "folder on windows host" to show that the command is working. The Dockerfile create the directory /data, defines it as VOLUME and WORKDIR.
Having E:\test as the current directory in PowerShell and executing the Docker command with an absolute path, I can see the content of E:\test:
PS E:\test> docker run --rm -it -v E:\test:/data mirkohaaser/docker-clitools ls -la
total 0
drwxr-xr-x 2 root root 0 Jan 4 11:45 .
drwxr-xr-x 2 root root 0 Jan 5 12:17 folder on windows host
Problem
I want to use the current directory and not an absolute notation. I could not use pwd in the volume because of different error messages:
Trying with ($pwd)
PS E:\test> docker run --rm -it -v ($pwd):/data mirkohaaser/docker-clitools ls -la
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error parsing reference: ":/data" is not a valid repository/tag.
See 'C:\Program Files\Docker\Docker\Resources\bin\docker.exe run --help'.
Trying with /($pwd)
PS E:\test> docker run --rm -it -v /($pwd):/data mirkohaaser/docker-clitools ls -la
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error parsing reference: "E:\\test" is not a valid repository/tag.
See 'C:\Program Files\Docker\Docker\Resources\bin\docker.exe run --help'.
Trying with \´pwd\´
PS E:\test> docker run --rm -it -v ´$pwd´:/data mirkohaaser/docker-clitools ls -la
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response from daemon: Invalid bind mount spec "´E:\\test´:/data": invalid mode: /data.
See 'C:\Program Files\Docker\Docker\Resources\bin\docker.exe run --help'.
Trying with `pwd`
PS E:\test> docker run --rm -it -v `$pwd`:/data mirkohaaser/docker-clitools ls -la
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response from daemon: create $pwd: "$pwd" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed.
See 'C:\Program Files\Docker\Docker\Resources\bin\docker.exe run --help'.
What is the correct syntax of mounting the current directory as a volume in Docker on Windows 10?
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
This works for me in PowerShell:
docker run --rm -v ${PWD}:/data alpine ls /data
Command prompt (Cmd.exe)
When the Docker CLI is used from the Windows Cmd.exe, use %cd% to mount the current directory:
echo test > test.txt
docker run --rm -v %cd%:/data busybox ls -ls /data/test.txt
Git Bash (MinGW)
When the Docker CLI is used from the Git Bash (MinGW), mounting the current directory may fail due to a POSIX path conversion: Docker mounted volume adds ;C to end of windows path when translating from linux style path.
Escape the POSIX paths by prefixing with /
To skip the path conversion, POSIX paths have to be prefixed with the slash (/) to have leading double slash (//), including /$(pwd)
touch test.txt
docker run --rm -v /$(pwd):/data busybox ls -la //data/test.txt
Disable the path conversion
Disable the POSIX path conversion in Git Bash (MinGW) by setting MSYS_NO_PATHCONV=1 environment variable at the command level
touch test.txt
MSYS_NO_PATHCONV=1 docker run --rm -v $(pwd):/data busybox ls -la /data/test.txt
or shell (system) level
export MSYS_NO_PATHCONV=1
touch test.txt
docker run --rm -v $(pwd):/data busybox ls -la /data/test.txt
Open Settings on Docker Desktop (Docker for Windows).
Select Shared Drives.
Select the drive that you want to use inside your containers (e.g., C).
Click Apply. You may be asked to provide user credentials.
The command below should now work on PowerShell (command prompt does not support ${PWD}):
docker run --rm -v ${PWD}:/data alpine ls /data
IMPORTANT: if/when you change your Windows domain password, the mount will stop working silently, that is, -v will work but the container will not see your host folders and files. Solution: go back to Settings, uncheck the shared drives, Apply, check them again, Apply, and enter the new password when prompted.
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
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
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.
This command should fix it.
docker run --rm -it -v ${PWD}:c:\data mirkohaaser/docker-clitools
{PWD} is the host current folder. after the : is the container folder.
If the mounting is correct then files will be listed in the director c:\data in the container.
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
If you are still having this issue in 2022, you can install docker in windows with WSL(Windows Subsystem for Linux). Then you can go on Microsoft Store and install one of the Linux project like Ubuntu, Debian or Kali Linux.
On Docker Desktop go to setting -> WSL integration
and enable your version of Linux.
On VS Code open new WSL terminal and execute the Linux command there.
If you want to pass your project directory with the DockerfileRunArguments property to your debug container, then pwd won't work.
<PropertyGroup>
<!-- Will result in `` -->
<DockerfileRunArguments>-v "$(pwd):/data:ro"</DockerfileRunArguments>
</PropertyGroup>
Use $(MSBuildProjectDirectory) instead of $(pwd)
<PropertyGroup>
<!-- Will result in the full path to your project directory -->
<DockerfileRunArguments>-v "$(MSBuildProjectDirectory):/data:ro"</DockerfileRunArguments>
</PropertyGroup>
Reference: MSDocs - Visual Studio Container Tools
PowerShell on Windows 10 Pro
The above solutions did not work for me as plain pwd gives a description in the response:
Path
----
C:\Users\barnaby
It needs outputting as a variable in the script $(pwd) but then docker complains invalid reference format
The solution is to wrap the whole switch parameters in double quotes and this works for me:
docker run --rm -v "$(pwd):/app" php:7.4-cli php /app/hello.php
docker run --rm -v /c/Users/Christian/manager/bin:/app --workdir=/app php:7.2-cli php app.php
Git bash
cd /c/Users/Christian/manager
docker run --rm -v ${PWD}:/app --workdir=/app php:7.2-cli php bin/app.php
echo ${PWD}
result:
/c/Users/Christian/manager
cmd or PowerShell
cd C:\Users\Christian\manager
echo ${PWD}
result:
Path
---- C:\Users\Christian\manager
as we see in cmd or PowerShell $ {PWD} will not work

Docker volume mapping folder issue on Windows 10

I am trying to run the following command
docker run -p 3000:3000 -v/app/node_modules -v $(pwd):/app 2ef0206fcf99
I am getting the following error
docker: Error response from daemon: create $(pwd): "$(pwd)" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended to pass a host directory, use absolute path.
How can I fix the issue?
1) Using Windows Powershell, following works for me:
docker run --rm -it -v ${pwd}:/mydir nginx:latest bash
Note:
I have used curly braces around pwd instead of small braces
2) Using Git Bash, following syntax should work:
winpty docker run --rm -it -v "/$PWD":/mydir nginx:latest bash
Note:
If you do not use winpty at the start of the command, you will get error message: the input device is not a TTY. If you are using mintty, try prefixing the command with 'winpty'
Also notice the / before $PWD. Without the /, it will not throw error but i noticed that it didn't mount the directory.
I also had the same issue on windows make sure that you put "$PWD" something like this so your command should be something like this
docker run --rm -it -p 3000:3000 -v "$PWD:/app" 2ef0206fcf99
or another way is
docker run --rm -it -p 3000:3000 --volume="$PWD:/app" 2ef0206fcf99

Windows docker, specifying named volume under winpty command line causing includes invalid characters problem

I want to run docker in Git Bash under Windows.
If use docker run --rm -ti -v my-vol:/myvol my_volume_test:latest under Cmd/Powershell, all is good.
But to use docker under Git Bash, I need to prefix the command with winpty, that's where the problem comes in:
$ winpty docker run --rm -ti -v my-vol:/myvol my_volume_test:latest
C:/Program Files/Docker/Docker/resources/bin/docker.exe: Error response from daemon: create my-vol;C: "my-vol;C"
includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended
to pass a host directory, use absolute path.
See 'C:/Program Files/Docker/Docker/resources/bin/docker.exe run --help'.
I tried to escape the ':' character, but that didn't work either:
$ winpty docker run --rm -ti -v my-vol\:/myvol my_volume_test:latest
C:/Program Files/Docker/Docker/resources/bin/docker.exe: Error response from daemon: create my-vol;C: "my-vol;C" i
ncludes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended
to pass a host directory, use absolute path.
See 'C:/Program Files/Docker/Docker/resources/bin/docker.exe run --help'.

docker volume during docker run

I am trying to mount library present in the container into docker volume during docker run . The command is as below:
docker run -d --name mbus-docker -it --rm --mount source=/mbus/lib/libMurata.a,target=/mbus_volume mbus-docker
I have verified by execing into the container that the library is present in path /mbus/lib/libMurata.a
When I try to mount the library on to volume.
I am getting the below error:
docker: Error response from daemon: create /mbus/lib: "/mbus/lib" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended to pass a host directory, use absolute path.
If you want to mount /mbus/lib/libMurata.a onto /mbus_volume path inside container then specify the type for mount as bind.
Your docker run command should be
docker run -d --name mbus-docker -it --rm --mount type=bind,source=/mbus/lib/libMurata.a,target=/mbus_volume/ mbus-docker
This will mount /mbus/lib/libMurata.a onto /mbus_volume/ folder.
The error you got "/mbus/lib" includes invalid characters for a local volume name says /mbus/lib is invalid volume name. Because the default bind type for mount option is type volume. In this case it will try to create a volume locally on your system with the name /mbus/lib which is an invalid volume name.
Please go through this.
Hope this helps.
Update:
If volume named mbus_volume exists on your host. Then try this:
docker run -d --name mbus-docker -it --rm --mount type=volume,source=mbus_volume,target=/mbus/lib/ mbus-docker
you can just use:
docker run -d --name mbus-docker -it --rm -v /mbus/lib/libMurata.a:/mbus_volume/libMurata.a mbus-docker

Volume path or Mount in Windows container

Description
I got error "Error response from daemon: invalid mount config for type "volume": invalid mount path" in Windows Container
Steps to reproduce the issue:
1. DockerFile
FROM microsoft/aspnetcore-build AS base
WORKDIR /app
ENTRYPOINT [ "dotnet", "Test.dll" ]
Run command docker build -t docker-vol .
Run Command docker run -it -p 8001:80 -v D:\Projects\Docker\publish:c:/app --name docker-vol-test docker-vol (This works)
Run Command docker run -it -p 8001:80 --mount "source=D:\Projects\Docker\publish,target=c:/app" --name docker-vol-test docker-vol This fails with Error response from daemon: invalid mount config for type "volume": invalid volume name
I am wondering how to work with --mount and whether it is possible to use relative path instead of absolute path with -v?
You are using a bind mount, but because you have not specified a type, then it has defaulted to volume. In this case, source must be the name of the volume, or omitted for an anonymous volume.
Because you have give a path instead, you are getting this error. If you add a type key to your command, it should work:
docker run -it -p 8001:80 --mount 'type=bind, source="D:\Projects\Docker\publish", target="c:/app"' --name docker-vol-test docker-vol
In answer to your second point, bind mounts require an absolute path. The usual way to use a relative path in Linux-land is to prepend the path with $PWD. In Windows, the equivalent of SPWD would be %cd%, so if you were running from D:\Projects\Docker, then the above would probably be:
docker run -it -p 8001:80 --mount 'type=bind, source="%cwd%\publish", target="c:/app"' --name docker-vol-test docker-vol
Note that I have no experience of Docker under Windows, but I believe the above should work.
The above is correct but if you want to use -v instead the syntax is: docker run -v C:\SomePath:C:\app\somePath image
Note the path must exist or the command fails.

Resources