Skip to content
This repository was archived by the owner on Jul 19, 2024. It is now read-only.

Commit 998fe6e

Browse files
authored
Merge branch 'master' into fix/dockerSocket
2 parents 3f155b8 + 81d475c commit 998fe6e

18 files changed

+209
-82
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ cache:
1111
- "/home/travis/.local/share/powershell/Modules"
1212

1313
if: |
14-
repo != 3shapeAS/dockerbuild-pwsh OR \
14+
repo != 3shapeAS/docker-ci OR \
1515
type = pull_request OR \
1616
branch = master OR \
1717
tag IS present

README.md

Lines changed: 72 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -72,31 +72,54 @@ To build an image based on a Dockerfile, use the Invoke-DockerBuild CmdLet, like
7272

7373
```powershell
7474
PS C:\docker> Invoke-DockerBuild . -ImageName structure
75+
Sending build context to Docker daemon 4.608kB
76+
77+
Step 1/8 : FROM ubuntu:18.04
78+
---> 775349758637
79+
Step 2/8 : SHELL ["/bin/bash", "-o", "pipefail", "-c"]
80+
---> Using cache
81+
---> b3d8c49615a7
82+
Step 3/8 : RUN apt-get update && apt-get install -y --no-install-recommends curl=7.* ca-certificates=* && apt-get clean && rm -rf /var/lib/apt/lists/*
83+
---> Using cache
84+
---> 9470f9c9ecea
85+
Step 4/8 : RUN update-ca-certificates
86+
---> Using cache
87+
---> 80853c222946
88+
Step 5/8 : RUN curl -sL https://get.docker.com/ | sh
89+
---> Using cache
90+
---> af17b9b8fb1b
91+
Step 6/8 : RUN curl -LO https://storage.googleapis.com/container-structure-test/v1.8.0/container-structure-test-linux-amd64 && chmod +x container-structure-test-linux-amd64 && mv container-structure-test-linux-amd64 /usr/local/bin/container-structure-test
92+
---> Using cache
93+
---> bea1dca8e10e
94+
Step 7/8 : VOLUME /configs
95+
---> Using cache
96+
---> 97e90bf8481b
97+
Step 8/8 : ENTRYPOINT [ "/usr/local/bin/container-structure-test" ]
98+
---> Using cache
99+
---> 6b9746ab76d8
100+
Successfully built 6b9746ab76d8
101+
Successfully tagged structure:latest
102+
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
75103
76104
Dockerfile : Dockerfile
77105
ImageName : structure
78106
Registry :
79107
Tag : latest
80108
CommandResult : CommandResult
81109
```
82-
In this scenario, you will see the result of the execution which is a PSCustomObject that holds the command result and image name of the image you just created.
83110

84-
You can verify the existence of the image using `docker images`
85-
```
86-
PS C:\docker> docker images
87-
```
111+
In this scenario, you will see the both the output from Docker and the result of the execution which is a PSCustomObject that holds:
88112

