19 releases

0.0.60 Jun 23, 2021
0.0.59 Jun 17, 2021
0.0.58 Nov 17, 2019
0.0.53 Sep 13, 2019
0.0.49 Jan 31, 2019

#679 in Development tools

Download history 40/week @ 2024-07-24 26/week @ 2024-07-31 1/week @ 2024-08-07 7/week @ 2024-08-21 16/week @ 2024-08-28 55/week @ 2024-09-04 36/week @ 2024-09-11 18/week @ 2024-09-18 26/week @ 2024-09-25 61/week @ 2024-10-02 115/week @ 2024-10-09 114/week @ 2024-10-16 36/week @ 2024-10-23 15/week @ 2024-10-30 6/week @ 2024-11-06

201 downloads per month

Apache-2.0

63KB
1.5K SLoC

This is a fork

This crate and repo were forked from https://github.com/ghmlee/rust-docker because the original repo seems to no longer be maintained.

With this in mind I will try to keep this crate as backwards compatible as possible.

Issues and PRs welcome.

Docker

Build Status

This is a Docker Remote API binding in Rust. Documentation is available here.

Quick start

[dependencies]
rs-docker = "0.0.58"
extern crate rs_docker;

use rs_docker::Docker;

fn main() {
    let docker = match Docker::connect("unix:///var/run/docker.sock") {
    	Ok(docker) => docker,
        Err(e) => { panic!("{}", e); }
    };
}

Debug

  • Rust (>= v1.4.0)
  • Docker (>= v1.5.0)

Examples

Networks

extern crate rs_docker;

use rs_docker::Docker;

fn main() {
    let mut docker = match Docker::connect("unix:///var/run/docker.sock") {
    	Ok(docker) => docker,
        Err(e) => { panic!("{}", e); }
    };

    let networks = match docker.get_networks() {
        Ok(networks) => networks,
        Err(e) => { panic!("{}", e); }
    };
}

Containers

extern crate rs_docker;

use rs_docker::Docker;

fn main() {
    let mut docker = match Docker::connect("unix:///var/run/docker.sock") {
    	Ok(docker) => docker,
        Err(e) => { panic!("{}", e); }
    };

    let containers = match docker.get_containers(false) {
        Ok(containers) => containers,
        Err(e) => { panic!("{}", e); }
    };
}

Stats

extern crate rs_docker;

use rs_docker::Docker;

fn main() {
    let mut docker = match Docker::connect("unix:///var/run/docker.sock") {
    	Ok(docker) => docker,
        Err(e) => { panic!("{}", e); }
    };

    let containers = match docker.get_containers(false) {
        Ok(containers) => containers,
        Err(e) => { panic!("{}", e); }
    };

    let stats = match docker.get_stats(&containers[0]) {
        Ok(stats) => stats,
        Err(e) => { panic!("{}", e); }
    };
}

Images

extern crate rs_docker;

use rs_docker::Docker;

fn main() {
    let mut docker = match Docker::connect("unix:///var/run/docker.sock") {
    	Ok(docker) => docker,
        Err(e) => { panic!("{}", e); }
    };

    let images = match docker.get_images(false) {
        Ok(images) => images,
        Err(e) => { panic!({}, e); }
    };
}

Info

extern crate rs_docker;

use rs_docker::Docker;

fn main() {
    let mut docker = match Docker::connect("unix:///var/run/docker.sock") {
    	Ok(docker) => docker,
        Err(e) => { panic!("{}", e); }
    };

    let info = match docker.get_system_info() {
        Ok(info) => info,
        Err(e) => { panic!("{}", e); }
    };
}

Processes

extern crate rs_docker;

use rs_docker::Docker;

fn main() {
    let mut docker = match Docker::connect("unix:///var/run/docker.sock") {
    	Ok(docker) => docker,
        Err(e) => { panic!("{}", e); }
    };

    let containers = match docker.get_containers(false) {
        Ok(containers) => containers,
        Err(e) => { panic!("{}", e); }
    };

    let processes = match docker.get_processes(&containers[0]) {
        Ok(processes) => processes,
        Err(e) => { panic!("{}", e); }
    };
}

