I am following https://docs.docker.com/get-started/06_bind_mounts/#start-a-dev-mode-container on a Windows PC and am stuck here:
Run the following command. We’ll explain what’s going on afterwards:
docker run -dp 3000:3000 \
-w /app -v "$(pwd):/app" \
node:12-alpine \
sh -c "yarn install && yarn run dev"
If you are using PowerShell then use this command:
docker run -dp 3000:3000 `
-w /app -v "$(pwd):/app" `
node:12-alpine `
sh -c "yarn install && yarn run dev"
When using Command Prompt, I get errors (tried multiple variations as shown below), and when using PowerShell, I don't appear to get errors but am not running anything as showed when executing docker ps.
Note that I would rather use Command Prompt and not PowerShell as I could use Linux commands with ComandPrompt on my PC.
What is the significance of backslashes when using Dockers with Command Prompt (and tick marks with PowerShell for that matter)?
I have since found that docker run -dp 3000:3000 -w /app -v "%cd%:/app" node:12-alpine sh -c "yarn install && yarn run dev" works without errors (got rid of backslashes, put on one line, and used %cd% instead of $(pwd)), but would still like to know why using the exact script in the example results in errors.
Using Command Prompt
C:\Users\michael\Documents\Docker\app>docker run -dp 3000:3000 \
docker: invalid reference format.
See 'docker run --help'.
C:\Users\michael\Documents\Docker\app> -w /app -v "$(pwd):/app" \
'-w' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\michael\Documents\Docker\app> node:12-alpine \
The filename, directory name, or volume label syntax is incorrect.
C:\Users\michael\Documents\Docker\app> sh -c "yarn install && yarn run dev"
sh: yarn: command not found
C:\Users\michael\Documents\Docker\app>docker run -dp 3000:3000 \ -w /app -v "$(pwd):/app" \ node:12-alpine \ sh -c "yarn install && yarn run dev"
docker: invalid reference format.
See 'docker run --help'.
C:\Users\michael\Documents\Docker\app>docker run -dp 3000:3000 -w /app -v "$(pwd):/app" node:12-alpine sh -c "yarn install && yarn run dev"
docker: Error response from daemon: create $(pwd): "$(pwd)" 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.
See 'docker run --help'.
C:\Users\michael\Documents\Docker\app>
Using PowerShell
PS C:\Users\michael\Documents\Docker> docker run -dp 3000:3000 `
>> -w /app -v "$(pwd):/app" `
>> node:12-alpine `
>> sh -c "yarn install && yarn run dev"
849af42e78d4ab09242fdd6c3d03bcf1b6b58de984c4485a441a2e2c88603767
PS C:\Users\michael\Documents\Docker> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
PS C:\Users\michael\Documents\Docker>
Because the command with the line-ending
\characters is meant for POSIX-compatible shells such asbash, not forcmd.exePOSIX-compatible shells (
sh,bash,dash,ksh,zsh):\for line-continuation (continuing a command on the following line) and escaping in general.$varNameto reference both environment and shell-only variables.$(...)for embedding the output from a command (...) in command lines (command substitution)."...", interpolating) and single-quoted ('...', verbatim) strings; use'\''to - in effect - include a'inside'...'.(Additionally, in
bash,ksh, andzsh, there are the rarely used ANSI C-quoted strings,$'...', and, inbashandksh, perhaps even more rarely, localizable strings,$"...").cmd.exe:^for line-continuation and escaping in general (in unquoted arguments only).%varName%to reference environment variables (the only variable type supported)."..."strings (interpolating).PowerShell:
`(the backtick) for line-continuation and escaping in general.$env:varNameto reference environment variables,$varNameto reference shell-only variables.$(...), called subexpressions, the equivalent of command substitutions (outside of double-quoted strings,(...)is usually sufficient)."...", interpolating) and single-quoted ('...', verbatim) strings; use''to embed a'inside'...'.cmd.exe, notably including@ { } , ;, which therefore require individual`-escaping in unquoted arguments or embedding in quoted strings - see this answer.Potential line-continuation pitfall: in all of the shells discussed, the escape character must be the very last character on the line - not even trailing (intra-line) whitespace is allowed (because the escape character would then apply to it rather than to the newline).
The information above is summarized in the following table:
\)^)`)$varName/export varName=...%varName%/set varName=...$env:varName/$env:varName = ...$varName/varName=...setlocal)$varName/$varName = ...$(...)(...)/$(...), esp. in stringsNote re setting variables with respect to whitespace on either side of the
=symbol:=.cmd.exe, such whitespace is significant and becomes part of the variable / value name, and is therefore usually to be avoided.$var = 'hi!')See also:
https://hyperpolyglot.org/shell for a much more comprehensive juxtaposition of these shells, though note that - as of this writing - the information about PowerShell is incomplete.
Sage Pourpre's helpful answer for links to the line-continuation documentation of the respective shells.