use futures::StreamExt; use moby::{tty::TtyChunk, Docker, ExecContainerOptions}; use std::{env, str::from_utf8}; #[tokio::main] async fn main() { let docker = Docker::new("tcp://127.0.0.1:80").unwrap(); let id = env::args() .nth(1) .expect("You need to specify a container id"); let options = ExecContainerOptions::builder() .cmd(vec![ "bash", "-c", "echo -n \"echo VAR=$VAR on stdout\"; echo -n \"echo VAR=$VAR on stderr\" >&2", ]) .env(vec!["VAR=value"]) .attach_stdout(true) .attach_stderr(true) .build(); while let Some(exec_result) = docker.containers().get(&id).exec(&options).next().await { match exec_result { Ok(chunk) => print_chunk(chunk), Err(e) => eprintln!("Error: {}", e), } } } fn print_chunk(chunk: TtyChunk) { match chunk { TtyChunk::StdOut(bytes) => println!("Stdout: {}", from_utf8(&bytes).unwrap()), TtyChunk::StdErr(bytes) => eprintln!("Stdout: {}", from_utf8(&bytes).unwrap()), TtyChunk::StdIn(_) => unreachable!(), } }