feat: optionally run container detached and optionall pull before run

This commit is contained in:
Aravinth Manivannan 2023-09-27 16:34:45 +05:30
parent b3f7cb943e
commit 4fb5f350e1
Signed by: realaravinth
GPG key ID: F8F50389936984FF

View file

@ -14,16 +14,35 @@ impl Docker {
x.get(1).unwrap().trim().to_string()
}
pub fn run_container(name: &str, img: &str, env: &HashMap<String, String>) {
pub fn run_container(
name: &str,
img: &str,
detached: bool,
env: &HashMap<String, String>,
network: Option<String>,
pull: bool,
) {
let mut env_args = Vec::with_capacity(env.len() * 2 + 6);
env_args.push("run".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()