Home > Net >  powershell pass optional parameters to "docker" call
powershell pass optional parameters to "docker" call

Time:09-23

Note, I'm pretty new to powershell thus there might be an obvious solution.

I have a powershell script, which copies some files, to my folder e.g credentials and stuff, builds a docker image, and then delete the credentials-files again e.g

param($DockerName,$push=$false)

$CurrentFolder = split-path -parent $MyInvocation.MyCommand.Definition

#Copy credentials

$ServiceAccountFile = "my/super/safe/credentials.json"
$ServiceAccountDestination =  $CurrentFolder "\credentials.json" 
if(-Not(Test-Path -Path $ServiceAccountDestination))
{
    Copy-Item -Path $ServiceAccountFile -Destination $ServiceAccountDestination

}

docker build -t $DockerName . --build-arg api_token=$Env:API_TOKEN 
if($? -and $push) {
    docker push $DockerName
    Remove-Item -Path "$ServiceAccountDestination"
    Remove-Item -Path "$UtilsDestination" -recurse} #Build image and push only if build was completed

and I can do powershell ./build_docker.ps1 -dockername test_docker which works fine.

But, if I want to pass docker paramters like --no-cache etc. how do I incorporate that in the script, such that I can do it from the commandline without having to hard-code all the possible docker paramteres? I.e like the python **kwargs

CodePudding user response:

In param you can declare an array parameter, and then use the splatting operator when calling the docker executable.

param($DockerName,$push=$false, [array]$kwargs)


docker build -t $DockerName . --build-arg api_token=$Env:API_TOKEN  @kwargs
  • Related