docker_engine_api 0.1.2

A simple way to interact with the Docker Engine API
Documentation

Docker Engine API Wrapper

A simple way to interact with the Docker Engine API

Getting Start

First you should to have Docker installed locally; then you should run library test with the commnad cargo test (the tests uses the alpine image: docker pull alpine); if everything goes ok, congratulations you can start to code.

Create a Client

A Client is the main way to connect to Docker Engine API, usually the socket is located in /var/run/docker.sock.

let mut client = Client::new("/var/run/docker.sock".to_string());

Fetch Containers

To fetch containers, provide you with methods to fetch multiple containers, the most primitive and unadapted part is the filters that should be provided as a string, anyways you can check the docs filters.

fn get_containers(&mut self, all: bool, limit: i32, size: bool, filters: String) -> Result<Vec<Container>, Box<dyn std::error::Error + Send + Sync>>

What are those arguments in the function? Check Docker Engine API documentation

match client.get_containers(true, 0, false, "".to_string()) {
    Ok(containers) => containers,
    Err(e) => panic!("Error: {}", e)
};

Create New Container

For the following example you've already nginx:latest image in your system. For know open a cmd and type docker image ls, otherwise you can edit the image and use what you want...

More Information

In a simple way:

let mut options = CreateContainerBody::default();
options.image = "alpine:latest".to_string();
options.cmd = vec!["/bin/true".to_string()];

let response = match client.create_container("test2", "linux", &options) {
    Ok(response) => response,
    Err(e) => panic!("Error: {}", e)
};

Max customization options:

let options = CreateContainerBody {
    hostname: "localhost".to_string(),
    domainname: "localhost".to_string(),
    user: "".to_string(),
    attach_stdin: true,
    attach_stdout: true,
    attach_stderr: true,
    tty: true,
    open_stdin: true,
    stdin_once: false,
    env: vec![],
    cmd: vec!["echo test'".to_string()],
    image: "alpine:latest".to_string(),
    labels: HashMap::new(),
    volumes: HashMap::new(),
    working_dir: "".to_string(),
    entrypoint: "".to_string(),
    network_disabled: false,
    mac_address: "".to_string(),
    stop_signal: "".to_string(),
    stop_timeout: 0,
    host_config: HostConfig::default(),
    networking_config: NetworkingConfig::default(),
    exposed_ports: HashMap::new(),
};

let response = match client.create_container("test", "linux", &options) {
    Ok(response) => response,
    Err(e) => panic!("Error: {}", e)
};

Get Stats

match client.get_stats_container(&response.id, true, false) {
    Ok(stats) => {
        println!("Container CPU Usage (this mean that the container is running): {:?}", stats.cpu_stats.cpu_usage.total_usage);
    },
    Err(e) => panic!("Error: {}", e)
};

Another Methods

fn inspect_container(&mut self, id: &str) -> Result<NoImplementedYet, Box<dyn std::error::Error + Send + Sync>>
fn start_container(&mut self, id: &str) -> Result<NoImplementedYet, Box<dyn std::error::Error + Send + Sync>>
fn stop_container(&mut self, id: &str, timeout: i32) -> Result<NoImplementedYet, Box<dyn std::error::Error + Send + Sync>>
fn restart_container(&mut self, id: &str) -> Result<NoImplementedYet, Box<dyn std::error::Error + Send + Sync>>
fn remove_container(&mut self, id: &str, remove_associated_volumes: bool, force: bool, remove_specified_linked: bool) -> Result<NoImplementedYet, Box<dyn std::error::Error + Send + Sync>>
fn get_container_logs(&mut self, id: &str) -> Result<GET_CONTAINER_LOGS_RETURN, Box<dyn std::error::Error + Send + Sync>>
fn list_processes(&mut self, id: &str) -> Result<LIST_PROCESSES_RETURN, Box<dyn std::error::Error + Send + Sync>>

Contributors

Would be a pleasure to get you here...