Struct DockerClient

Source
pub struct DockerClient { /* private fields */ }
Expand description

DockerClient struct.

Implementations§

Source§

impl DockerClient

Source

pub fn connect<T>(path: T) -> DockerClient
where T: Into<String>,

Connect to docker

§Arguments
  • sock - connection address
§Examples
use docker_client::DockerClient;

fn main() {
    let client = DockerClient::connect("/var/run/docker.sock");
}
Source§

impl DockerClient

Source

pub fn create_container( &self, config: Config, ) -> Result<CreatedContainer, DockerError>

Create a container

§Arguments
  • Config is container to create.
§Examples
use docker_client::DockerClient;
use docker_client::container::Config;

fn main() {
    let client = DockerClient::connect("/var/run/docker.sock");

    let config = Config::with_image("alpine").name("test").build();
    match client.create_container(config) {
        Ok(_) => {},
        Err(_) => {}
    }
}
Source

pub fn get_fs_changes<T>(&self, id: T) -> Result<Vec<FSChanges>, DockerError>
where T: Into<String>,

Returns which files in a container’s filesystem have been added, deleted, or modified.

§Arguments
  • id - ID or name of the container.
§Examples
use docker_client::DockerClient;

fn main() {
    let client = DockerClient::connect("/var/run/docker.sock");

    let changes = client.get_fs_changes("test").unwrap_or(Vec::new());

    for change in &changes {
        println!("{:?}", change);
    }
}
Source

pub fn start_container<T>( &self, id: T, _detach_keys: T, ) -> Result<(), DockerError>
where T: Into<String>,

Start a container.

§Arguments
  • id - ID or name of the container.
  • detach_keys - The key sequence for detaching a container.
§Examples
use docker_client::{DockerClient, DockerError};

fn main() {
    let client = DockerClient::connect("/var/run/docker.sock");

    match client.start_container("test", "-d") {
        Ok(_) => {},
        Err(e) => {
            match e {
                DockerError::NotFound(e) => println!("{}", e.message),
                DockerError::ServerError(e) => println!("{}", e.message),
                _ => {}
            }
        },
    }

}
Source

pub fn stop_container<T>( &self, id: T, _wait: Option<i32>, ) -> Result<(), DockerError>
where T: Into<String>,

Stop a container.

§Arguments
  • id - ID or name of the container.
  • wait - Number of seconds to wait before killing the container.
§Examples
use docker_client::{DockerClient, DockerError};

fn main() {
    let client = DockerClient::connect("/var/run/docker.sock");

    match client.stop_container("test", Some(12)) {
        Ok(_) => {},
        Err(e) => {
            match e {
                DockerError::NotFound(e) => println!("{}", e.message),
                DockerError::ServerError(e) => println!("{}", e.message),
                _ => {}
            }
        },
    }

}
Source

pub fn pause_container<T>(&self, id: T) -> Result<(), DockerError>
where T: Into<String>,

Pause a container.

§Arguments
  • id - ID or name of the container.
§Examples
use docker_client::{DockerClient, DockerError};

fn main() {
    let client = DockerClient::connect("/var/run/docker.sock");

    match client.pause_container("test") {
        Ok(_) => {},
        Err(e) => {
            match e {
                DockerError::NotFound(e) => println!("{}", e.message),
                DockerError::ServerError(e) => println!("{}", e.message),
                _ => {}
            }
        },
    }

}
Source

pub fn unpause_container<T>(&self, id: T) -> Result<(), DockerError>
where T: Into<String>,

Unpause a container.

§Arguments
  • id - ID or name of the container.
§Examples
use docker_client::{DockerClient, DockerError};

fn main() {
    let client = DockerClient::connect("/var/run/docker.sock");

    match client.unpause_container("test") {
        Ok(_) => {},
        Err(e) => {
            match e {
                DockerError::NotFound(e) => println!("{}", e.message),
                DockerError::ServerError(e) => println!("{}", e.message),
                _ => {}
            }
        },
    }

}
Source

pub fn rename_container<T>(&self, id: T, new_name: T) -> Result<(), DockerError>
where T: Into<String>,

Rename a container.

§Arguments
  • id - ID or name of the container.
  • new_name - New name for the container.
§Examples
use docker_client::{DockerClient, DockerError};

fn main() {
    let client = DockerClient::connect("/var/run/docker.sock");

    match client.rename_container("test", "test1") {
        Ok(_) => {},
        Err(e) => {
            match e {
                DockerError::NotFound(e) => println!("{}", e.message),
                DockerError::ContainerExists(e) => println!("{}", e.message),
                DockerError::ServerError(e) => println!("{}", e.message),
                _ => {}
            }
        },
    }

}
Source

pub fn kill_container(&self, killer: Killer) -> Result<(), DockerError>

Kill a container.

§Arguments
  • killer is a struct with metadata to kill a container.
§Examples
use docker_client::{DockerClient, DockerError};
use docker_client::container::Killer;

fn main() {

    let client = DockerClient::connect("/var/run/docker.sock");

    let killer = Killer::new()
        .id("test")
        .build();

    match client.kill_container(killer) {
        Ok(_) => {}
        Err(e) => {
            match e {
                DockerError::NotFound(e) => println!("{}", e.message),
                DockerError::NotRunning(e) => println!("{}", e.message),
                DockerError::ServerError(e) => println!("{}", e.message),
                _ => {}
            }
        }
    }

}
Source

pub fn remove_container(&self, remover: Remover) -> Result<(), DockerError>

Remove a container.

§Arguments
  • remover is a struct with metadata to remove a container.
