fussybeaver / bollard

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

Add a `Docker::connect_with_url()` method #395

Closed xfbs closed 2 months ago

xfbs commented 8 months ago

Could we have a connect_with_url() method? I'm thinking to basically move use what you are doing in connect_with_defaults, and then call this method from connect with defaults().

    pub fn connect_with_defaults() -> Result<Docker, Error> {
        let host = env::var("DOCKER_HOST").unwrap_or_else(|_| DEFAULT_DOCKER_HOST.to_string());
        Self::connect_with_url(host)
    }

    pub fn connect_with_url(host: &str) -> Result<Docker, Error> {
        match host {
            #[cfg(unix)]
            h if h.starts_with("unix://") => {
                Docker::connect_with_unix(&h, DEFAULT_TIMEOUT, API_DEFAULT_VERSION)
            }
            #[cfg(windows)]
            h if h.starts_with("npipe://") => {
                Docker::connect_with_named_pipe(&h, DEFAULT_TIMEOUT, API_DEFAULT_VERSION)
            }
            h if h.starts_with("tcp://") || h.starts_with("http://") => {
                #[cfg(feature = "ssl")]
                if env::var("DOCKER_TLS_VERIFY").is_ok() {
                    return Docker::connect_with_ssl_defaults();
                }
                Docker::connect_with_http_defaults()
            }
            #[cfg(feature = "ssl")]
            h if h.starts_with("https://") => Docker::connect_with_ssl_defaults(),
            _ => Err(UnsupportedURISchemeError {
                uri: host.to_string(),
            }),
        }
    }
fussybeaver commented 8 months ago

We recently added a connect_with_defaults method which sources that DOCKER_HOST environment variable https://github.com/fussybeaver/bollard/blob/4d803568ea94abb6a2214c85ebb13dc4a065058c/src/docker.rs#L656 . I'd be happy for a PR that extends or refactors this method to accept a connect_with_url invocation

eddumelendez commented 8 months ago

Hi, jumping here because I'm also interested in this issue. It would be nice to allow setting headers as well to set custom User-Agent from clients consuming the library. It would be even much better to accept a http request to make the client fully configurable.

bpmooch commented 5 months ago

@eddumelendez do you still care?

DDtKey commented 4 months ago

Hi @bpmooch 👋

I'll put some context: we use bollard client in Testcontainers for Rust . And recently we completely switched to the bollard client, there is no issues with it and we don't depend on this issue. It was customized and current interface is sufficient for this.

But the ability to customize the request still looks interesting, at least we would like to add the header (User-Agent). However, IMO, this is question for a separate issue.

UPD: extracted into separate issue https://github.com/fussybeaver/bollard/issues/436

fussybeaver commented 2 months ago

@DDtKey @xfbs @bpmooch @eddumelendez There's an open PR tackling this issue, happy if any of you could have a quick look to check this will satisfy your needs https://github.com/fussybeaver/bollard/pull/459

fussybeaver commented 2 months ago

Closed due to a flexible implementation done in #459