fussybeaver / bollard

Docker daemon API in Rust
Apache License 2.0
907 stars 134 forks source link

Generic strategy for health checking a container? #397

Open bpmooch opened 7 months ago

bpmooch commented 7 months ago
    let id = docker
        .create_container::<&str, &str>(None, mysql_config)
        .await
        .expect("failed to create mysql container")
        .id;
    docker
        .start_container::<String>(&id, None)
        .await
        .expect("failed to start mysql container");

    // FIXME: might have to wait a big longer for mysql than minio
    // FIXME: this is just not a solution for verifying a container works
    let wait_duration = 2_500;
    sleep(Duration::from_millis(wait_duration)).await;
    println!(
        "waited {wait_duration} milliseconds before returning from mysql container initialization"
    );

I am writing a test that spins up a mysql container at the start, runs some tests in sequence, then stops and removes the mysql container. I am having trouble reliably connecting to the container when I start it using bollard. If I were to add a HEALTHCHECK instruction to the dockerfile of the mysql image, could I use bollard to query that status? I guess I'm searching for a good pattern to deal with the problem generally

paul-hansen commented 7 months ago

You're probably looking for inspect_container

if let Ok(inspect_response) = docker.inspect_container("container_name", None).await {
    if let Ok(state) = inspect_response.state {
        println!("{:?}", state.health);
    }
}

(have not tried that code so expect typos, but should be close from looking at the docs)