3 releases
0.1.2 | Oct 5, 2021 |
---|---|
0.1.1 | Jun 25, 2021 |
0.1.0 | Jun 14, 2021 |
#29 in #synchronous
106 downloads per month
Used in scaphandre
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:
git clone https://github.com/bpetit/rs-docker-sync
cd rust-docker/devenv
./build_docker
(this assumes your user can run docker commands, otherwisesudo
)./run_docker -ti
- Already inside the container:
cd Code
cargo test
Dependencies
~11–20MB
~331K SLoC