Struct bollard::Docker

source ·
pub struct Docker<C> { /* private fields */ }
Expand description

Docker

The main interface for calling the Docker API. Construct a new Docker instance using one of the connect methods:

Implementations§


List Containers

Returns a list of containers.

Arguments
Returns
Examples
use bollard::container::{ListContainersOptions};

use std::collections::HashMap;
use std::default::Default;

let mut filters = HashMap::new();
filters.insert("health", vec!("unhealthy"));

let options = Some(ListContainersOptions{
    all: true,
    filters: filters,
    ..Default::default()
});

docker.list_containers(options);

Create Container

Prepares a container for a subsequent start operation.

Arguments
Returns
Examples
use bollard::container::{CreateContainerOptions, Config};

use std::default::Default;

let options = Some(CreateContainerOptions{
    name: "my-new-container",
});

let config = Config {
    image: Some("hello-world"),
    cmd: Some(vec!["/hello"]),
    ..Default::default()
};

docker.create_container(options, config);

Start Container

Starts a container, after preparing it with the Create Container API.

Arguments
Returns
  • unit type (), wrapped in a Future.
Examples
use bollard::container::StartContainerOptions;

docker.start_container("hello-world", None::<StartContainerOptions<String>>);

Stop Container

Stops a container.

Arguments
Returns
  • unit type (), wrapped in a Future.
Examples
use bollard::container::StopContainerOptions;

let options = Some(StopContainerOptions{
    t: 30,
});

docker.stop_container("hello-world", options);

Remove Container

Remove a container.

Arguments
Returns
  • unit type (), wrapped in a Future.
Examples

use bollard::container::RemoveContainerOptions;

use std::default::Default;

let options = Some(RemoveContainerOptions{
    force: true,
    ..Default::default()
});

docker.remove_container("hello-world", options);

Wait Container

Wait for a container to stop. This is a non-blocking operation, the resulting stream will end when the container stops.

Arguments
Returns
Examples

use bollard::container::WaitContainerOptions;

let options = Some(WaitContainerOptions{
    condition: "not-running",
});

docker.wait_container("hello-world", options);

Restart Container

Restart a container.

Arguments
Returns
  • unit type (), wrapped in a Future.
Examples

use bollard::container::RestartContainerOptions;

let options = Some(RestartContainerOptions{
    t: 30,
});

docker.restart_container("postgres", options);

Inspect Container

Inspect a container.

Arguments
Returns
Examples
use bollard::container::InspectContainerOptions;

let options = Some(InspectContainerOptions{
    size: false,
});

docker.inspect_container("hello-world", options);

Top Processes

List processes running inside a container.

Arguments
  • Container name as string slice.
  • Optional Top Options struct.
Returns
Examples
use bollard::container::TopOptions;

let options = Some(TopOptions{
    ps_args: "aux",
});

docker.top_processes("fnichol/uhttpd", options);

Logs

Get container logs.

Arguments
  • Container name as string slice.
  • Optional Logs Options struct.
Returns
Examples

use bollard::container::LogsOptions;

use std::default::Default;

let options = Some(LogsOptions{
    stdout: true,
    ..Default::default()
});

docker.logs("hello-world", options);

Container Changes

Get changes on a container’s filesystem.

Arguments
  • Container name as string slice.
Returns
  • An Option of Vector of Change structs, wrapped in a Future.
Examples

docker.container_changes("hello-world");

Stats

Get container stats based on resource usage.

Arguments
Returns
  • Stats struct, wrapped in a Stream.
Examples

use bollard::container::StatsOptions;

let options = Some(StatsOptions{
    stream: false,
});

docker.stats("hello-world", options);

Kill Container

Kill a container.

Arguments
Returns
  • unit type (), wrapped in a Future.
Examples

use bollard::container::KillContainerOptions;

let options = Some(KillContainerOptions{
    signal: "SIGINT",
});

docker.kill_container("postgres", options);

Update Container

Update a container.

Arguments
Returns
  • unit type (), wrapped in a Future.
Examples

use bollard::container::UpdateContainerOptions;
use std::default::Default;

let config = UpdateContainerOptions {
    memory: Some(314572800),
    memory_swap: Some(314572800),
    ..Default::default()
};

docker.update_container("postgres", config);

Rename Container

Rename a container.

Arguments
Returns
  • unit type (), wrapped in a Future.
Examples

use bollard::container::RenameContainerOptions;

let required = RenameContainerOptions {
    name: "my_new_container_name"
};

docker.rename_container("hello-world", required);

Pause Container

