forked from rogerfar/rdt-client
-
Notifications
You must be signed in to change notification settings - Fork 5
/
docker-build.ps1
executable file
·167 lines (136 loc) · 5.86 KB
/
docker-build.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
#!/usr/bin/env pwsh
<#
.SYNOPSIS
Helper script to execute docker buildx
.DESCRIPTION
Simplifies executing docker buildx for multi-architecture for the current Dockerfile
.PARAMETER Version
Specifies the version number to take this image with (defaults to 'latest' only)
.PARAMETER DockerAccount
The docker account to use to push the image
.PARAMETER Platforms
The platforms to target for the image (defaults to linux/arm/v7,linux/arm64/v8,linux/amd64)
.INPUTS
None. You cannot pipe objects to to this script.
.OUTPUTS
None
.EXAMPLE
PS> /docker-build.ps1
.EXAMPLE
PS> ./docker-build.ps1 -Version v1.7.4
.EXAMPLE
PS> ./docker-build.ps1 -Platforms "linux/arms64/v8"
#>
param(
[string]$Version = "",
[string]$DockerAccount = "rogerfar",
[string]$Platforms = "linux/arm64/v8,linux/amd64",
[string]$Dockerfile = "Dockerfile",
[switch]$SkipPush,
[switch]$SkipCache,
[switch]$OutputToDocker,
[string]$BuildProgress="auto",
[switch]$AutoRun
)
# Set Defaults
$defaultPort = 6500
$appName = "rdtclient"
$imageName = "$($DockerAccount)/$($appName)"
# Load version details (if needed)
if ([string]::IsNullOrEmpty($Version)) {
$Version = (Get-Content "package.json" | ConvertFrom-Json).version
}
# Define the default arguments for docker build and run
$dockerCommandArgsList = @()
$baseDockerBuildArgs = @( "buildx", "build", "--network=default", "--progress=$BuildProgress", "--file", $Dockerfile, "--build-arg", "VERSION=$($Version)", "." )
$baseDockerRunArgs = @( "run", "--cap-add=NET_ADMIN", "-d", "--log-driver", "json-file", "--log-opt", "max-size=10m" )
# Add any conditional argumes
if (-Not $SkipPush.IsPresent) { $baseDockerBuildArgs += @("--push") }
if ($SkipCache.IsPresent) { $baseDockerBuildArgs += @("--no-cache") }
if ($OutputToDocker.IsPresent) { $baseDockerBuildArgs += @("--load") }
# If we have multiple platforms AND we want to export them to locally to docker so we can run them
# we need to build and tag each arch individually
$lstPLatforms = $Platforms -split ","
if ($OutputToDocker.IsPresent -and $lstPLatforms.Count -gt 0) {
$extPort=$defaultPort
foreach ($p in $lstPLatforms) {
$extPort++
$pn = $p -replace "/", "-"
$dbArgs = $baseDockerBuildArgs
$dbArgs += @("--tag", "${imageName}:${Version}-${pn}")
$dbArgs += @("--platform", $p)
$drArgs = $baseDockerRunArgs
$drArgs += @( "-v", "${PWD}/data/${pn}/db:/data/db" )
$drArgs += @( "-v", "${PWD}/data/${pn}/downloads:/data/downloads" )
$drArgs += @( "--platform", $p )
$drArgs += @( "-p", "${extPort}:${defaultPort}")
$drArgs += @( "--name", "${appName}-${pn}" )
$drArgs += @( "${imageName}:${Version}-${pn}")
$o = @{
ImageName = "${imageName}:${Version}-${pn}"
AppName = "${appName}-${pn}"
Platform = $p
CmdArgs = $dbArgs
RunArgs = $drArgs
}
$dockerCommandArgsList += @(, $o)
}
} else {
# Looks like we don't need to load them locally so lets build everything at once as a sinlge mutli-arch image
$dbArgs = $baseDockerBuildArgs
$dbArgs += @("--tag", "${imageName}:${Version}")
$dbArgs += @("--platform", $Platforms)
$drArgs = $baseDockerRunArgs
$drArgs += @( "-v", "${PWD}/data/db:/data/db" )
$drArgs += @( "-v", "${PWD}/data/downloads:/data/downloads" )
$drArgs += @( "-p ${defaultPort}:${defaultPort}")
$drArgs += @( "--name", "${appName}" )
$drArgs += @( "${imageName}:${Version}")
$o = @{
ImageName = "${imageName}:${Version}"
AppName = "${appName}"
Platform = $Platforms
CmdArgs = $ddbArgs
RunArgs = $drArgs
}
$dockerCommandArgsList += @(, $o)
}
# Lets trigger the builds
foreach ($c in $dockerCommandArgsList) {
Write-Host "Generating docker image $imageName for $($c.Platform)" -ForegroundColor Green
Write-Host "Args: $($c.CmdArgs)" -ForegroundColor Yellow
&docker $($c.CmdArgs)
if ($AutoRun.IsPresent) {
Write-Host "Running docker image $imageName for $($c.Platform)" -ForegroundColor Green
Write-Host "Args: $($c.RunArgs)" -ForegroundColor Yellow
&docker $($c.RunArgs)
Write-Host "Sleeping for 30s (allowing containers to start)" -ForegroundColor Green
Start-Sleep -Seconds 30
Write-Host "Checking on status of containers" -ForegroundColor Green
&docker container ls --all --filter "name=$($c.AppName)" --format '{{json .}}' | jq --slurp | ConvertFrom-Json | % {
if ($d.Status -contains "*unhealthy*" ) {
Write-Host "Image ${c.ImageName} is not starting correctly" -ForegroundColor Red
} else {
Write-Host "Image ${c.ImageName} is starting correctly" -ForegroundColor Green
}
}
}
}
# # Do we want to automatically start them after they are build?
# if ($AutoRun.IsPresent) {
# foreach ($c in $dockerCommandArgsList) {
# Write-Host "Running docker image $imageName for $($c.Platform)" -ForegroundColor Green
# Write-Host "Args: $($c.RunArgs)" -ForegroundColor Yellow
# docker $($c.RunArgs)
# }
# Write-Host "Sleeping for 30s (allowing containers to start)" -ForegroundColor Green
# Start-Sleep -Seconds 30
# Write-Host "Checking on status of containers" -ForegroundColor Green
# docker container ls --all --filter "name=$appName" --format '{{json .}}' | jq --slurp | ConvertFrom-Json | % {
# if ($d.Status -contains "*unhealthy*" ) {
# Write-Host "Image ${c.ImageName} is not starting correctly" -ForegroundColor Red
# } else {
# Write-Host "Image ${c.ImageName} is starting correctly" -ForegroundColor Green
# }
# }
# }