Skip to content

Commit e07a537

Browse files
authored
Merge pull request #1473 from mittwald/feat/container-limits
Add resource limits support to container run command
2 parents 3d03250 + 32a2de6 commit e07a537

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

docs/container.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,10 +518,12 @@ ARGUMENTS
518518
FLAGS
519519
-P, --publish-all publish all ports that are defined in the image
520520
-e, --env=<value>... set environment variables in the container
521+
-m, --memory=<value> set memory limit for the container
521522
-p, --project-id=<value> ID or short ID of a project; this flag is optional if a default project is set in the
522523
context
523524
-q, --quiet suppress process output and only display a machine-readable summary
524525
-v, --volume=<value>... bind mount a volume to the container
526+
--cpus=<value> set CPU limit for the container
525527
--create-volumes automatically create named volumes that do not exist
526528
--description=<value> add a descriptive label to the container
527529
--entrypoint=<value> override the default entrypoint of the container image
@@ -542,6 +544,11 @@ FLAG DESCRIPTIONS
542544
543545
Format: KEY=VALUE. Multiple environment variables can be specified with multiple --env flags.
544546
547+
-m, --memory=<value> set memory limit for the container
548+
549+
Specify the maximum amount of memory the container can use (e.g., '512m', '1g', '2g'). This is equivalent to the
550+
docker run --memory flag or the deploy.resources.limits.memory field in docker-compose.
551+
545552
-p, --project-id=<value> ID or short ID of a project; this flag is optional if a default project is set in the context
546553
547554
May contain a short ID or a full ID of a project; you can also use the "mw context set --project-id=<VALUE>" command
@@ -559,6 +566,11 @@ FLAG DESCRIPTIONS
559566
mount a path from your hosting environment's file system (NOT your local file system) into the container. You can
560567
also specify a named volume, which needs to be created beforehand.
561568
569+
--cpus=<value> set CPU limit for the container
570+
571+
Specify the number of CPUs available to the container (e.g., '0.5', '1', '2'). This is equivalent to the docker run
572+
--cpus flag or the deploy.resources.limits.cpus field in docker-compose.
573+
562574
--create-volumes automatically create named volumes that do not exist
563575
564576
When enabled, any named volumes referenced in --volume flags that do not already exist will be automatically created

src/commands/container/run.tsx

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,21 @@ export class Run extends ExecRenderBaseCommand<typeof Run, Result> {
117117
required: false,
118118
default: false,
119119
}),
120+
cpus: Flags.string({
121+
summary: "set CPU limit for the container",
122+
description:
123+
"Specify the number of CPUs available to the container (e.g., '0.5', '1', '2'). " +
124+
"This is equivalent to the docker run --cpus flag or the deploy.resources.limits.cpus field in docker-compose.",
125+
required: false,
126+
}),
127+
memory: Flags.string({
128+
summary: "set memory limit for the container",
129+
description:
130+
"Specify the maximum amount of memory the container can use (e.g., '512m', '1g', '2g'). " +
131+
"This is equivalent to the docker run --memory flag or the deploy.resources.limits.memory field in docker-compose.",
132+
required: false,
133+
char: "m",
134+
}),
120135
};
121136
static args = {
122137
image: Args.string({
@@ -268,6 +283,36 @@ export class Run extends ExecRenderBaseCommand<typeof Run, Result> {
268283
return command;
269284
}
270285

286+
/**
287+
* Builds the deploy.resources structure from command line flags
288+
*
289+
* @returns The deploy configuration with resource limits, or undefined if no
290+
* limits are specified
291+
*/
292+
private buildDeployResources():
293+
| { resources: { limits: { cpus?: string; memory?: string } } }
294+
| undefined {
295+
if (!this.flags.cpus && !this.flags.memory) {
296+
return undefined;
297+
}
298+
299+
const limits: { cpus?: string; memory?: string } = {};
300+
301+
if (this.flags.cpus) {
302+
limits.cpus = this.flags.cpus;
303+
}
304+
305+
if (this.flags.memory) {
306+
limits.memory = this.flags.memory;
307+
}
308+
309+
return {
310+
resources: {
311+
limits,
312+
},
313+
};
314+
}
315+
271316
/**
272317
* Builds a container service request from command line arguments and image
273318
* metadata
@@ -297,6 +342,7 @@ export class Run extends ExecRenderBaseCommand<typeof Run, Result> {
297342
this.flags.publish,
298343
);
299344
const volumes = this.flags.volume;
345+
const deploy = this.buildDeployResources();
300346

301347
return {
302348
image,
@@ -306,6 +352,7 @@ export class Run extends ExecRenderBaseCommand<typeof Run, Result> {
306352
environment,
307353
ports,
308354
volumes,
355+
deploy,
309356
};
310357
}
311358

0 commit comments

Comments
 (0)