softprops / shiplift

🐳 🦀 rust interface for maneuvering docker containers
MIT License
618 stars 119 forks source link

Exec Stream never ends #330

Open chillcaw opened 1 year ago

chillcaw commented 1 year ago

Background

Not sure if this a bug or I'm missing something here.

I'm copying a python file over and running it, but the Stream from Container::exec never seems to return None. When I run through docker exec it'll run once and exit.

# sample.py
my_var = 123
print(my_var)
print(124)
print(125)
print(126)
// main.rs
#[tokio::main]
async fn main() {

    // There is more code here, but it all works so far
    // ...

    println!("Running Exec");

    let options = ExecContainerOptions::builder()
        .cmd(vec!["python", "/opt/sample.py"])
        .attach_stdout(true)
        .attach_stderr(true)
        .build();

    while let Some(exec_result) = docker
        .containers()
        .get(&container_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!(),
    }
}

Output:

Stdout: 123
124
125
126

Stdout: 123
124
125
126

Stdout: 123
124
125
126

Stdout: 123
124
125
126

Stdout: 123
124
125
126

Stdout: 123
124
125
126

Stdout: 123
124
125
126

Stdout: 123
124
125
126

..... forever