Skip to content

Instantly share code, notes, and snippets.

Created October 23, 2015 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fnichol/21738b2d98e6c9ce8d4d to your computer and use it in GitHub Desktop.
Save fnichol/21738b2d98e6c9ce8d4d to your computer and use it in GitHub Desktop.
Research/prototype code to talk to a Docker Engine with Rust and Hyper
extern crate hyper;
extern crate openssl;
use hyper::{Client, Url};
use hyper::client::pool::{Config, Pool};
use hyper::http::h1::Http11Protocol;
use hyper::net::{HttpsConnector, Openssl};
use openssl::ssl::{SslContext, SslMethod};
use openssl::x509::X509FileType;
use std::env;
use std::io;
use std::io::Write;
use std::path::Path;
use std::sync::Arc;
macro_rules! println_stderr(
($($arg:tt)*) => (
match writeln!(&mut ::std::io::stderr(), $($arg)*) {
Ok(_) => {},
Err(x) => panic!("Unable to write to stderr: {}", x),
struct CertFiles {
ca_file: String,
private_key_file: String,
certificate_file: String,
fn make_tls_client(certs: CertFiles) -> Client {
let config = Config::default();
let mut ssl_context = SslContext::new(SslMethod::Sslv23).unwrap();
ssl_context.set_private_key_file(certs.private_key_file, X509FileType::PEM).unwrap();
ssl_context.set_certificate_file(certs.certificate_file, X509FileType::PEM).unwrap();
let connector = HttpsConnector::new(Openssl {
context: Arc::new(ssl_context)
let pool = Pool::with_connector(config, connector);
fn main() {
let mut docker_host = match env::var("DOCKER_HOST") {
Ok(val) => Url::parse(&val).unwrap(),
Err(e) => panic!("DOCKER_HOST must be set ({})", e)
docker_host.scheme = "https".to_owned();
let docker_cert_path = match env::var("DOCKER_CERT_PATH") {
Ok(val) => val,
Err(e) => panic!("DOCKER_CERT_PATH must be set ({})", e)
let cert_files = CertFiles {
ca_file: Path::new(&docker_cert_path).
private_key_file: Path::new(&docker_cert_path).
certificate_file: Path::new(&docker_cert_path).
let url = format!("{}/info", docker_host);
let client = make_tls_client(cert_files);
let mut res = client.get(&url).send().unwrap();
println_stderr!("URL: {}", res.url);
println_stderr!("Response: {}", res.status);
println_stderr!("Headers:\n{}", res.headers);
io::copy(&mut res, &mut io::stdout()).unwrap();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment