1

In a powershell module there is a class with the following method (here simplified). It simply calls docker to automatically build an image.

Everything works and the image is correctly built. What I cannot understand is why the logs from docker are not printed in the console (as they do if I call the same docker command directly instead of inside a powershell module).

[void] BuildImage() {
    $imageId = ...
    docker build -f ../Dockerfile . -t $imageId
}
1
  • if the docker command is an external command a & at the start would be correct in anyway, even if it is not the root cause of the problem. & docker build -f ../Dockerfile . -t $imageId Commented May 5, 2023 at 3:52

1 Answer 1

1

You have a void method, it produces no output. If you want to send output to the success stream (stdout), then change the return type to [string[]] and use return:

[string[]] BuildImage() {
    $imageId = ...
    return docker build -f ../Dockerfile . -t $imageId
}

If instead you're interested in sending the output to the other PowerShell streams, you could try the following example which includes redirection of errors then checks if the output type was an ErrorRecord it sends it to the error stream otherwise it sends it to the information stream.

[void] BuildImage() {
    $imageId = ...
    docker build -f ../Dockerfile . -t $imageId 2>&1 | ForEach-Object {
        if($_ -is [System.Management.Automation.ErrorRecord]) {
            return Write-Error $_
        }

        Write-Host $_
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately piping to Write-Host doesn't change the result
@FrancoTiveron try with that update, perhaps its sending output to the stderr, adding 2>&1 might solve the problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.