-
Notifications
You must be signed in to change notification settings - Fork 1
/
Run-Docker.ps1
263 lines (223 loc) · 7.46 KB
/
Run-Docker.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<#
.SYNOPSIS
This script automates Docker operations including building and optionally pushing a Docker image to a specified registry.
.DESCRIPTION
'Run-Docker.ps1' is a PowerShell script designed for automating Docker tasks. It includes functionalities for building a Docker image from a specified Dockerfile and optionally pushing this image to a Docker registry. The script is flexible and allows users to specify various parameters such as the Dockerfile name, image name, container name, registry details, and working directory.
.PARAMETERS
DockerFileName
The name of the Dockerfile to be used for building the Docker image. Default is 'Dockerfile'.
DockerImageName
The name to be assigned to the built Docker image. Default is 'example'.
RegistryUrl
The URL of the Docker registry where the image should be pushed. Default is 'ghcr.io'.
RegistryUsername
The username for the Docker registry.
RegistryPassword
The password for the Docker registry.
WorkingDirectory
The directory where the Dockerfile is located and where Docker commands will be executed.
DebugMode
Enables or disables debug mode. Accepts 'true' or 'false'. Default is 'false'.
PushDockerImage
Specifies whether to push the Docker image to the registry. Accepts 'true' or 'false'. Default is 'true'.
.FUNCTIONS
Convert-ToBoolean
Converts string parameters to boolean values.
Check-DockerExists
Checks if Docker is installed and available in the system's PATH.
Build-DockerImage
Builds a Docker image using the specified Dockerfile and path.
Push-DockerImage
Pushes the built Docker image to the specified registry.
.EXAMPLE
./Run-Docker.ps1 -WorkingDirectory "$(Get-Location)/containers/ubuntu" -RegistryUsername $Env:registry_username -RegistryPassword $Env:registry_password
This example demonstrates how to run the script with a specified working directory and Docker registry credentials sourced from environment variables.
.NOTES
Ensure Docker is installed and that the provided credentials for the Docker registry are valid. The script parameters can be adjusted according to specific requirements.
Author: Craig Thacker
Date: 11/12/2023
#>
param (
[string]$DockerFileName = "Dockerfile",
[string]$DockerImageName = "base-images/azdo-agent-containers",
[string]$RegistryUrl = "ghcr.io",
[string]$RegistryUsername = "myusername",
[string]$RegistryPassword = "mypassword",
[string]$ImageOrg,
[string]$WorkingDirectory = (Get-Location).Path,
[string]$DebugMode = "false",
[string]$PushDockerImage = "true",
[string[]]$AdditionalTags = @("latest", (Get-Date -Format "yyyy-MM"))
)
# Function to convert string to boolean
function Convert-ToBoolean($value)
{
$valueLower = $value.ToLower()
if ($valueLower -eq "true")
{
return $true
}
elseif ($valueLower -eq "false")
{
return $false
}
else
{
Write-Error "Error: Invalid value - $value. Exiting."
exit 1
}
}
# Function to check if Docker is installed
function Check-DockerExists
{
try
{
$dockerPath = Get-Command docker -ErrorAction Stop
Write-Host "Success: Docker found at: $( $dockerPath.Source )" -ForegroundColor Green
}
catch
{
Write-Error "Error: Docker is not installed or not in PATH. Exiting."
exit 1
}
}
if ($null -eq $ImageOrg)
{
$ImageOrganisation = $RegistryUsername
}
else
{
$ImageOrganisation = $ImageOrg
}
$DockerImageName = "${RegistryUrl}/${ImageOrganisation}/${DockerImageName}"
function Build-DockerImage
{
param (
[string]$Path,
[string]$DockerFile
)
$filePath = Join-Path -Path $Path -ChildPath $DockerFile
# Check if Dockerfile exists at the specified path
if (-not(Test-Path -Path $filePath))
{
Write-Error "Error: Dockerfile not found at $filePath. Exiting."
return $false
}
try
{
Write-Host "Info: Building Docker image $DockerImageName from $filePath" -ForegroundColor Green
if ($IsWindows)
{
docker build -t $DockerImageName -f $filePath $Path | Out-Host
}
else
{
docker build --squash -t $DockerImageName -f $filePath $Path | Out-Host
}
if ($LASTEXITCODE -eq 0)
{
return $true
}
else
{
Write-Error "Error: Docker build failed with exit code $LASTEXITCODE"
return $false
}
}
catch
{
Write-Error "Error: Docker build encountered an exception"
return $false
}
}
function Push-DockerImage
{
param (
[string[]]$FullTagNames
)
try
{
Write-Host "Info: Logging into Docker registry $RegistryUrl" -ForegroundColor Green
$RegistryPassword | docker login $RegistryUrl -u $RegistryUsername --password-stdin
if ($LASTEXITCODE -eq 0)
{
foreach ($tagName in $FullTagNames)
{
Write-Host "Info: Pushing Docker image $tagName to registry" -ForegroundColor Green
docker push $tagName | Out-Host
if ($LASTEXITCODE -eq 0)
{
Write-Host "Success: Docker image $tagName pushed successfully." -ForegroundColor Green
}
else
{
Write-Error "Error: Docker push failed for tag $tagName with exit code $LASTEXITCODE"
# Depending on your preference, you might choose to stop trying to push additional tags after the first failure
# return $false
}
}
# Assuming all tags are pushed successfully if we reach this point
return $true
}
else
{
Write-Error "Error: Docker login failed with exit code $LASTEXITCODE"
return $false
}
}
catch
{
Write-Error "Error: An exception occurred during Docker push"
return $false
}
finally
{
Write-Host "Info: Logging out of Docker registry $RegistryUrl" -ForegroundColor Green
docker logout $RegistryUrl
}
}
# Convert string parameters to boolean
$DebugMode = Convert-ToBoolean $DebugMode
$PushDockerImage = Convert-ToBoolean $PushDockerImage
# Enable debug mode if DebugMode is set to $true
if ($DebugMode)
{
$DebugPreference = "Continue"
}
# Diagnostic output
Write-Debug "DockerFileName: $DockerFileName"
Write-Debug "DockerImageName: $DockerImageName"
Write-Debug "DebugMode: $DebugMode"
# Checking prerequisites
Check-DockerExists
# Execution flow
$buildSuccess = Build-DockerImage -Path $WorkingDirectory -DockerFile $DockerFileName
if ($buildSuccess)
{
Write-Host "Docker build complete." -ForegroundColor Green
foreach ($tag in $AdditionalTags)
{
$fullTagName = "${DockerImageName}:$tag"
Write-Host "Info: Tagging Docker image as $fullTagName" -ForegroundColor Green
docker tag $DockerImageName $fullTagName
}
if ($PushDockerImage -eq $true)
{
$fullTagNames = @()
foreach ($tag in $AdditionalTags)
{
$fullTagNames += "${DockerImageName}:$tag"
}
Push-DockerImage -FullTagNames $fullTagNames
}
else
{
Write-Host "Docker image push failed." -ForegroundColor Red
exit 1
}
}
else
{
Write-Host "Docker build failed." -ForegroundColor Red
exit 1
}