§Examples
use docker_client::{DockerClient, DockerError};
use docker_client::container::Remover;

fn main() {

    let client = DockerClient::connect("/var/run/docker.sock");

    let remover = Remover::new()
        .id("test")
        .with_remove_volumes(true)
        .build();

    match client.remove_container(remover) {
        Ok(_) => {}
        Err(e) => {
            match e {
                DockerError::BadParameters(e) => println!("{}", e.message),
                DockerError::NotFound(e) => println!("{}", e.message),
                DockerError::NotRunning(e) => println!("{}", e.message),
                DockerError::ServerError(e) => println!("{}", e.message),
                _ => {}
            }
        }
    }

}
Source

pub fn inspect_container<T>( &self, id: T, size: bool, ) -> Result<ContainerInfo, DockerError>
where T: Into<String>,

Inspect a container.

Return ContainerInfo structure about a container.

§Arguments
  • id - ID or name of the container.
  • size - Return the size of container as fields SizeRw and SizeRootFs.
§Examples
use docker_client::{DockerClient, DockerError};

fn main() {
    let client = DockerClient::connect("/var/run/docker.sock");

    match client.inspect_container("test", true) {
        Ok(s) => { println!("{:?}", s) },
        Err(e) => {},
    }

}
Source

pub fn get_container_log<T>(&self, id: T) -> Result<String, DockerError>
where T: Into<String>,

Get container logs

Get stdout and stderr logs from a container.

§Note

This endpoint works only for containers with the json-file or journald logging driver.

§Arguments

id - ID or name of the container.

§Examples
fn main() {
    let client = DockerClient::connect("/var/run/docker.sock");

    match client.get_container_log("test-container") {
        Ok(log) => { println!("Log: {}", log); }
        Err(e) => { println!("Error: {:?}", e); }
    }

}
Source

pub fn wait_container<T>( &self, id: T, condition: WaitCondition, ) -> Result<WaitStatus, DockerError>
where T: Into<String>,

Wait for a container

Block until a container stops, then returns the exit code.

§Arguments

id - ID or name of the container. condition - Wait until a container state reaches the given condition, either ‘not-running’ (default), ‘next-exit’, or ‘removed’.

§Examples
fn main() {
    let client = DockerClient::connect("/var/run/docker.sock");

    match client.wait_container("test-container", WaitCondition::NotRunning) {
        Ok(status) => { println!("Status: {:?}", status); }
        Err(e) => { println!("Error: {:?}", e); }
    }

}
Source

pub fn export_container<T>(&self, id: T) -> Result<(), DockerError>
where T: Into<String>,

Export a container

Return empty object or DockerError

§Arguments

id - ID or name of the container.

§Examples
fn main() {
    let client = DockerClient::connect("/var/run/docker.sock");

    match client.export_container("test-container") {
        Ok(_) => {},
        Err(e) => { println!("Error: {:?}", e); },
    }

}
Source

pub fn get_image_list(&self) -> Result<Vec<ShortImageInfo>, DockerError>

Get images list

Return vector of ShortImageInfo or DockerError

§Examples
fn main() {
    let client = DockerClient::connect("/var/run/docker.sock");

    match client.get_image_list() {
        Ok(list) => { println!("{:?}", list); },
        Err(e) => { println!("Error: {:?}", e); },
    }

}
Source

pub fn create_volume(&self, volume: VolumeCreator) -> Result<(), DockerError>

Create a volume

Return empty object or DockerError

§Arguments
  • volume - VolumeCreator struct.
§Examples
fn main() {
    let client = DockerClient::connect("/var/run/docker.sock");

    let creator = VolumeCreator::builder()
        .name("test")
        .build();

    match client.create_volume(creator) {
        Ok(_) => {},
        Err(e) => { println!("Error: {:?}", e); },
    }

}
Source

pub fn inspect_volume<T>(&self, name: T) -> Result<VolumeInfo, DockerError>
where T: Into<String>,

Inspect volume

Return VolumeInfo or DockerError

§Arguments
  • name - ID or name of the volume.
§Examples
fn main() {
    let client = DockerClient::connect("/var/run/docker.sock");

    match client.inspect_volume("test") {
        Ok(info) => { println!("{:?}", info); },
        Err(e) => { println!("Error: {:?}", e); },
    }

}
Source

pub fn remove_volume<T>(&self, name: T, force: bool) -> Result<(), DockerError>
where T: Into<String>,

Remove volume

Instruct the driver to remove the volume.

§Arguments
  • name - ID or name of the volume.
  • force - Force the removal of the volume.
§Examples
fn main() {
    let client = DockerClient::connect("/var/run/docker.sock");

    match client.remove_volume("test", false) {
        Ok(_) => {},
        Err(e) => { println!("Error: {:?}", e); },
    }

}
Source

pub fn delete_unused_volumes(&self) -> Result<DeletedInfo, DockerError>

Delete unused volumes

Return empty or DockerError

§Examples
fn main() {
    let client = DockerClient::connect("/var/run/docker.sock");

    match client.delete_unused_volumes() {
        Ok(_) => {},
        Err(e) => { println!("Error: {:?}", e); },
    }

}
Source

pub fn get_volumes_list(&self) -> Result<VolumesList, DockerError>

Get volumes list

Return VolumesList or DockerError

§Examples
fn main() {
    let client = DockerClient::connect("/var/run/docker.sock");

    match client.get_volumes_list() {
        Ok(list) => { println!("{:?}", list); },
        Err(e) => { println!("Error: {:?}", e); },
    }

}

Trait Implementations§

Source§

impl Debug for DockerClient

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.