Docker provides a REST API.
$ docker build
Or
$ docker run
For command operations such as, internally, commands to docker deamon are performed via the REST API.
It can be hit directly with curl or a program as well as the CLI.
Docker Engine
Docker official SDK (Go, Python) is provided, but I tried hitting it with Rust after studying Rust.
main.rs
use std::error::Error;
use std::path::Path;
use futures_util::stream::TryStreamExt;
use hyper::{Client};
use hyperlocal::{Uri, UnixClientExt};
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let path = Path::new("/var/run/docker.sock");
//API version checks the version of docker installed locally
let url = Uri::new(path, "/v1.40/containers/json");
let client = Client::unix();
let response_body = client.get(url.into()).await?.into_body();
let bytes = response_body
.try_fold(Vec::default(), |mut buf, bytes| async {
buf.extend(bytes);
Ok(buf)
})
.await?;
println!("{}", String::from_utf8(bytes)?);
Ok(())
}
The code is almost quoted from README of hyperlocal repository, but it didn't work as it is, so I modified it partially.
The status of the following containers When I run the program with, it returns like this. (Since it is displayed as a character string, the display is rough.)
hyperlocal The docker daemon is set to listen to UNIX domain socket (/var/run/docker.sock) by default, and realizes not only Hyper (Rust's HTTP client & server library) but also unix socket communication hyperlocal had to be used.
[Official Doc] https://docs.docker.com/get-started/overview/#docker-engine https://docs.docker.com/get-started/overview/#docker-architecture Introduction to Docker to touch and understand
Recommended Posts