|
| 1 | +--- |
| 2 | +date: Wed Aug 24 2022 22:38:38 -0700 (Pacific Daylight Time) |
| 3 | +qualitative_time: |
| 4 | +title: One time when it really was a Docker command quoting issue |
| 5 | +previous_teaser: The average number of slices of pizza a fork touches in its lifetime |
| 6 | +previous_first: false |
| 7 | +has_highlighting: true |
| 8 | +--- |
| 9 | +A few days ago I was staring at a Docker command like this: |
| 10 | + |
| 11 | +```sh |
| 12 | +set -e |
| 13 | +image=debian # set externally |
| 14 | +tag=latest # derived from some command output |
| 15 | +cd /tmp |
| 16 | +docker pull "$image:$tag" |
| 17 | +docker run --rm \ |
| 18 | + --entrypoint echo \ |
| 19 | + -v $PWD/a:/mnt/a \ |
| 20 | + $image:$tag \ |
| 21 | + "hello world" |
| 22 | +``` |
| 23 | + |
| 24 | +But it would give: |
| 25 | + |
| 26 | +``` |
| 27 | +latest: Pulling from library/debian |
| 28 | +Digest: sha256:d52921d97310d0bd48dab928548ef539d5c88c743165754c57cfad003031386c |
| 29 | +Status: Image is up to date for debian:latest |
| 30 | +docker.io/library/debian:latest |
| 31 | +docker: invalid reference format. |
| 32 | +See 'docker run --help'. |
| 33 | +``` |
| 34 | + |
| 35 | +And there were plenty of unquoted variables, which by now is well known to mess up Docker commands. |
| 36 | +But we were able to confirm that they didn't contain any spaces. |
| 37 | +(I think we later added quotes around stuff in order to be safer.) |
| 38 | +So I was thinking, this couldn't be a quoting issue. |
| 39 | + |
| 40 | +One interesting thing is that if any of `image` or `tag` were empty, it would give the exact error we're seeing: |
| 41 | + |
| 42 | +``` |
| 43 | +$ docker run :latest |
| 44 | +docker: invalid reference format. |
| 45 | +See 'docker run --help'. |
| 46 | +$ docker run debian: |
| 47 | +docker: invalid reference format. |
| 48 | +See 'docker run --help'. |
| 49 | +$ docker run : |
| 50 | +docker: invalid reference format. |
| 51 | +See 'docker run --help'. |
| 52 | +``` |
| 53 | + |
| 54 | +But the `docker pull` above it was working fine, so it couldn't have been those variables being empty. |
| 55 | + |
| 56 | +At that point, I tried typing in the whole command in my terminal and found that I couldn't reproduce the issue: |
| 57 | + |
| 58 | +``` |
| 59 | +... |
| 60 | +$ docker run --rm \ |
| 61 | +> --entrypoint echo \ |
| 62 | +> -v $PWD/a:/mnt/a \ |
| 63 | +> $image:$tag \ |
| 64 | +> "hello world" |
| 65 | +hello world |
| 66 | +``` |
| 67 | + |
| 68 | +Could it have been some sneaky non-ASCII characters hiding in the original? |
| 69 | +I checked with an online tool, and there was not. |
| 70 | +No specialized dashes instead of hyphens, no smart quotes instead of the straight ones. |
| 71 | + |
| 72 | +--- |
| 73 | + |
| 74 | +It turned out that there was an extra space after the backslash in the `--entrypoint` line. |
| 75 | +(Apologies if anyone was reading a printed copy of this; you stood no chance.) |
| 76 | + |
| 77 | +The result was that the backslash-space would give `docker run` a space character as the first positional parameter, i.e. the image name. |
| 78 | +Then there would be a plain old newline marking the end of the command. |
| 79 | + |
| 80 | +We never saw any junk about `-v` not being found as a program because the thing we were using to run it bailed on the first failed command (I've tried to capture that with the `set -e` above). |
| 81 | + |
| 82 | +And I was looking up the terminology for this, and supposedly, escaping a character such as a space with a backslash in shell script is described under the "Quoting" section, so it was technically a quoting issue after all. |
0 commit comments