From 4fb5f350e1b62180b344eb2f367df03cc0888422 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Wed, 27 Sep 2023 16:34:45 +0530 Subject: [PATCH] feat: optionally run container detached and optionall pull before run --- src/docker.rs | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src/docker.rs b/src/docker.rs index 2b8da69..cb36fc1 100644 --- a/src/docker.rs +++ b/src/docker.rs @@ -14,16 +14,35 @@ impl Docker { x.get(1).unwrap().trim().to_string() } - pub fn run_container(name: &str, img: &str, env: &HashMap) { + pub fn run_container( + name: &str, + img: &str, + detached: bool, + env: &HashMap, + network: Option, + pull: bool, + ) { let mut env_args = Vec::with_capacity(env.len() * 2 + 6); env_args.push("run".to_string()); - env_args.push("-d".to_string()); + if detached { + env_args.push("-d".to_string()); + } + + if pull { + env_args.push("--pull=always".into()); + } env_args.push("--name".to_string()); env_args.push(name.to_string()); for (k, v) in env.iter() { env_args.push("-e".to_string()); env_args.push(format!("{k}={v}")); } + + if let Some(network) = network { + env_args.push("--network".into()); + env_args.push(network); + } + env_args.push(img.to_string()); let mut child = Command::new("docker") .args(&env_args) @@ -75,7 +94,14 @@ mod tests { let mut env = HashMap::new(); env.insert("FOO".to_string(), "BAR".to_string()); env.insert("BAZ".to_string(), "BOO".to_string()); - Docker::run_container(&name, "forgeflux/ftest-dev-docker-cmd", &env); + Docker::run_container( + &name, + "forgeflux/ftest-dev-docker-cmd", + true, + &env, + None, + false, + ); let out = Command::new("docker") .args(["container", "inspect", "-f", "'{{.State.Running}}'", &name]) .output()