I just want to run a docker command with parameters from PowerShell 7 script. When I just use the command:
if ($isRunning -eq $false) {
docker run -it --rm -p 8080:8080 -v <some_path> <image_name>
}
it works. But I need to use a variable in this command, so I changed it to:
& "docker run -it --rm -p 8080:8080 -v $script:myPath\:/usr/local/whatever"
And it runs but then it tells me The term ‘docker run -it …’ is not recognized as a name of a cmdlet…. It seems like PowerShell script does not have the same $PATH environment variable value as cmd.
What’s wrong here?
>Solution :
The following might work, passing the arguments to your docker command as an array of arguments instead of embedding all the expression in a string which is currently failing because PowerShell is trying to find a command with the name of the entire expression (docker run -it ...).
if (-not $isRunning) {
docker @(
'run'
'-it'
'--rm'
'-p'
'8080:8080'
'-v'
"$script:myPath\:/usr/local/whatever"
)
}