File: attach-container.rs

package info (click to toggle)
rust-dockworker 0.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 764 kB
  • sloc: makefile: 2
file content (37 lines) | stat: -rw-r--r-- 1,252 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use dockworker::{
    container::ContainerStdioType, ContainerCreateOptions, ContainerHostConfig, Docker,
};

#[tokio::main]
async fn main() {
    let docker = Docker::connect_with_defaults().unwrap();
    let mut host_config = ContainerHostConfig::new();
    host_config.auto_remove(true);
    let mut create = ContainerCreateOptions::new("hello-world:linux");
    create.host_config(host_config);

    let container = docker
        .create_container(Some("testing"), &create)
        .await
        .unwrap();
    docker.start_container(&container.id).await.unwrap();
    let mut res = docker
        .attach_container(&container.id, None, true, true, false, true, false)
        .await
        .unwrap();

    use futures::stream::StreamExt;
    while let Some(stdio) = res.next().await.transpose().unwrap() {
        match stdio.type_ {
            ContainerStdioType::Stdin => {
                println!("{}", String::from_utf8(stdio.frame).unwrap());
            }
            ContainerStdioType::Stdout => {
                println!("{}", String::from_utf8(stdio.frame).unwrap());
            }
            ContainerStdioType::Stderr => {
                println!("{}", String::from_utf8(stdio.frame).unwrap());
            }
        }
    }
}