Bollard: an asynchronous rust client library for the docker API
Bollard leverages the latest Hyper and Tokio improvements for an asynchronous API containing futures and streams.
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.1"
API documentation
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 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 Docker;
connect_with_named_pipe_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 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 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 Docker;
connect_with_tls_defaults;
Examples
Note: all these examples need a Tokio Runtime. A small example about how to use Tokio is below.
Version
First, check that the API is working with your server:
use Future;
use Docker;
// Use a connection function described above
// let docker = Docker::connect_...;
docker.version
.map;
Listing images
To list docker images available on the Docker server:
use Future;
use Docker;
use ListImagesOptions;
use Default;
// Use a connection function described above
// let docker = Docker::connect_...;
docker.list_images
.map;
Streaming Stats
To receive a stream of stats for a running container.
use Stream;
use Docker;
use StatsOptions;
use Default;
// Use a connection function described above
// let docker = Docker::connect_...;
docker.stats
.map;
Chaining docker commands
It's sometimes more convenient to chain a string of Docker API calls. The DockerChain
API
will return an instance of itself in the return call.
use Docker;
use CreateImageOptions;
use CreateContainerOptions;
use Config;
use Future;
use Default;
// Use a connection function described above
// let docker = Docker::connect_...;
docker.chain.create_image.and_then;
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 Runtime;
let mut rt = new.unwrap;
Subsequently, use the docker API:
// Use a connection function described above
// let docker = Docker::connect_...;
let future = docker.list_images;
Execute the future aynchronously:
rt.spawn;
Or, to execute and receive the result:
let result = rt.block_on;
Finally, to shut down the executor:
rt.shutdown_now.wait.unwrap;
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.
REGISTRY_HTTP_ADDR=localhost:5000
License: Apache-2.0