Volume path or Mount in Windows container - docker

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.

Related

Why docker volume needs absolute path instead of relative?

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.

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

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

Docker how to pass a relative path as an argument

I would like to run this command:
docker run docker-mup deploy --config .deploy/mup.js
where docker-mup is the name the image, and deploy, --config, .deploy/mup.js are arguments
My question: how to mount a volume such that .deploy/mup.js is understood as the relative path on the host from where the docker run command is run?
I tried different things with VOLUME but it seems that VOLUME does the contrary: it exposes a container directory to the host.
I can't use -v because this container will be used as a build step in a CI/CD pipeline and as I understand it, it is just run as is.
I can't use -v because this container will be used as a build step in a CI/CD pipeline and as I understand it, it is just run as is.
Using -v to expose your current directory is the only way to make that .deploy/mup.js file inside your container, unless you are baking it into the image itself using a COPY directive in your Dockerfile.
Using the -v option to map a host directory might look something like this:
docker run \
-v $PWD/.deploy:/data/.deploy \
-w /data \
docker-mup deploy --config .deploy/mup.js
This would map (using -v ...) the $PWD/.deploy directory onto /data/.deploy in your container, set the current working directory to /data (using -w ...), and then run deploy --config .deploy/mup.js.
Windows - Powershell
If you're inside the directory you want to bind mount, use ${pwd}:
docker run -it --rm -d -p 8080:80 --name web -v ${pwd}:/usr/share/nginx/html nginx
or $pwd/. (forward slash dot):
docker run -it --rm -d -p 8080:80 --name web -v $pwd/.:/usr/share/nginx/html nginx
Just $pwd will cause an error:
docker run -it --rm -d -p 8080:80 --name web -v $pwd:/usr/share/nginx/html nginx
Variable reference is not valid. ':' was not followed by a valid variable name character. Consider using ${} to
delimit the name
Mounting a subdirectory underneath your current location, e.g. "site-content", $pwd/ + subdir is fine:
docker run -it --rm -d -p 8080:80 --name web -v $pwd/site-content:/usr/share/nginx/html nginx
In my case there was no need for $pwd, and using the standard current folder notation . was enough. For reference, I used docker-compose.yml and ran docker-compose up.
Here is a relevant part of docker-compose.yml.
volumes:
- '.\logs\:/data'

Cannot mount volume in docker container when directory name contains colon

I cannot mount a volume to a docker container when the directory name contains a colon (:)
The name of the directory is 2012-08-05-00:16:37 and I prefer not renaming the directory. I tried:
docker run -it --name test1 \
-v /host_system_path/2012-08-05-00\:16\:37/:/container_path/2012-08-05-00\:16\:37/
image_name
I get the error:
docker: Error response from daemon: invalid bind mount spec.See
'docker run --help'.
If I rename the directory without spaces or only with hyphens, then the directory is mounted into the container without any issues. Can someone point out how can I solve the problem when the directory contains a colon.
I am on Ubuntu:16.04 and Docker version 17.06.0-ce.
Colons are currently not supported when specifying directory mappings via -v, and it seems you cannot escape them either.
You need to leverage --mount instead:
docker run ... --mount type=bind,source=/some:colon:file,destination=/container-path ...
In the worst of cases, you may of course alternatively still work around this limitation with a temporary system link (ln -s) or rename the target directory temporarily.
It's an open issue with Docker. But in your case, why would docker run -it --name test1 -v /host_system_path:/container_path image_name not be sufficient?

Resources