bollard icon indicating copy to clipboard operation
bollard copied to clipboard

question: How to create hyper::Response from create image stream?

Open chanble opened this issue 3 years ago • 2 comments

I write like this:

let mut stream = docker_client.create_image(Some(CreateImageOptions{
    from_image: "hello-world",
    ..Default::default()
}), None, None);
HyperResponse::new(HyperBody::wrap_stream(stream))

error:

 the trait `From<CreateImageInfo>` is not implemented for `hyper::body::Bytes`
    |
    = help: the following implementations were found:
              <hyper::body::Bytes as From<&'static [u8]>>
              <hyper::body::Bytes as From<&'static str>>
              <hyper::body::Bytes as From<Box<[u8]>>>
              <hyper::body::Bytes as From<String>>
            and 5 others
    = note: required because of the requirements on the impl of `Into<hyper::body::Bytes>` for `CreateImageInfo`
note: required by a bound in `hyper::Body::wrap_stream`

chanble avatar Jan 07 '22 05:01 chanble

The create_image endpoint deserializes the docker payload into a stream of CreateImageInfo. If you wanted to recreate a new HyperResponse, you would need to serialize each instance back into bytes or text and create a new Hyper::Body out of that. Does that help ?

fussybeaver avatar Jan 11 '22 18:01 fussybeaver

Thanks, It works like this.

let stream = docker_client.create_image(create_image_option, None, None);
let stream2 = stream.map(|create_result| match create_result {
    Ok(create_image_info) => {
        println!("create_image_info: {:?}", create_image_info);
        serde_json::to_string(&create_image_info).map_err(BollardError::from)
    }
    Err(err) => {
        println!("create_image_info err: {:?}", err);
        Err(err)
    }
});
HyperResponse::new(HyperBody::wrap_stream(stream2))

chanble avatar Jan 12 '22 15:01 chanble