Skip to content

Commit 5e9c562

Browse files
gfraiteurclaude
andcommitted
Deprecate Memory property from ContainerHostRequirements
Remove Memory configuration from Docker containers as it's not supported in process isolation mode. --memory and --cpus flags are now only passed to Docker when using hyperv isolation. - Remove Memory property from ContainerHostRequirements - Make DockerSpec.Memory nullable with no default - Update DockerBuild.ps1 to conditionally add resource limits - Remove unused variables and duplicate display string logic Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 87a69fd commit 5e9c562

7 files changed

Lines changed: 49 additions & 43 deletions

File tree

DockerBuild.ps1

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ param(
1919
[string]$Script = 'Build.ps1', # The build script to be executed inside Docker.
2020
[string]$Dockerfile, # Path to custom Dockerfile (defaults to Dockerfile or Dockerfile.claude based on -Claude).
2121
[switch]$NoInit, # Do not generate or call Init.g.ps1 (skips git config, safe.directory, etc).
22-
[string]$Isolation = 'process', # Docker isolation mode (process or hyperv).
23-
[string]$Memory, # Docker memory limit. Default calculated from $DefaultMemoryGb.
24-
[int]$Cpus = [Environment]::ProcessorCount, # Docker CPU limit (defaults to host's CPU count).
22+
[string]$Isolation = 'process', # Docker isolation mode (process or hyperv). Memory/CPU limits only apply to hyperv.
23+
[string]$Memory, # Docker memory limit (e.g., "8g"). Only used with hyperv isolation.
24+
[int]$Cpus = [Environment]::ProcessorCount, # Docker CPU limit. Only used with hyperv isolation.
2525
[string[]]$Mount, # Additional directories to mount from host (readonly by default, append :w for writable). Supports * and ** glob patterns.
2626
[Parameter(ValueFromRemainingArguments)]
2727
[string[]]$BuildArgs # Arguments passed to `Build.ps1` within the container (or Claude prompt if -Claude is specified).
@@ -31,15 +31,8 @@ param(
3131
# These settings are replaced by the generate-scripts command.
3232
$EngPath = 'eng'
3333
$EnvironmentVariables = 'AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY,AZ_IDENTITY_USERNAME,AZURE_CLIENT_ID,AZURE_CLIENT_SECRET,AZURE_DEVOPS_TOKEN,AZURE_DEVOPS_USER,AZURE_TENANT_ID,DOC_API_KEY,DOWNLOADS_API_KEY,ENG_USERNAME,GIT_USER_EMAIL,GIT_USER_NAME,GITHUB_AUTHOR_EMAIL,GITHUB_REVIEWER_TOKEN,GITHUB_TOKEN,IS_POSTSHARP_OWNED,IS_TEAMCITY_AGENT,MetalamaLicense,NUGET_ORG_API_KEY,PostSharpLicense,SIGNSERVER_SECRET,TEAMCITY_CLOUD_TOKEN,TYPESENSE_API_KEY,VS_MARKETPLACE_ACCESS_TOKEN,VSS_NUGET_EXTERNAL_FEED_ENDPOINTS'
34-
$DefaultMemoryGb = 8
3534
####
3635

37-
# Calculate default memory: add 4GB for Claude mode
38-
if (-not $Memory) {
39-
$memoryGb = if ($Claude) { $DefaultMemoryGb + 4 } else { $DefaultMemoryGb }
40-
$Memory = "${memoryGb}g"
41-
}
42-
4336
$ErrorActionPreference = "Stop"
4437
$dockerContextDirectory = "$EngPath/docker-context"
4538

@@ -1112,7 +1105,15 @@ RUN if [ -n "`$MOUNTPOINTS" ]; then \
11121105
}
11131106

11141107
Write-Host "Building the image with tag: $ImageTag" -ForegroundColor Green
1115-
$dockerfileContent | docker build -t $ImageTag --memory=$Memory --build-arg MOUNTPOINTS="$mountPointsAsString" -f - $dockerContextDirectory
1108+
1109+
# Build docker build command with optional --memory (not supported in process isolation)
1110+
$dockerBuildCmd = @('build', '-t', $ImageTag)
1111+
if ($Memory -and $Isolation -ne 'process') {
1112+
$dockerBuildCmd += "--memory=$Memory"
1113+
}
1114+
$dockerBuildCmd += @('--build-arg', "MOUNTPOINTS=$mountPointsAsString", '-f', '-', $dockerContextDirectory)
1115+
1116+
$dockerfileContent | & docker @dockerBuildCmd
11161117
if ($LASTEXITCODE -ne 0)
11171118
{
11181119
Write-Host "Docker build failed with exit code $LASTEXITCODE" -ForegroundColor Red
@@ -1147,7 +1148,6 @@ if (-not $BuildImage)
11471148
{
11481149
$volumeArgs += @("-v", $mapping)
11491150
}
1150-
$VolumeMappingsAsString = ($VolumeMappings | ForEach-Object { "-v $_" }) -join " "
11511151

11521152
if ($Claude)
11531153
{
@@ -1287,7 +1287,6 @@ if (-not $BuildImage)
12871287

12881288
# Common docker execution for both modes
12891289
$dockerArgsAsString = $dockerArgs -join " "
1290-
$envArgsAsString = ($envArgs -join " ")
12911290

12921291
# Execute docker command
12931292
if ($existingContainerId)
@@ -1299,10 +1298,17 @@ if (-not $BuildImage)
12991298
else
13001299
{
13011300
# Start new container with docker run
1302-
Write-Host "Executing: ``docker run --rm --memory=$Memory --cpus=$Cpus $isolationArg $dockerArgsAsString $VolumeMappingsAsString $envArgsAsString -w $ContainerCallingDir $ImageTag `"$pwshPath`" $pwshArgs -Command `"$inlineScript`"" -ForegroundColor Cyan
1303-
13041301
# Build docker command with proper argument handling (avoid empty strings)
1305-
$dockerCmd = @('run', '--rm', "--memory=$Memory", "--cpus=$Cpus")
1302+
$dockerCmd = @('run', '--rm')
1303+
1304+
# Only add --memory and --cpus when NOT using process isolation
1305+
if ($Isolation -ne 'process') {
1306+
if ($Memory) {
1307+
$dockerCmd += "--memory=$Memory"
1308+
}
1309+
$dockerCmd += "--cpus=$Cpus"
1310+
}
1311+
13061312
if ($isolationArg) { $dockerCmd += $isolationArg }
13071313
$dockerCmd += $dockerArgs
13081314
$dockerCmd += $volumeArgs
@@ -1313,6 +1319,7 @@ if (-not $BuildImage)
13131319
$dockerCmd += @('-w', $ContainerCallingDir, $ImageTag, $pwshPath, '-Command', $inlineScript)
13141320
}
13151321

1322+
Write-Host "Executing: ``docker $($dockerCmd -join ' ')" -ForegroundColor Cyan
13161323
& docker @dockerCmd
13171324
}
13181325
$dockerExitCode = $LASTEXITCODE

src/PostSharp.Engineering.BuildTools/Build/Model/Product.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,7 @@ void AddComponents( IEnumerable<IBuildComponent>? newComponents )
223223
public DockerSpec? DockerSpec
224224
=> this.ResolvedBuildAgentRequirements is ContainerHostRequirements containerHostRequirements
225225
? new DockerSpec(
226-
$"{this.ProductNameWithoutDot}-{this.ProductFamily.Version}".ToLowerInvariant(),
227-
containerHostRequirements.Memory )
226+
$"{this.ProductNameWithoutDot}-{this.ProductFamily.Version}".ToLowerInvariant() )
228227
: null;
229228

230229
public bool IsPublishingNonReleaseBranchesAllowed { get; init; }

src/PostSharp.Engineering.BuildTools/ContinuousIntegration/Model/PowershellAdditionalCiBuildConfiguration.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ internal override TeamCityBuildConfiguration TeamCityBuildConfiguration(
108108
this.Arguments,
109109
this.BuildAgentRequirements == null ? product.DockerSpec :
110110
this.BuildAgentRequirements is ContainerHostRequirements containerHostRequirements ? new DockerSpec(
111-
$"{productProperties.Product.ProductNameWithoutDot}-{productProperties.Product.ProductFamily.Version}-{this.Id}".ToLowerInvariant(),
112-
containerHostRequirements.Memory ) :
111+
$"{productProperties.Product.ProductNameWithoutDot}-{productProperties.Product.ProductFamily.Version}-{this.Id}".ToLowerInvariant() ) :
113112
null,
114113
true )
115114
{

src/PostSharp.Engineering.BuildTools/Docker/ContainerHostRequirements.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ public record ContainerHostRequirements : BuildAgentRequirements
1313
{
1414
public ContainerHostKind HostKind { get; }
1515

16-
/// <summary>
17-
/// Gets or sets the memory limit in GB for Docker containers. Default is 8 GB.
18-
/// </summary>
19-
public int Memory { get; init; } = 8;
20-
2116
public ContainerHostRequirements( ContainerHostKind hostKind ) : base(
2217
new BuildAgentRequirement( "env.BuildAgentType", ContainerHelper.GetBuildAgentType( hostKind ) ) )
2318
{

src/PostSharp.Engineering.BuildTools/Docker/DockerSpec.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
namespace PostSharp.Engineering.BuildTools.Docker;
44

5-
public record DockerSpec( string ImageName, int Memory = 8 );
5+
public record DockerSpec( string ImageName, int? Memory = null );

src/PostSharp.Engineering.BuildTools/Resources/DockerBuild.ps1

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ param(
1919
[string]$Script = 'Build.ps1', # The build script to be executed inside Docker.
2020
[string]$Dockerfile, # Path to custom Dockerfile (defaults to Dockerfile or Dockerfile.claude based on -Claude).
2121
[switch]$NoInit, # Do not generate or call Init.g.ps1 (skips git config, safe.directory, etc).
22-
[string]$Isolation = 'process', # Docker isolation mode (process or hyperv).
23-
[string]$Memory, # Docker memory limit. Default calculated from $DefaultMemoryGb.
24-
[int]$Cpus = [Environment]::ProcessorCount, # Docker CPU limit (defaults to host's CPU count).
22+
[string]$Isolation = 'process', # Docker isolation mode (process or hyperv). Memory/CPU limits only apply to hyperv.
23+
[string]$Memory, # Docker memory limit (e.g., "8g"). Only used with hyperv isolation.
24+
[int]$Cpus = [Environment]::ProcessorCount, # Docker CPU limit. Only used with hyperv isolation.
2525
[string[]]$Mount, # Additional directories to mount from host (readonly by default, append :w for writable). Supports * and ** glob patterns.
2626
[Parameter(ValueFromRemainingArguments)]
2727
[string[]]$BuildArgs # Arguments passed to `Build.ps1` within the container (or Claude prompt if -Claude is specified).
@@ -31,15 +31,8 @@ param(
3131
# These settings are replaced by the generate-scripts command.
3232
$EngPath = '<ENG_PATH>'
3333
$EnvironmentVariables = '<ENVIRONMENT_VARIABLES>'
34-
$DefaultMemoryGb = <DEFAULT_MEMORY_GB>
3534
####
3635

37-
# Calculate default memory: add 4GB for Claude mode
38-
if (-not $Memory) {
39-
$memoryGb = if ($Claude) { $DefaultMemoryGb + 4 } else { $DefaultMemoryGb }
40-
$Memory = "${memoryGb}g"
41-
}
42-
4336
$ErrorActionPreference = "Stop"
4437
$dockerContextDirectory = "$EngPath/docker-context"
4538

@@ -1112,7 +1105,15 @@ RUN if [ -n "`$MOUNTPOINTS" ]; then \
11121105
}
11131106

11141107
Write-Host "Building the image with tag: $ImageTag" -ForegroundColor Green
1115-
$dockerfileContent | docker build -t $ImageTag --memory=$Memory --build-arg MOUNTPOINTS="$mountPointsAsString" -f - $dockerContextDirectory
1108+
1109+
# Build docker build command with optional --memory (not supported in process isolation)
1110+
$dockerBuildCmd = @('build', '-t', $ImageTag)
1111+
if ($Memory -and $Isolation -ne 'process') {
1112+
$dockerBuildCmd += "--memory=$Memory"
1113+
}
1114+
$dockerBuildCmd += @('--build-arg', "MOUNTPOINTS=$mountPointsAsString", '-f', '-', $dockerContextDirectory)
1115+
1116+
$dockerfileContent | & docker @dockerBuildCmd
11161117
if ($LASTEXITCODE -ne 0)
11171118
{
11181119
Write-Host "Docker build failed with exit code $LASTEXITCODE" -ForegroundColor Red
@@ -1147,7 +1148,6 @@ if (-not $BuildImage)
11471148
{
11481149
$volumeArgs += @("-v", $mapping)
11491150
}
1150-
$VolumeMappingsAsString = ($VolumeMappings | ForEach-Object { "-v $_" }) -join " "
11511151

11521152
if ($Claude)
11531153
{
@@ -1287,7 +1287,6 @@ if (-not $BuildImage)
12871287

12881288
# Common docker execution for both modes
12891289
$dockerArgsAsString = $dockerArgs -join " "
1290-
$envArgsAsString = ($envArgs -join " ")
12911290

12921291
# Execute docker command
12931292
if ($existingContainerId)
@@ -1299,10 +1298,17 @@ if (-not $BuildImage)
12991298
else
13001299
{
13011300
# Start new container with docker run
1302-
Write-Host "Executing: ``docker run --rm --memory=$Memory --cpus=$Cpus $isolationArg $dockerArgsAsString $VolumeMappingsAsString $envArgsAsString -w $ContainerCallingDir $ImageTag `"$pwshPath`" $pwshArgs -Command `"$inlineScript`"" -ForegroundColor Cyan
1303-
13041301
# Build docker command with proper argument handling (avoid empty strings)
1305-
$dockerCmd = @('run', '--rm', "--memory=$Memory", "--cpus=$Cpus")
1302+
$dockerCmd = @('run', '--rm')
1303+
1304+
# Only add --memory and --cpus when NOT using process isolation
1305+
if ($Isolation -ne 'process') {
1306+
if ($Memory) {
1307+
$dockerCmd += "--memory=$Memory"
1308+
}
1309+
$dockerCmd += "--cpus=$Cpus"
1310+
}
1311+
13061312
if ($isolationArg) { $dockerCmd += $isolationArg }
13071313
$dockerCmd += $dockerArgs
13081314
$dockerCmd += $volumeArgs
@@ -1313,6 +1319,7 @@ if (-not $BuildImage)
13131319
$dockerCmd += @('-w', $ContainerCallingDir, $ImageTag, $pwshPath, '-Command', $inlineScript)
13141320
}
13151321

1322+
Write-Host "Executing: ``docker $($dockerCmd -join ' ')" -ForegroundColor Cyan
13161323
& docker @dockerCmd
13171324
}
13181325
$dockerExitCode = $LASTEXITCODE

src/PostSharp.Engineering.BuildTools/Utilities/EmbeddedResourceHelper.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public static void ExtractScript( BuildContext context, string fileName, string
1717
replacements.Add( "<ENG_PATH>", product.EngineeringDirectory );
1818
replacements.Add( "<ENVIRONMENT_VARIABLES>", string.Join( ",", EnvironmentVariableNames.All.OrderBy( x => x ) ) );
1919
replacements.Add( "<PRODUCT_NAME>", product.ProductNameWithoutDot );
20-
replacements.Add( "<DEFAULT_MEMORY_GB>", (product.DockerSpec?.Memory ?? 8).ToString( System.Globalization.CultureInfo.InvariantCulture ) );
2120

2221
ExtractResource( context, fileName, targetDirectory, replacements );
2322
}

0 commit comments

Comments
 (0)