docker volume during docker run - docker

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

Related

invalid argument for "--mount" flag: unexpected key 'addr'

I want to use docker nfs volume.
What I have tried:
1. Create a volume first then use it, it's OK
docker volume create --driver local --opt type=nfs --opt o=nfsvers=4,addr=10.192.244.109 --opt device=:/var/lib/lava/dispatcher/tmp my1
docker run -it --rm --name nfs-test -v my1:/data alpine sh
2. Directly use volume when docker run, it's also OK
docker run -it --rm --name nfs-test --mount type=volume,volume-driver=local,dst=/data,volume-opt=type=nfs,volume-opt=device=:/var/lib/lava/dispatcher/tmp,"volume-opt=o=addr=10.192.244.109" alpine sh
The problem happens when I want to specify nfsvers=4 in docker run:
# docker run -it --rm --name nfs-test --mount type=volume,volume-driver=local,dst=/data,volume-opt=type=nfs,volume-opt=device=:/var/lib/lava/dispatcher/tmp,"volume-opt=o=nfsvers=4,addr=10.192.244.109" alpine sh
invalid argument "type=volume,volume-driver=local,dst=/data,volume-opt=type=nfs,volume-opt=device=:/var/lib/lava/dispatcher/tmp,volume-opt=o=nfsvers=4,addr=10.192.244.109" for "--mount" flag: unexpected key 'addr' in 'addr=10.192.244.109'
See 'docker run --help'.
You could see Item1 shows we could specify nfs version when use nfs volume, while Item2 shows we could directly use nfs volume within docker run without pre-create a volume.
But, how I could specify nfs version when directly use docker run? What's the correct format here?
This works for me:
--mount 'type=volume,dst=/data,volume-driver=local,volume-opt=type=nfs,volume-opt=device=:/var/lib/lava/dispatcher/tmp,"volume-opt=o=addr=10.192.244.109,rw,nfsvers=4"'
Seems like the argument parser is picky with the quotes.
It is parsed as an extra argument then.
Alternately you can use
..,volume-opt=o=nfsvers=4,volume-opt=o=addr=10.192.244.109

How to get inside docker container to see the mounted volume?

I am trying to buld a simple docker file that has a debian image.
Also, I want to mount my local volume inside the docker container.
The problem I have is that how do I get inside the container to see the volume mounted.
$docker run -d -it bash --mount type=bind,source="$(pwd)",target=/app docker_test:latest
43db16a76d50f1da0f8589c9ec460080ccef40122c9bc54abad3230dbbfe7885
I believe this 43db16a.. is container id. Even I try to attach to this container id I get an an error message. It says you cannot attach to the stop container. What am I missing here.
It works if I do
docker run -d -it --name test_docker1 --mount type=bind,source="$(pwd)"/,target=/app docker_test:latest
and then
docker attach
d6bd3cc6dc667e742d0bb3c7fbec58935046c1bf7a2e0b6806d48817082c05be
Also, it works when I do
$docker run --rm -ti --mount type=bind,source="$(pwd)"/,target=/app docker_test:latest
In another terminal do a docker ps, then look for the image you are looking for and copy the id, then do a docker exec -ti <your-image> bash there you have a bash terminal inside the container and you can check the mounted volume.

Start a Docker volume and give it a name

I want to know how to start a Docker container with a named volume. I've tried this
docker run -it --name container1 -v path:path --name volumename image bin/bash
But the container was also named "volumename"
How can I resolve this issue?
First of all, if you don't have an existing image then you have to create one by using:
docker volume create --name [volume name]
For instance:
docker volume create --name namedvolume
If you did this you can check if it has been created. Just type:
docker volume ls
You did all right, if it shows your created volume.
Last step:
docker run -v [volume name]:[container directory]
With a directory:
docker run -it --name container -v data-volume:/data image /bin/bash

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.

Why doesn't the docker volume in Windows Server show up when I run this command?

I have a folder setup on the host machine at c:\testvol.
The docker image does not have a folder at c:\testvol.
When I run:
docker run --rm -d --name {name} {imagename} --v c:\testvol:c:\testvol
Why doesn't the volume show up on the container?
It appears in the docker command you need to specify the image name as the last parameter unless you want to pass arguments to be processed by the docker file.
Also, --v should be -v or --volume as --v is not recognized by the docker command.
The command you want is:
docker run --rm -d --name {name} --volume c:\testvol:c:\testvol {imagename}

Resources