Filesystem changes

extern crate rs_docker;

use rs_docker::Docker;

fn main() {
    let mut docker = match Docker::connect("unix:///var/run/docker.sock") {
    	Ok(docker) => docker,
        Err(e) => { panic!("{}", e); }
    };

    let containers = match docker.get_containers(false) {
        Ok(containers) => containers,
        Err(e) => { panic!("{}", e); }
    };

    let changes = match docker.get_filesystem_changes(&containers[0]) {
        Ok(changes) => changes,
        Err(e) => { panic!("{}", e); }
    };
}

Export a container

extern crate rs_docker;

use rs_docker::Docker;

fn main() {
    let mut docker = match Docker::connect("unix:///var/run/docker.sock") {
    	Ok(docker) => docker,
        Err(e) => { panic!("{}", e); }
    };

    let containers = match docker.get_containers(false) {
        Ok(containers) => containers,
        Err(e) => { panic!("{}", e); }
    };

    let bytes = match docker.export_container(&containers[0]) {
        Ok(bytes) => bytes,
        Err(e) => { panic!("{}", e); }
    };
}

Create an image

extern crate rs_docker;

use rs_docker::Docker;

fn main() {
    let mut docker = match Docker::connect("unix:///var/run/docker.sock") {
    	Ok(docker) => docker,
        Err(e) => { panic!("{}", e); }
    };

    let image = "debian".to_string();
    let tag = "latest".to_string();
    
    let statuses = match docker.create_image(image, tag) {
        Ok(statuses) => statuses,
        Err(e) => { panic!("{}", e); }
    };
    
    match statuses.last() {
        Some(last) => {
            println!("{}", last.clone().status.unwrap());
        }
        None => { println!("none"); }
    }
}

Ping the docker server

extern crate rs_docker;

use rs_docker::Docker;

fn main() {
    let mut docker = match Docker::connect("unix:///var/run/docker.sock") {
    	Ok(docker) => docker,
        Err(e) => { panic!("{}", e); }
    };
    
    let ping = match docker.ping() {
        Ok(ping) => ping,
        Err(e) => { panic!("{}", e); }
    };
}

Show the docker version information

extern crate rs_docker;

use rs_docker::Docker;

fn main() {
    let mut docker = match Docker::connect("unix:///var/run/docker.sock") {
    	Ok(docker) => docker,
        Err(e) => { panic!("{}", e); }
    };
    
    let version = match docker.get_version() {
        Ok(version) => version,
        Err(e) => {panic!("{}",e)}
    };
}

Docker Toolbox

By default, Docker Toolbox runs docker with TLS enabled. It auto-generates certificates. The docker-machine will copy them to ~/.docker/machine/certs on the host machine once the VM has started.

Example

extern crate rs_docker;

use rs_docker::Docker;
use std::path::Path;

fn main() {
    let key = Path::new("/Users/<username>/.docker/machine/certs/key.pem");
    let cert = Path::new("/Users/<username>/.docker/machine/certs/cert.pem");
    let ca = Path::new("/Users/<username>/.docker/machine/certs/ca.pem");

    let mut docker = match Docker::connect("tcp://192.168.99.100:2376") {
    	Ok(docker) => docker,
        Err(e) => { panic!("{}", e); }
    };
    docker.set_tls(&key, &cert, &ca).unwrap();
}

Contributing

To have a consistent dev environment one can use the docker image in /devenv like so:

  1. git clone https://gitlab.com/kblobr/rust-docker
  2. cd rust-docker/devenv
  3. ./build_docker (this assumes your user can run docker commands, otherwise sudo)
  4. ./run_docker -ti
  5. Already inside the container:
  6. cd Code
  7. cargo test

For changes:

  1. Fork it
  2. Create your a new remote upstream repository (git remote add upstream https://gitlab.com/kblobr/rust-docker)
  3. Commit your changes (git commit -m 'Adds some feature')
  4. Push to the branch (git push origin your-branch)
  5. Create new Pull Request

Dependencies

~5–14MB
~173K SLoC