0

In order for me to write the most bullet-proof scripts as possible that don't break with possible edge cases (no matter how unlikely), how do I escape commas and quotes in Docker bind-mount paths using --mount syntax?

Note that Docker docs on bind mounting say everyone is "encouraged to use" --mount instead of -v, but the case of -v, I have a similar question for how to escape : in paths.

For example,

cd /Users/name
mkdir te,\"st
touch te,\"st/file.txt
docker run -it --rm --mount 'type=bind,source=/Users/name/te??st,target=/usr/test' alpine ash

Wher e?? needs to be the comma , and double quote " from the te,"st directory created above.

  • I tried wrapping the value after source= in double quotes just to test if it will accept a comma, but get error bare " in non-quoted-field. This attempt was based on the info-box "Escape values from outer CSV parser" in the Choose the -v or --mount flag section of Docker storage volumes docs. But this field clearly doesn't like double quotes, at all, ever.
  • Single quotes, double commas, and backslashes in the volume path don't seem to work, with errors such as invalid field 'st' must be a key=value pair.
  • With this, I am out of guesses. I tried a few random stabs in the dark to see if Docker would do environment variable expansion instead of the shell, but nothing is working.

Is it simply impossible to work with paths or volumes that contain a comma or a double quote in their names? Can I instruct Docker to use environment variables somehow so it does the environment variable expansion instead of the shell?

Or, do I need to use a compose.yaml file to get around this limitation so I can present the parts/pieces of the mounts in a different encoding/format?

1 Answer 1

2

If you have a directory named te,"st, it's trivial to work with using -v: just quote it, keeping in mind that if you have a double quote inside a double quote you need to escape it. So this works:

$ mkdir te,\"st
$ touch te,\"st/file1
$ docker run --rm -v "$PWD/te,\"st:/data" alpine ls /data
file1

For --mount it's a little trickier. From https://github.com/docker/cli/issues/1480 we know that the argument to --mount is parsed using CSV syntax, so we can use typical CSV escaping to take of things (quote fields that contain commas, and double-quote to escape quotes ("")):

$ docker run --rm --mount "type=bind,\"src=$PWD/te,\"\"st\",target=/data" alpine ls /data
file1
2
  • Thank you! I will check this as soon as I get a chance. I think I had tried double double quotes, but obviously that wouldn't work without wrapping the whole field in quotes.
    – ErikE
    Commented Oct 3, 2022 at 2:48
  • Unfortuntely it doesn't work if you have : in path. Open Build Service often names its project as home:lang:rust/devel etc. :sad:
    – Dilawar
    Commented Apr 8, 2023 at 8:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.