Use the cgroups freezer to suspend all processes in a container.

Arguments
  • Container name as a string slice.
Returns
  • unit type (), wrapped in a Future.
Examples

docker.pause_container("postgres");

Unpause Container

Resume a container which has been paused.

Arguments
  • Container name as a string slice.
Returns
  • unit type (), wrapped in a Future.
Examples

docker.unpause_container("postgres");

Prune Containers

Delete stopped containers.

Arguments
Returns
Examples
use bollard::container::PruneContainersOptions;

use std::collections::HashMap;

let mut filters = HashMap::new();
filters.insert("until", vec!("10m"));

let options = Some(PruneContainersOptions{
    filters: filters
});

docker.prune_containers(options);

A Docker implementation typed to connect to an unsecure Http connection.

Connect using unsecured HTTP using defaults that are signalled by environment variables.

Defaults
  • The connection url is sourced from the DOCKER_HOST environment variable, and defaults to localhost:2375.
  • The number of threads used for the HTTP connection pool defaults to 1.
  • The request timeout defaults to 2 minutes.
Examples
use bollard::Docker;

use futures::future::Future;

let connection = Docker::connect_with_http_defaults().unwrap();
connection.ping()
  .and_then(|_| Ok(println!("Connected!")));

Connect using unsecured HTTP.

Arguments
  • addr: connection url including scheme and port.
  • num_threads: the number of threads for the HTTP connection pool.
  • timeout: the read/write timeout (seconds) to use for every hyper connection
Examples
use bollard::Docker;

use futures::future::Future;

let connection = Docker::connect_with_http(
                   "http://my-custom-docker-server:2735", 4, 20)
                   .unwrap();
connection.ping()
  .and_then(|_| Ok(println!("Connected!")));

A Docker implementation typed to connect to a Windows Named Pipe, exclusive to the windows target.

Connect using a Windows Named Pipe using defaults that are signalled by environment variables.

Defaults
  • The socket location defaults to //./pipe/docker_engine.
  • The request timeout defaults to 2 minutes.
Examples
use bollard::Docker;

use futures::future::Future;

let connection = Docker::connect_with_named_pipe_defaults().unwrap();
connection.ping().and_then(|_| Ok(println!("Connected!")));

Connect using a Windows Named Pipe.

Arguments
  • addr: socket location.
  • timeout: the read/write timeout (seconds) to use for every hyper connection
Examples
use bollard::Docker;

use futures::future::Future;


let connection = Docker::connect_with_named_pipe(
    "//./pipe/docker_engine", 120).unwrap();
connection.ping().and_then(|_| Ok(println!("Connected!")));

A Docker implementation typed to connect to a secure HTTPS connection, using the native rust TLS library.

Connect using secure HTTPS using native TLS and defaults that are signalled by environment variables.

Defaults
  • The connection url is sourced from the DOCKER_HOST environment variable.
  • The certificate directory is sourced from the DOCKER_CERT_PATH environment variable.
  • Certificate PKCS #12 archive is named identity.pfx and the certificate chain is named ca.pem.
  • The password for the PKCS #12 archive defaults to an empty password.
  • The number of threads used for the HTTP connection pool defaults to 1.
  • The request timeout defaults to 2 minutes.
PKCS12

PKCS #12 archives can be created with OpenSSL:

openssl pkcs12 -export -out identity.pfx -inkey key.pem -in cert.pem -certfile
chain_certs.pem
Examples
use bollard::Docker;

use futures::future::Future;

let connection = Docker::connect_with_tls_defaults().unwrap();
connection.ping()
  .and_then(|_| Ok(println!("Connected!")));

Connect using secure HTTPS using native TLS.

Arguments
  • addr: the connection url.
  • pkcs12_file: the PKCS #12 archive.
  • ca_file: the certificate chain.
  • pkcs12_password: the password to the PKCS #12 archive.
  • num_threads: the number of threads for the HTTP connection pool.
  • timeout: the read/write timeout (seconds) to use for every hyper connection
PKCS12

PKCS #12 archives can be created with OpenSSL:

openssl pkcs12 -export -out identity.pfx -inkey key.pem -in cert.pem -certfile
chain_certs.pem
Examples
use bollard::Docker;

use std::path::Path;

use futures::future::Future;

let connection = Docker::connect_with_tls(
    "localhost:2375",
    Path::new("/certs/identity.pfx"),
    Path::new("/certs/ca.pem"),
    "my_secret_password",
    1,
    120
).unwrap();
connection.ping()
  .and_then(|_| Ok(println!("Connected!")));

