Answers: <p>If you have a directory named <code>te,&quot;st</code>, it's trivial to work with using <code>-v</code>: 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:</p>
  <pre><code>$ mkdir te,\&quot;st
  $ touch te,\&quot;st/file1
  $ docker run --rm -v &quot;$PWD/te,\&quot;st:/data&quot; alpine ls /data
  file1
  </code></pre>
  <p>For <code>--mount</code> it's a little trickier. From <a href="https://github.com/docker/cli/issues/1480" rel="nofollow noreferrer">https://github.com/docker/cli/issues/1480</a> we know that the argument to <code>--mount</code> 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 (<code>&quot;&quot;</code>)):</p>
  <pre><code>$ docker run --rm --mount &quot;type=bind,\&quot;src=$PWD/te,\&quot;\&quot;st\&quot;,target=/data&quot; alpine ls /data
  file1
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2022-10-03T02:48:55.560Z
Answers: 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.
Answers: ErikE
Answers: CC BY-SA 4.0
Answers: 2023-04-08T08:20:15.763Z
Answers: Unfortuntely it doesn't work if you have `:` in path. Open Build Service often names its project as `home:lang:rust/devel` etc. :sad:
Answers: Dilawar
Answers: CC BY-SA 4.0
Answers: 2022-09-30T20:09:42.487Z
Answers: 2022-09-30T20:09:42.487Z
Answers: larsks
Body: <p>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 <code>--mount</code> syntax?</p>
  <p>Note that <a href="https://docs.docker.com/storage/bind-mounts/" rel="nofollow noreferrer">Docker docs on bind mounting</a> say everyone is &quot;encouraged to use&quot; <code>--mount</code> instead of <code>-v</code>, but the case of <code>-v</code>, I have a similar question for how to escape <code>:</code> in paths.</p>
  <p>For example,</p>
  <pre><code>cd /Users/name
  mkdir te,\&quot;st
  touch te,\&quot;st/file.txt
  docker run -it --rm --mount 'type=bind,source=/Users/name/te??st,target=/usr/test' alpine ash
  </code></pre>
  <p>Wher e<code>??</code> needs to be the comma <code>,</code> and double quote <code>&quot;</code> from the <code>te,&quot;st</code> directory created above.</p>
  <ul>
  <li>I tried wrapping the value after <code>source=</code> in double quotes just to test if it will accept a comma, but get error <code>bare &quot; in non-quoted-field</code>. This attempt was based on the info-box &quot;Escape values from outer CSV parser&quot; in the <a href="https://docs.docker.com/storage/volumes/#choose-the--v-or---mount-flag" rel="nofollow noreferrer">Choose the -v or --mount flag</a> section of Docker storage volumes docs. But this field clearly doesn't like double quotes, at all, ever.</li>
  <li>Single quotes, double commas, and backslashes in the volume path don't seem to work, with errors such as <code>invalid field 'st' must be a key=value pair</code>.</li>
  <li>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.</li>
  </ul>
  <p>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?</p>
  <p>Or, do I need to use a <code>compose.yaml</code> file to get around this limitation so I can present the parts/pieces of the mounts in a different encoding/format?</p>
  
Tags: <docker>
Title: Escape quotes and commas in Docker volume paths using bind-mount syntax
UserDisplayName: ErikE
ThreadText: # Escape quotes and commas in Docker volume paths using bind-mount syntax
  
  (Asked by: username_0 on 2022-09-26)
  
  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](https://docs.docker.com/storage/bind-mounts/) 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](https://docs.docker.com/storage/volumes/#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?
  
  **Tags:** docker
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-09-30. Score: 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
  
  ```
  
  > **Comments:**
  > **username_0**: 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.  
  > **username_2**: Unfortuntely it doesn't work if you have `:` in path. Open Build Service often names its project as `home:lang:rust/devel` etc. :sad:  
  
  ---
  
  