Giter Club home page Giter Club logo

bollard's Introduction

crates.io license circle-ci appveyor docs

Bollard: an asynchronous rust client library for the docker API

Bollard leverages the latest Hyper and Tokio improvements for an asynchronous API containing futures, streams and the async/await paradigm.

The library also features Windows support through Named Pipes and HTTPS support through optional SSL bindings or a native TLS implementation.

Install

Add the following to your Cargo.toml file

[dependencies]
bollard = "0.7"

API

Documentation

API docs.

As of version 0.6, this project now generates API stubs from the upstream Docker-maintained Swagger OpenAPI specification. The generated models are committed to this repository, but packaged in a separate crate bollard-stubs.

Version

The Docker API is pegged at version 1.40

Usage

Connecting with the docker daemon

Connect to the docker server according to your architecture and security remit.

Unix socket

The client will connect to the standard unix socket location /var/run/docker.sock. Use the Docker::connect_with_unix method API to parameterise the interface.

use bollard::Docker;
#[cfg(unix)]
Docker::connect_with_unix_defaults();

Windows named pipe

The client will connect to the standard windows pipe location \\.\pipe\docker_engine. Use the Docker::connect_with_name_pipe method API to parameterise the interface.

use bollard::Docker;
#[cfg(windows)]
Docker::connect_with_named_pipe_defaults();

Local

The client will connect to the OS specific handler it is compiled for. This is a convenience for localhost environment that should run on multiple operating systems. Use the Docker::connect_with_local method API to parameterise the interface.

use bollard::Docker;
Docker::connect_with_local_defaults();

HTTP

The client will connect to the location pointed to by DOCKER_HOST environment variable, or localhost:2375 if missing. Use the Docker::connect_with_http method API to parameterise the interface.

use bollard::Docker;
Docker::connect_with_http_defaults();

SSL via openssl

Openssl is switched off by default, and can be enabled through the ssl cargo feature.

The client will connect to the location pointed to by DOCKER_HOST environment variable, or localhost:2375 if missing.

The location pointed to by the DOCKER_CERT_PATH environment variable is searched for certificates - key.pem for the private key, cert.pem for the server certificate and ca.pem for the certificate authority chain.

Use the Docker::connect_with_ssl method API to parameterise the interface.

use bollard::Docker;
#[cfg(feature = "ssl")]
Docker::connect_with_ssl_defaults();

TLS

Native TLS allows you to avoid the SSL bindings.

The client will connect to the location pointed to by DOCKER_HOST environment variable, or localhost:2375 if missing.

The location pointed to by the DOCKER_CERT_PATH environment variable is searched for certificates - identity.pfx for the PKCS #12 archive and ca.pem for the certificate authority chain.

Use the Docker::connect_with_ssl method API to parameterise the interface.

use bollard::Docker;
#[cfg(feature = "tls")]
Docker::connect_with_tls_defaults();

Examples

Note: all these examples need a Tokio Runtime. A small example about how to use Tokio is further below.

Version

First, check that the API is working with your server:

use bollard::Docker;

use futures_util::future::FutureExt;

// Use a connection function described above
// let docker = Docker::connect_...;
## let docker = Docker::connect_with_local_defaults().unwrap();

async move {
    let version = docker.version().await.unwrap();
    println!("{:?}", version);
};
```rust

### Listing images

To list docker images available on the Docker server:

```rust,no_run
use bollard::Docker;
use bollard::image::ListImagesOptions;

use futures_util::future::FutureExt;

use std::default::Default;

// Use a connection function described above
// let docker = Docker::connect_...;

async move {
    let images = &docker.list_images(Some(ListImagesOptions::<String> {
        all: true,
        ..Default::default()
    })).await.unwrap();

    for image in images {
        println!("-> {:?}", image);
    }
};

Streaming Stats

To receive a stream of stats for a running container.

use bollard::Docker;
use bollard::container::StatsOptions;

use futures_util::stream::TryStreamExt;

use std::default::Default;

// Use a connection function described above
// let docker = Docker::connect_...;

async move {
    let stats = &docker.stats("postgres", Some(StatsOptions {
       stream: true,
       ..Default::default()
    })).try_collect::<Vec<_>>().await.unwrap();

    for stat in stats {
        println!("{} - mem total: {:?} | mem usage: {:?}",
            stat.name,
            stat.memory_stats.max_usage,
            stat.memory_stats.usage);
    }
};

Examples

Further examples are available in the examples folder, or the integration/unit tests.

A Primer on the Tokio Runtime

In order to use the API effectively, you will need to be familiar with the Tokio Runtime.

Create a Tokio Runtime:

use tokio::runtime::Runtime;

let rt = Runtime::new().unwrap();

Subsequently, use the docker API:

// Use a connection function described above
// let docker = Docker::connect_...;
let future = async move {
    &docker.list_images(None::<ListImagesOptions<String>>).await;
};

Execute the future aynchronously:

rt.spawn(future);

Or, to execute and receive the result:

let result = rt.block_on(future);

Building the stubs

Serialization stubs are generated through the Swagger library. To generate these files, use the following in the codegen folder:

mvn -D org.slf4j.simpleLogger.defaultLogLevel=debug clean compiler:compile generate-resources

History

This library stems from the boondock rust library, which in turn originates from the rust-docker library, but most parts were rewritten to adobt the new functionality provided by tokio. Many thanks to the original authors for the initial code and inspiration.

Integration tests

Running the integration tests by default requires a running docker registry, with images tagged and pushed there. To disable this behaviour, set the DISABLE_REGISTRY environment variable.

docker run -d --restart always --name registry -p 5000:5000 registry:2
docker pull hello-world:linux
docker pull fussybeaver/uhttpd
docker pull alpine
docker tag hello-world:linux localhost:5000/hello-world:linux
docker tag fussybeaver/uhttpd localhost:5000/fussybeaver/uhttpd
docker tag alpine localhost:5000/alpine
docker push localhost:5000/hello-world:linux
docker push localhost:5000/fussybeaver/uhttpd
docker push localhost:5000/alpine
docker swarm init
REGISTRY_HTTP_ADDR=localhost:5000 cargo test -- --test-threads 1

License: Apache-2.0

bollard's People

Contributors

akshayknarayan avatar blkpark avatar bunogi avatar dieff avatar dudymas avatar edigaryev avatar emk avatar ethankhall avatar fanatid avatar fussybeaver avatar griff avatar innectic avatar ipoupaille avatar iyzana avatar jonhoo avatar khuey avatar mre avatar psftw avatar qwaz avatar simmsb avatar simonsparks avatar stephanbuys avatar tailhook avatar tobz avatar veeg avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.