#docker #同步 #简约 #本地 #分支 #套接字 #同步

docker-sync

本地Docker套接字的简约、同步、只读客户端

3个版本

0.1.2 2021年10月5日
0.1.1 2021年6月25日
0.1.0 2021年6月14日

#1093 in 网络编程

Download history 21/week @ 2024-03-11 26/week @ 2024-03-18 16/week @ 2024-03-25 96/week @ 2024-04-01 15/week @ 2024-04-08 18/week @ 2024-04-15 40/week @ 2024-04-22 9/week @ 2024-04-29 14/week @ 2024-05-06 12/week @ 2024-05-13 7/week @ 2024-05-20 44/week @ 2024-05-27 69/week @ 2024-06-03 56/week @ 2024-06-10 64/week @ 2024-06-17 46/week @ 2024-06-24

每月239次下载
用于scaphandre

Apache-2.0

54KB
1K SLoC

这是一个分支

这是一个从https://gitlab.com/kblobr/rust-docker (rs-docker on crate.io)分支出来的https://github.com/ghmlee/rust-docker (docker on crates.io)的分支。这两个仓库似乎都没有再维护。除了维护之外,分支的主要原因是因为Scaphandre需要一个同步库来与Docker套接字通信。

欢迎提交问题和PR。

Docker

本地Docker套接字的简约、同步、只读客户端。

文档可在此处找到:这里

快速开始

[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); }
    };
}

要求

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

示例

网络

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); }
    };
}

容器

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); }
    };
}

统计信息

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); }
    };
}

镜像

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); }
    };
}

信息

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); }
    };
}

进程

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); }
    };
}

文件系统更改

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); }
    };
}

导出容器

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 docker服务器

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); }
    };
}

显示docker版本信息

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)}
    };
}

贡献

为了有一个一致的开发环境,可以使用/devenv中的docker镜像,如下所示

  1. gitclone https://github.com/bpetit/rs-docker-sync
  2. cdrust-docker/devenv
  3. ./build_docker (这假设你的用户可以运行docker命令,否则 sudo)
  4. ./run_docker -ti
  5. 已经在容器内
  6. cd Code
  7. cargotest

依赖

~11–20MB
~330K SLoC