File: container-logs.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 (62 lines) | stat: -rw-r--r-- 1,835 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use std::collections::HashMap;
use std::iter::FromIterator;

use dockworker::{
    ContainerCreateOptions, ContainerHostConfig, ContainerLogOptions, Docker, LogConfig,
};
#[tokio::main]
async fn main() {
    let docker = Docker::connect_with_defaults().unwrap();

    let mut create = ContainerCreateOptions::new("alpine:latest");
    create.tty(true);
    create.entrypoint(vec!["/bin/ping".into(), "-c".into(), "5".into()]);
    create.cmd("localhost".to_string());
    create.host_config({
        let mut host = ContainerHostConfig::new();
        let log_config = LogConfig {
            config: HashMap::from_iter(
                vec![("tag".to_string(), "dockworker-test".to_string())].into_iter(),
            ),
            ..Default::default()
        };
        println!("logging with: {log_config:?}");
        host.log_config(log_config);
        host
    });

    let container = docker.create_container(None, &create).await.unwrap();
    docker.start_container(&container.id).await.unwrap();

    println!("Container to log: {}", &container.id);
    let log_options = ContainerLogOptions {
        stdout: true,
        stderr: true,
        follow: true,
        ..ContainerLogOptions::default()
    };

    let mut res = docker
        .log_container(&container.id, &log_options)
        .await
        .unwrap();

    use futures::stream::StreamExt;
    while let Some(line) = res.next().await {
        match line {
            Ok(line) => println!("read: {line}"),
            Err(e) => eprintln!("err: {e:?}"),
        }
    }
    println!(); // line break

    // already stopped
    // docker
    //     .stop_container(&container.id, Duration::from_secs(2))
    //     .await
    //     .unwrap();
    docker
        .remove_container(&container.id, None, Some(true), None)
        .await
        .unwrap();
}