Skip to content

Commit f458f39

Browse files
committed
Use docker limits with Env
1 parent 5083bdf commit f458f39

File tree

1 file changed

+47
-11
lines changed

1 file changed

+47
-11
lines changed

src/main.rs

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,21 @@ pub fn get_genesis_hash(url: Option<String>) -> anyhow::Result<String> {
314314
Ok(genesis_hash.to_string())
315315
}
316316

317+
318+
pub fn get_docker_resource_limits() -> Option<(String, String)> {
319+
let memory = std::env::var("SVB_DOCKER_MEMORY_LIMIT").ok();
320+
let cpus = std::env::var("SVB_DOCKER_CPU_LIMIT").ok();
321+
if memory.is_some() || cpus.is_some() {
322+
println!("Using docker resource limits: memory: {:?}, cpus: {:?}", memory, cpus);
323+
} else {
324+
// Print message to user that they can set these environment variables to limit docker resources
325+
println!("No Docker resource limits are set.");
326+
println!("You can set the SVB_DOCKER_MEMORY_LIMIT and SVB_DOCKER_CPU_LIMIT environment variables to limit Docker resources.");
327+
println!("For example: SVB_DOCKER_MEMORY_LIMIT=2g SVB_DOCKER_CPU_LIMIT=2.");
328+
}
329+
memory.zip(cpus)
330+
}
331+
317332
pub fn build(
318333
mount_directory: Option<String>,
319334
library_name: Option<String>,
@@ -428,12 +443,22 @@ pub fn build(
428443

429444
// change directory to program/build dir
430445
let mount_params = format!("{}:{}", mount_path, workdir);
431-
let container_id = std::process::Command::new("docker")
432-
.args(["run", "--rm", "-v", &mount_params, "-dit","--memory=4g","--cpus=2", &image, "bash"])
433-
.stderr(Stdio::inherit())
434-
.output()
435-
.map_err(|e| anyhow::format_err!("Docker build failed: {}", e.to_string()))
436-
.and_then(|output| parse_output(output.stdout))?;
446+
let container_id = {
447+
let mut cmd = std::process::Command::new("docker");
448+
cmd.args(["run", "--rm", "-v", &mount_params, "-dit"]);
449+
cmd.stderr(Stdio::inherit());
450+
451+
if let Some((memory_limit, cpu_limit)) = get_docker_resource_limits() {
452+
cmd.arg("--memory").arg(memory_limit).arg("--cpus").arg(cpu_limit);
453+
}
454+
455+
let output = cmd
456+
.args([&image, "bash"])
457+
.output()
458+
.map_err(|e| anyhow!("Docker build failed: {}", e.to_string()))?;
459+
460+
parse_output(output.stdout)?
461+
};
437462

438463
// Set the container id so we can kill it later if the process is interrupted
439464
container_id_opt.replace(container_id.clone());
@@ -532,11 +557,22 @@ pub fn verify_from_image(
532557

533558
println!("Workdir: {}", workdir);
534559

535-
let container_id = std::process::Command::new("docker")
536-
.args(["run", "--rm", "-dit", image.as_str()])
537-
.output()
538-
.map_err(|e| anyhow::format_err!("Failed to run image {}", e.to_string()))
539-
.and_then(|output| parse_output(output.stdout))?;
560+
561+
let container_id = {
562+
let mut cmd = std::process::Command::new("docker");
563+
cmd.args(["run", "--rm", "-dit"]);
564+
cmd.stderr(Stdio::inherit());
565+
566+
if let Some((memory_limit, cpu_limit)) = get_docker_resource_limits() {
567+
cmd.arg("--memory").arg(memory_limit).arg("--cpus").arg(cpu_limit);
568+
}
569+
570+
let output = cmd
571+
.args([&image])
572+
.output()
573+
.map_err(|e| anyhow!("Docker build failed: {}", e.to_string()))?;
574+
parse_output(output.stdout)?
575+
};
540576

541577
container_id_opt.replace(container_id.clone());
542578

0 commit comments

Comments
 (0)