Sending dynamic parameters to an external command, like docker
, in PowerShell is surprisingly easy. We may simply use the call operator &
and pass all arguments in an array. Schematically:
& $TheCommandName $TheArguments
For example:
PS> $TheCommandName = "docker"
PS> $TheArguments = @("image" "ls")
PS> & $TheCommandName $TheArguments
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest f825f99d3d8a 3 weeks ago 101MB
moby/buildkit buildx-stable-1 d524bfa81ecf 6 months ago 201MB
hello-world latest ee301c921b8a 18 months ago 9.14kB
Imagine for example, you want to parameterize the base image for a Docker image build using a build arg.
ARG IMAGE_VERSION=20.4
FROM "ubuntu:${IMAGE_VERSION}"
RUN echo "Building Ubuntu image version ${IMAGE_VERSION}..."
Now, let’s say you have a wrapper function, that builds this image—optionally overriding the default image version using the specified build arg—, like this:
function InvokeBuildImage([string]$ImageVersion) {
$DockerArguments = $Null
if($ImageVersion) { $DockerArguments = @("build", "--build-arg", "IMAGE_VERSION=${ImageVersion}", "--file", "Dockerfile", ".") }
else { $DockerArguments = @("build", "--file", "Dockerfile", ".") }
& "docker" $DockerArguments
}
Then calling this function with a (valid) version string will build our image for the passed version and otherwise will fall back to the default version.
PS> InvokeBuildImage "22.04"
[+] Building 8.3s (6/6) FINISHED docker:rancher-desktop
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 154B 0.0s
=> [internal] load metadata for docker.io/library/ubuntu:22.04 5.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [1/2] FROM docker.io/library/ubuntu:22.04@sha256:0e5e4a57c2499249aafc3b40fcd541e9a456aab7296681a3994d631587203f97 2.9s
=> => resolve docker.io/library/ubuntu:22.04@sha256:0e5e4a57c2499249aafc3b40fcd541e9a456aab7296681a3994d631587203f97 0.0s
=> => sha256:0e5e4a57c2499249aafc3b40fcd541e9a456aab7296681a3994d631587203f97 6.69kB / 6.69kB 0.0s
=> => sha256:7c75ab2b0567edbb9d4834a2c51e462ebd709740d1f2c40bcd23c56e974fe2a8 424B / 424B 0.0s
=> => sha256:981912c48e9a89e903c89b228be977e23eeba83d42e2c8e0593a781a2b251cba 2.31kB / 2.31kB 0.0s
=> => sha256:a186900671ab62e1dea364788f4e84c156e1825939914cfb5a6770be2b58b4da 27.36MB / 27.36MB 1.6s
=> => extracting sha256:a186900671ab62e1dea364788f4e84c156e1825939914cfb5a6770be2b58b4da 1.2s
=> [2/2] RUN echo "Building Ubuntu image version ${IMAGE_VERSION}..." 0.3s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:afd176bdda20220980173b88c05fc503db8cc7d506c6420f1d81ef189c2a3ea3 0.0s
Or
PS> InvokeBuildImage
[+] Building 2.5s (6/6) FINISHED docker:rancher-desktop
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 154B 0.0s
=> [internal] load metadata for docker.io/library/ubuntu:20.04 2.4s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [1/2] FROM docker.io/library/ubuntu:20.04@sha256:8e5c4f0285ecbb4ead070431d29b576a530d3166df73ec44affc1cd27555141b 0.0s
=> CACHED [2/2] RUN echo "Building Ubuntu image version ${IMAGE_VERSION}..." 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:f924f5547df66f7803c15614601ca0c6eacba5d4e7132f8fecfa96082c5d21a9 0.0s