#docker #synchronous #local #sockets #client #minimalist #fork

docker-sync

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

3 releases

0.1.2 Oct 5, 2021
0.1.1 Jun 25, 2021
0.1.0 Jun 14, 2021

#29 in #synchronous

Download history 27/week @ 2024-07-22 67/week @ 2024-07-29 4/week @ 2024-08-05 4/week @ 2024-08-12 81/week @ 2024-08-19 14/week @ 2024-08-26 7/week @ 2024-09-02 35/week @ 2024-09-09 26/week @ 2024-09-16 73/week @ 2024-09-23 20/week @ 2024-09-30 25/week @ 2024-10-07 24/week @ 2024-10-14 29/week @ 2024-10-21 38/week @ 2024-10-28 31/week @ 2024-11-04

124 downloads per month
Used in scaphandre

Apache-2.0

54KB
1K SLoC

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

Dependencies

~13–22MB
~337K SLoC