Build Docker Run Option String using Powershell Variables

Viewed 308

I'm trying to bind mount a hostpath to a Docker container using a Powershell script as wrapper for my docker run call using Docker Desktop WSL2.

The problem seems to be, that I store the bind mount option in the $Opts variable. I need this to only use specific options to the docker run command depending on params passed to the script.

Minimal (not) Working Example:

    param (
        [String] $MyPath = ""
    )
    begin {
        $Opts = "-v ${MyPath}:/etc/dir:ro "
        docker run --rm ${Opts} alpine:3.12
    }

Call:

.\test.ps1 ${HOME}

Error:

docker: Error response from daemon: Mount denied:
The source path " C:\\Users\\knoppiks:/etc/dir:ro "
too many colons.
See 'docker run --help'.

If I use the bind-mount option directly without storing it in a variable it works.

docker run --rm -v ${MyPath}:/etc/dir:ro alpine:3.12
1 Answers

I set $MyPath to C:\\Users\\Me, manually set $Opts, then ran the container as your script did, and got it to fail in the same exact way. Perfect.

Changing your command from

docker run --rm ${Opts} alpine:3.12

to

Invoke-Expression -Command "docker run --rm ${Opts} alpine:3.12"

didn't fail. Not sure what this should have done other than crank up an instance of Alpine and share a local path to the container, but it might be worth trying?

Related