Connect using a type that implements hyper::Connect.

Arguments
  • connector: type that implements hyper::Connect.
  • client_addr: location to connect to.
  • timeout: the read/write timeout (seconds) to use for every hyper connection
Examples
use bollard::Docker;

use futures::future::Future;

let mut connector = SequentialConnector::default();
connector.content.push(
  "HTTP/1.1 200 OK\r\nServer: mock1\r\nContent-Type: application/json\r\nContent-Length: 0\r\n\r\n".to_string()
);
let connection = Docker::connect_with(connector, String::new(), 5).unwrap();
connection.ping()
  .and_then(|_| Ok(println!("Connected!")));

Create a chain of docker commands, useful to calling the API in a sequential manner.

Examples
use bollard::Docker;
let docker = Docker::connect_with_http_defaults().unwrap();
docker.chain();

List Images

Returns a list of images on the server. Note that it uses a different, smaller representation of an image than inspecting a single image

Arguments
Returns
Examples
use bollard::image::ListImagesOptions;

use std::collections::HashMap;
use std::default::Default;

let mut filters = HashMap::new();
filters.insert("dangling", "true");

let options = Some(ListImagesOptions{
  all: true,
  filters: filters,
  ..Default::default()
});

docker.list_images(options);

Create Image

Create an image by either pulling it from a registry or importing it.

Arguments
Returns
Examples
use bollard::image::CreateImageOptions;

use std::default::Default;

let options = Some(CreateImageOptions{
  from_image: "hello-world",
  ..Default::default()
});

docker.create_image(options);

// do some other work while the image is pulled from the docker hub...
Unsupported
  • Import from tarball

Inspect Image

Return low-level information about an image.

Arguments
  • Image name as a string slice.
Returns
  • Image, wrapped in a Future.
Examples

use std::default::Default;

docker.inspect_image("hello-world");

Prune Images

Delete unused images.

Arguments
Returns
Examples
use bollard::image::PruneImagesOptions;

use std::collections::HashMap;

let mut filters = HashMap::new();
filters.insert("until", "10m");

let options = Some(PruneImagesOptions {
  filters: filters
});

docker.prune_images(options);

Image History

Return parent layers of an image.

Arguments
  • Image name as a string slice.
Returns
Examples

docker.image_history("hello-world");

Search Images

Search for an image on Docker Hub.

Arguments
Returns
Examples

use bollard::image::SearchImagesOptions;
use std::default::Default;
use std::collections::HashMap;

let mut filters = HashMap::new();
filters.insert("until", "10m");

let search_options = SearchImagesOptions {
    term: "hello-world",
    filters: filters,
    ..Default::default()
};

docker.search_images(search_options);

Remove Image

Remove an image, along with any untagged parent images that were referenced by that image.

Arguments
Returns
Examples

use bollard::image::RemoveImageOptions;
use std::default::Default;

let remove_options = Some(RemoveImageOptions {
    force: true,
    ..Default::default()
});

docker.remove_image("hello-world", remove_options);

Tag Image

Tag an image so that it becomes part of a repository.

Arguments
Returns
  • unit type (), wrapped in a Future.
Examples

use bollard::image::TagImageOptions;
use std::default::Default;

let tag_options = Some(TagImageOptions {
    tag: "v1.0.1",
    ..Default::default()
});

docker.tag_image("hello-world", tag_options);

Push Image

Push an image to a registry.

Arguments
Returns
  • unit type (), wrapped in a Future.
Examples

use bollard::auth::DockerCredentials;
use bollard::image::PushImageOptions;

let push_options = Some(PushImageOptions {
    tag: "v1.0.1",
});

let credentials = Some(DockerCredentials {
    username: Some("Jack".to_string()),
    password: Some("myverysecretpassword".to_string()),
    email: Some("jack.smith@example.com".to_string()),
    serveraddress: Some("localhost:5000".to_string())
});

docker.push_image("hello-world", push_options, credentials);

Commit Container

Create a new image from a container.

Arguments
Returns
Examples
use bollard::image::CommitContainerOptions;
use bollard::container::Config;

use std::default::Default;

let options = CommitContainerOptions{
    container: "my-running-container",
    pause: true,
    ..Default::default()
};

let config = Config::<String> {
    ..Default::default()
};

docker.commit_container(options, config);

Version

Returns the version of Docker that is running and various information about the system that Docker is running on.

Returns
Examples
docker.version();

Ping

This is a dummy endpoint you can use to test if the server is accessible.

Returns
  • A String, wrapped in a Future.
Examples

docker.ping();

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.