mviereck / x11docker

Run GUI applications and desktops in docker and podman containers. Focus on security.
MIT License
5.62k stars 378 forks source link

How can I use the -v to mapping a device with x11docker? #58

Closed wanhongbo closed 6 years ago

wanhongbo commented 6 years ago

When use docker ,I use the "-v /dev/kvm:/dev/kvm" to enable the kvm feature in the conatiner,but use x11docker,threre is no specified paramters,Could you help me?thanks

eine commented 6 years ago

You can use docker args/params with the following syntax:

x11docker <x11docker args> -- "<docker args>" <image_name> <container args>

For example:

x11docker --hostdisplay -- "-v $(pwd):/src" 11384eb/sozi sozi

See https://github.com/mviereck/x11docker#terminal-usage


BTW, @mviereck is there any possibility to get rid of double quotes around <docker args>? I'd like commands to be compatible between using GUIs or not. I.e.:

# Works
docker run --rm -it -v $(pwd):/src simexp/octave octave
# Does not work
docker run --rm -it "-v $(pwd):/src" simexp/octave octave
# Does not work (desired)
x11docker --hostdisplay -v $(pwd):/src simexp/octave octave
# Works
x11docker --hostdisplay "-v $(pwd):/src" simexp/octave octave
wanhongbo commented 6 years ago

It works successfully.Thanks very much!!!

mviereck commented 6 years ago

@wanhongbo For mounting a volume at the same location with r/w access, you can also use x11docker option --sharedir /dev/kvm. Option --sharedir $dir is a shortut for docker run option -v $dir:$dir:rw.

Though, devices should rather be declared with docker run option --device, e.g.

x11docker  --  --device=/dev/kvm  imagename

or

x11docker  --  "--device /dev/kvm"  imagename

@1138-4EB

BTW, @mviereck is there any possibility to get rid of double quotes around ? I'd like commands to be compatible between using GUIs or not.

It is possible if the arguments do not have a whitespace ` inside. I did not mention it in the documentation to avoid confusion. (While parsing arguments, x11docker takes the first argument after--without a leading-` as imagename.) Works:

x11docker --  --volume=/dev/kvm:/dev/kvm  imagename

Fails:

x11docker --  -v  /dev/kvm:/dev/kvm  imagename
x11docker --  --volume  /dev/kvm:/dev/kvm  imagename

The issue is to divide between docker run options and imagename+command+args. I could parse for a second -- to create a syntax like:

x11docker --options  --  --some docker run options -- imagename imagecommand args

This would be an issue if the image command contains --:

x11docker --options  --  imagename imagecommand args1 -- args2

Thoughts?

eine commented 6 years ago

It is possible if the arguments do not have a whitespace inside. I did not mention it in the documentation to avoid confusion.

Knowing this is enough for me, as it allows:

x11docker --hostdisplay -- -v=$(pwd):/src -v=/home/user/imgs/:/imgs simexp/octave octave
docker run --rm -it -v=$(pwd):/src -v=/home/user/imgs/:/imgs simexp/octave octave

I'd be ok just adding this to the documentation, even if it is only a side note or a hint.

I could parse for a second -- to create a syntax like:

I think that this would not solve my initial question. Apart from the possible conflict you mention, it would still require the user to remove the additional -- when using the docker CLI.

What I had thought was parsing the docker options explicitly in order to detect the image name. But I think that the current implementation is easier. Since it is compatible already, I don't think it is worth making it more complicated.

mviereck commented 6 years ago

The latest update introduces a new syntax for custom docker run options:

x11docker [OPTIONS] -- DOCKER_RUN_OPTIONS -- IMAGE [COMMAND [ARG1 ARG2 ...]]

Old variants with "" (or without whitespace) are still valid, with only one exception if the image command contained -- and also docker run options were given. Changelog entry:

     New:
   x11docker [OPTIONS] --  DOCKER_RUN_OPTIONS -- IMAGE [COMMAND [ARG1 ARG2 ...]]
     Previous, still valid:
   x11docker [OPTIONS] -- "DOCKER_RUN_OPTIONS"   IMAGE [COMMAND [ARG1 ARG2 ...]]
   x11docker [OPTIONS] -- IMAGE COMMAND ARG1 -- ARG2
     BREAKS due to wrongly parsed -- :
   x11docker [OPTIONS] -- "DOCKER_RUN_OPTIONS"   IMAGE COMMAND ARG1 -- ARG2

it would still require the user to remove the additional -- when using the docker CLI.

No, docker CLI accepts -- between options and image name. You can use the same syntax for docker and x11docker.

eine commented 6 years ago

@mviereck is x11docker [OPTIONS] -- "DOCKER_RUN_OPTIONS" -- IMAGE COMMAND ARG1 -- ARG2 valid as a workaround for the wrongly parsed format?

mviereck commented 6 years ago

It should be valid with and without "". Though, just found that x11docker parses it wrong. Maybe you have an idea how to fix it. Current issue: x11docker drops the part IMAGE COMMAND ARG1.

On first occurence of -- (and a following arg beginning with -), x11docker tries to split the remaining string at the second --.

This part correctly parses DOCKER_RUN_OPTIONS (aaa):

echo "aaa -- bbb -- ccc" | sed 's/ -- .*//'

This should return bbb -- ccc, but only returns ccc:

echo "aaa -- bbb -- ccc" | sed  -n 's/^.* -- //p'

Edit: Fixed it without sed:

Line="aaa -- bbb -- ccc" 
Line="${Line#aaa -- }"
eine commented 6 years ago

I saw that you solved without sed. But, just for completeness:

$ echo "aaa -- bbb -- ccc" | sed  -e 's/^[^-]* --//g'
 bbb -- ccc

$ echo "aaa -- bbb -- ccc" | sed  -e 's/^[^-]* --\s*//g'
bbb -- ccc
mviereck commented 6 years ago

saw that you solved without sed

I would prefer to solve it with sed. The bash string operation is quite sensitive to every difference, a wrongly parsed whitespace would already lead to wrong results.

I tried your code examples with more complex strings:

$ echo "--some --run option -- image --arg1 -- --arg2" | sed  -e 's/^[^-]* -- //g'
--some --run option -- image --arg1 -- --arg2
$ echo "--some --run option -- image --arg1 -- --arg2" | sed  -e 's/^[^-]* --\s*//g'
--some --run option -- image --arg1 -- --arg2

It does not divide between -- of an --option and the separator -- with a leading and a trailing `whitespace `.

Can you do some sed magic that can handle this?

eine commented 6 years ago

I could make it work in https://regexr.com/. If you put /^.*?(?=\s--\s) -- /gm in the expression, the following text:

--some --run option -- image --arg1 -- --arg2
aaa -- bbb -- ccc

will produce this output in 'Replace' mode:

image --arg1 -- --arg2
bbb -- ccc

But I could not reproduce it in the bash (neither cygwin nor MSYS).

mviereck commented 6 years ago

I've asked in a forum and got this one, works quite well: sed -E 's/.* -- (.*( --.*))/\1/'

mviereck commented 6 years ago

The snippet above fails for more -- separators:

$ echo "aaa -- bbb -- ccc -- ddd" | sed -E 's/.* -- (.*( --.*))/\1/'
ccc -- ddd

I got some more possible code snippets. Now x11docker uses:

$ sh -c 'echo "${0#* -- }"' "aaa -- bbb -- ccc -- ddd"
bbb -- ccc -- ddd