89-
This is fine when everything goes well. But it's not very practial for troubleshooting. It would be better, if we could get some feedback while on the go.
90-
To see the output of the Docker command being run, use the `-PassThru` switch of the pertinent CmdLet.
91-
For example:
113+
- The path to the Dockerfile being used as the basis for the image.
114+
- The name of the image being produced.
115+
- The registry (if unset defaults to Docker's default registry)
116+
- The command result object which has more detailed information about the execution.
92117

93-
```powershell
94-
PS C:\docker> Invoke-DockerBuild . -ImageName structure -PassThru
95-
```
96-
which will yield output along these lines:
118+
In most cases you will want to store the result in a variable for further processing or output to a CI/CD pipeline, like so:
97119

98120
```powershell
99-
Sending build context to Docker daemon 2.56kB
121+
PS C:\docker> Invoke-DockerBuild . -ImageName structure
122+
Sending build context to Docker daemon 4.608kB
100123
101124
Step 1/8 : FROM ubuntu:18.04
102125
---> 775349758637
@@ -107,26 +130,36 @@ Step 3/8 : RUN apt-get update && apt-get install -y --no-install-recommends
107130
---> Using cache
108131
---> 9470f9c9ecea
109132
Step 4/8 : RUN update-ca-certificates
110-
111-
(snip)
112-
113-
114-
Dockerfile : Dockerfile
115-
ImageName : structure
116-
Registry :
117-
Tag : latest
118-
CommandResult : CommandResult
133+
---> Using cache
134+
---> 80853c222946
135+
Step 5/8 : RUN curl -sL https://get.docker.com/ | sh
136+
---> Using cache
137+
---> af17b9b8fb1b
138+
Step 6/8 : RUN curl -LO https://storage.googleapis.com/container-structure-test/v1.8.0/container-structure-test-linux-amd64 && chmod +x container-structure-test-linux-amd64 && mv container-structure-test-linux-amd64 /usr/local/bin/container-structure-test
139+
---> Using cache
140+
---> bea1dca8e10e
141+
Step 7/8 : VOLUME /configs
142+
---> Using cache
143+
---> 97e90bf8481b
144+
Step 8/8 : ENTRYPOINT [ "/usr/local/bin/container-structure-test" ]
145+
---> Using cache
146+
---> 6b9746ab76d8
147+
Successfully built 6b9746ab76d8
148+
Successfully tagged structure:latest
149+
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
119150
```
151+
in which case you will only see the output from Docker, the result object is stored in $result.
120152

121-
Now, we get both the result and the output from the docker command.
122-
123-
In most cases you will want to store the result in a variable for further processing or output to a CI/CD pipeline, like so:
124-
153+
You can verify the existence of the image you just created using `docker images`
125154
```
126-
PS C:\docker> $result = Invoke-DockerBuild . -ImageName structure -PassThru
155+
PS C:\docker> docker images
127156
```
128157

129-
Which would then output the status of the command and store the final result object in `$result`.
158+
If you want less output, use `-Quiet` switch to output only the final result of the command. Combined with storing the result in a variable, this will give a completely silent execution of the CmdLet.
159+
160+
### Disabling verbose output
161+
The -Quiet setting for CmdLets that support it, defaults to the value of the enviromenment variable `DOCKER_CI_QUIET_MODE`.
162+
So you can set this environment variable to the desired setting for the `-Quiet` switch so you don't have to set it for each invocation of a cmdlet that supports it.
130163

131164
### Linting a Dockerfile
132165
An important aspect of writing quality Docker images is to try and learn from the best in the community. To this end, we provide a convenient way to run `hadolint` against a Dockerfile. Hadolint is a 3rd party component that scans a dockerfile and produces linted output. You can find the hadolint project here: https://github.com/hadolint/hadolint
@@ -135,7 +168,6 @@ Here's how to use the linter via a CmdLet:
135168

136169
```powershell
137170
PS C:\docker> $result = Invoke-DockerLint .\Dockerfile
138-
PS C:\docker> $result.LintOutput
139171
1: FROM ubuntu:18.04
140172
2:
141173
3: SHELL ["/bin/bash", "-o", "pipefail", "-c"]
@@ -158,11 +190,10 @@ PS C:\docker> $result.LintOutput
158190
20: ENTRYPOINT [ "/usr/local/bin/container-structure-test" ]
159191
```
160192

161-
This Dockerfile in particular has no linting remarks, so it is just output in its entirety with line numbers. Being a mere human, I will forget things from time to time. Let's imagine I omitted the instruction in line 3 on how to deal with commands that fail in a piped execution and run the same commands:
193+
This Dockerfile in particular has no linting remarks, so it is just output in its entirety with line numbers. Imagine I omitted the instruction in line 3 on how to deal with commands that fail in a piped execution and run the linting again:
162194

163195
```powershell
164196
PS C:\docker> $result = Invoke-DockerLint .\Dockerfile
165-
PS C:\docker> $result.LintOutput
166197
1: FROM ubuntu:18.04
167198
2:
168199
3: RUN apt-get update \
@@ -196,7 +227,7 @@ To run tests, you first define them in .yml configs. Then you build the image th
196227
So let's start by building an image called `structure`
197228

198229
```powershell
199-
PS C:\docker> Invoke-DockerBuild . -ImageName structure
230+
PS C:\docker> Invoke-DockerBuild . -Quiet -ImageName structure
200231
201232
Dockerfile : Dockerfile
202233
ImageName : structure
@@ -227,9 +258,17 @@ commandTests:
227258
And you run the tests like this:
228259
229260
```powershell
230-
PS C:\docker> Invoke-DockerTests -ImageName 3shape/containerized-structure-test -ConfigFiles gcs-commands.yml
261+
PS C:\docker> $result = Invoke-DockerTests -ImageName 3shape/containerized-structure-test -ConfigFiles gcs-commands.yml
262+
@{Pass=1; Fail=0; Total=1; Results=System.Object[]}
263+
PS C:\docker> $result
264+
265+
TestResult TestReportPath CommandResult ImageName
266+
---------- -------------- ------------- ---------
267+
@{Pass=1; Fail=0; Total=1; Results=System.Object[]} C:\docker\testreport.json CommandResult 3shape/containerized-structure-test
231268
```
232269

270+
This concludes the section with examples. Let us know if there is something missing, that is not clear from the documentation.
271+
233272
# Development environment setup
234273

235274
* Install PowerShell Core 6.x latest

Source/Private/Write-PassThruOutput.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function Write-PassThruOuput {
1+
function Write-CommandOuput {
22
[CmdletBinding()]
33
param (
44
[String[]]

Source/Public/Invoke-DockerBuild.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function Invoke-DockerBuild {
3131
$ExtraParams = '',
3232

3333
[Switch]
34-
$PassThru
34+
$Quiet = [System.Convert]::ToBoolean($env:DOCKER_CI_QUIET_MODE)
3535
)
3636
$postfixedRegistry = Add-Postfix -Value $Registry
3737
if ($ExtraParams) {
@@ -47,8 +47,8 @@ function Invoke-DockerBuild {
4747
'Tag' = $Tag;
4848
'CommandResult' = $commandResult
4949
}
50-
if ($PassThru) {
51-
Write-PassThruOuput $($commandResult.Output)
50+
if (!$Quiet) {
51+
Write-CommandOuput $($commandResult.Output)
5252
}
5353
return $result
5454
}

Source/Public/Invoke-DockerLint.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function Invoke-DockerLint {
1414
$TreatLintRemarksFoundAsException,
1515

1616
[Switch]
17-
$PassThru
17+
$Quiet = [System.Convert]::ToBoolean($env:DOCKER_CI_QUIET_MODE)
1818
)
1919
$pathToDockerFile = Format-AsAbsolutePath $DockerFile
2020
$dockerFileExists = Test-Path -Path $pathToDockerFile -PathType Leaf
@@ -36,8 +36,8 @@ function Invoke-DockerLint {
3636
'CommandResult' = $commandResult
3737
'LintOutput' = $lintedDockerfile
3838
}
39-
if ($PassThru) {
40-
Write-PassThruOuput $($commandResult.Output)
39+
if (!$Quiet) {
40+
Write-CommandOuput $($result.LintOutput)
4141
}
4242
return $result
4343
}

Source/Public/Invoke-DockerLogin.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ function Invoke-DockerLogin {
1616
$Registry,
1717

1818
[Switch]
19-
$PassThru
19+
$Quiet = [System.Convert]::ToBoolean($env:DOCKER_CI_QUIET_MODE)
2020
)
2121
[String] $plaintextPassword = [System.Net.NetworkCredential]::new("", $Password).Password
2222
$command = "Write-Output `"${plainTextPassword}`" | docker login --username `"${Username}`" --password-stdin ${Registry}".TrimEnd()
2323
$maskedCommand = $command.Replace($plaintextPassword, "*********")
2424
Write-Debug ($maskedCommand)
2525
[CommandResult] $commandResult = Invoke-Command $command
26-
if ($PassThru) {
27-
Write-PassThruOuput $($commandResult.Output)
26+
if (!$Quiet) {
27+
Write-CommandOuput $($commandResult.Output)
2828
}
2929
# Mask password from being shown
3030
$commandResult.Command = $maskedCommand

Source/Public/Invoke-DockerPull.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function Invoke-DockerPull {
2525
$Digest = '',
2626

2727
[Switch]
28-
$PassThru
28+
$Quiet = [System.Convert]::ToBoolean($env:DOCKER_CI_QUIET_MODE)
2929
)
3030

3131
if ($ImageName.Contains(':') -or $ImageName.Contains('@')) {
@@ -55,8 +55,8 @@ function Invoke-DockerPull {
5555
'Registry' = $postfixedRegistry
5656
'Digest' = $Digest
5757
}
58-
if ($PassThru) {
59-
Write-PassThruOuput $($commandResult.Output)
58+
if (!$Quiet) {
59+
Write-CommandOuput $($commandResult.Output)
6060
}
6161
return $result
6262
}

Source/Public/Invoke-DockerPush.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function Invoke-DockerPush {
1818
$Tag = 'latest',
1919

2020
[Switch]
21-
$PassThru
21+
$Quiet = [System.Convert]::ToBoolean($env:DOCKER_CI_QUIET_MODE)
2222
)
2323
$postfixedRegistry = Add-PostFix $Registry
2424
$command = "docker push ${postfixedRegistry}${ImageName}:${Tag}"
@@ -30,8 +30,8 @@ function Invoke-DockerPush {
3030
'Registry' = $postfixedRegistry;
3131
'Tag' = $Tag;
3232
}
33-
if ($PassThru) {
34-
Write-PassThruOuput $($commandResult.Output)
33+
if (!$Quiet) {
34+
Write-CommandOuput $($commandResult.Output)
3535
}
3636
return $result
3737
}

Source/Public/Invoke-DockerTag.ps1

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function Invoke-DockerTag {
2828
$NewTag = 'latest',
2929

3030
[Switch]
31-
$PassThru
31+
$Quiet = [System.Convert]::ToBoolean($env:DOCKER_CI_QUIET_MODE)
3232
)
3333

3434
$postfixedRegistry = Add-Postfix -Value $Registry
@@ -39,13 +39,13 @@ function Invoke-DockerTag {
3939
$commandResult = Invoke-Command "docker tag ${source} ${target}"
4040
Assert-ExitCodeOk $commandResult
4141
$result = [PSCustomObject]@{
42-
'Tag' = $NewTag
43-
'ImageName' = $NewImageName
4442
'Registry' = $postfixedNewRegistry
43+
'ImageName' = $NewImageName
44+
'Tag' = $NewTag
4545
'CommandResult' = $commandResult
4646
}
47-
if ($PassThru) {
48-
Write-PassThruOuput $($commandResult.Output)
47+
if (!$Quiet) {
48+
Write-CommandOuput $($commandResult.Output)
4949
}
5050
return $result
5151
}

Source/Public/Invoke-DockerTests.ps1

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function Invoke-DockerTests {
2121
$TreatTestFailuresAsExceptions = $false,
2222

2323
[Switch]
24-
$PassThru
24+
$Quiet = [System.Convert]::ToBoolean($env:DOCKER_CI_QUIET_MODE)
2525
)
2626
if ($null -eq $ConfigFiles -or $ConfigFiles.Length -eq 0) {
2727
throw [System.ArgumentException]::new('$ConfigFiles must contain one more test configuration file paths.')
@@ -58,17 +58,21 @@ function Invoke-DockerTests {
5858
}
5959

6060
$testReportPath = Join-Path $absoluteTestReportDir $TestReportName
61+
$testReportExists = Test-Path -Path $testReportPath -PathType Leaf
62+
if ($testReportExists) {
63+
$testResult = $(ConvertFrom-Json $(Get-Content $testReportPath))
64+
}
6165

6266
$result = [PSCustomObject]@{
6367
# Todo: Need to check if the test report folder is missing.
6468
# It should not crash when folder is not there, but should simply return nothing
65-
'TestResult' = $(ConvertFrom-Json $(Get-Content $testReportPath))
69+
'TestResult' = $testResult
6670
'TestReportPath' = $testReportPath
6771
'CommandResult' = $commandResult
6872
'ImageName' = $ImageName
6973
}
70-
if ($PassThru) {
71-
Write-PassThruOuput $($commandResult.Output)
74+
if (!$Quiet) {
75+
Write-CommandOuput $($result.TestResult)
7276
}
7377
return $result
7478
}

Test-Source/Docker.Build.Tests.psm1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ function Set-GlobalVar {
1313
}
1414
}
1515

16+
# Set the module to quiet mode for testing purposes so we don't spam the test logs.
17+
$env:DOCKER_CI_QUIET_MODE = $true
18+
1619
# Add any variables that are needed globally in test scope
1720
Set-GlobalVar -Variable TestDataDir -Value (Join-Path $PSScriptRoot '../Test-Data')
1821
Set-GlobalVar -Variable DockerImagesDir -Value(Join-Path $Global:TestDataDir 'DockerImage')

Test-Source/Invoke-DockerBuild.Tests.ps1

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,22 @@ Describe 'Build docker images' {
9999
}
100100
}
101101

102-
Context 'Passthru execution' {
102+
Context 'Verbosity of execution' {
103103

104-
It 'Captures the output of the command invoked' {
104+
It 'outputs the result if Quiet is disabled' {
105105
$tempFile = New-TemporaryFile
106-
Invoke-DockerBuild -ImageName "leeandrasmus" -Dockerfile $dockerFile -Passthru 6> $tempFile
106+
Invoke-DockerBuild -ImageName "leeandrasmus" -Dockerfile $dockerFile -Quiet:$false 6> $tempFile
107107
$result = Get-Content $tempFile
108108

109109
$result | Should -Be @('Hello', 'World')
110110
}
111+
112+
It 'produces no output if quiet mode is enabled' {
113+
$tempFile = New-TemporaryFile
114+
Invoke-DockerBuild -ImageName "leeandrasmus" -Dockerfile $dockerFile -Quiet:$true 6> $tempFile
115+
$result = Get-Content $tempFile
116+
117+
$result | Should -BeNullOrEmpty
118+
}
111119
}
112120
}

0 commit comments

Comments
 (0)