docker-sync

Crates.iodocker-sync
lib.rsdocker-sync
version0.1.2
sourcesrc
created_at2021-06-14 17:17:56.700574
updated_at2021-10-05 10:32:54.069812
descriptionMinimalistic, synchronous, read-only client for local Docker socket
homepagehttps://github.com/bpetit/rs-docker-sync
repositoryhttps://github.com/bpetit/rs-docker-sync
max_upload_size
id410013
size67,129
bpetit (bpetit)

documentation

https://docs.rs/docker-sync

README

This is a fork

This is a fork of https://gitlab.com/kblobr/rust-docker (rs-docker on crate.io) which itself is a fork from https://github.com/ghmlee/rust-docker (docker on crates.io). Both repositories seemed to be no longer be maintained. The main reason for this fork, besides the maintainance, is that Scaphandre needed a synchronous library to talk to Docker socket.

Issues and PRs welcome.

Docker

Minimalistic, synchronous, read-only client for local Docker socket.

Documentation is available here.

Quick start

[dependencies]
rs-docker-sync = "0.1.0"
extern crate docker_sync;

use docker_sync::Docker;

fn main() {
    let docker = match Docker::connect() { // we consider the local Docker socket by default, with the default path (/var/run/docker.sock), no need to precise the path
    	Ok(docker) => docker,
        Err(e) => { panic!("{}", e); }
    };
}

Requirements

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

Examples

Networks

extern crate docker_sync;

use docker_sync::Docker;

fn main() {
    let mut docker = match Docker::connect() {
    	Ok(docker) => docker,
        Err(e) => { panic!("{}", e); }
    };

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

Containers

extern crate docker_sync;

use docker_sync::Docker;

fn main() {
    let mut docker = match Docker::connect() {
    	Ok(docker) => docker,
        Err(e) => { panic!("{}", e); }
    };

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

Stats

extern crate docker_sync;

use docker_sync::Docker;

fn main() {
    let mut docker = match Docker::connect() {
    	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 docker_sync;

use docker_sync::Docker;

fn main() {
    let mut docker = match Docker::connect() {
    	Ok(docker) => docker,
        Err(e) => { panic!("{}", e); }
    };

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

Info

extern crate docker_sync;

use docker_sync::Docker;

fn main() {
    let mut docker = match Docker::connect() {
    	Ok(docker) => docker,
        Err(e) => { panic!("{}", e); }
    };

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

Processes

extern crate docker_sync;

use docker_sync::Docker;

fn main() {
    let mut docker = match Docker::connect() {
    	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 docker_sync;

use docker_sync::Docker;

fn main() {
    let mut docker = match Docker::connect() {
    	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 docker_sync;

use docker_sync::Docker;

fn main() {
    let mut docker = match Docker::connect() {
    	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); }
    };
}

Ping the docker server

extern crate docker_sync;

use docker_sync::Docker;

fn main() {
    let mut docker = match Docker::connect() {
    	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 docker_sync;

use docker_sync::Docker;

fn main() {
    let mut docker = match Docker::connect() {
    	Ok(docker) => docker,
        Err(e) => { panic!("{}", e); }
    };

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

Contributing

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

  1. git clone https://github.com/bpetit/rs-docker-sync
  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
Commit count: 191

cargo fmt