Hit the Docker API in Rust

Docker provides a REST API. $ docker buildOr $ 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

  1. docker daemon
  2. REST API
  3. docker CLI It is a client & server application composed of. https://docs.docker.com/get-started/overview/#docker-engine

Docker official SDK (Go, Python) is provided, but I tried hitting it with Rust after studying Rust.

code

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 image.png When I run the program with, it returns like this. (Since it is displayed as a character string, the display is rough.) image.png

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.

reference

[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

Hit the Docker API in Rust
[Parse] Hit the API using callFunctionInBackground
Hit Zaim's API (OAuth 1.0) in Java
Parsing the COTOHA API in Java
[Swift] Hit the API using Decodable / Generics
Try using the Stream API in Java
Call the Windows Notification API in Java
Hit the Salesforce REST API from Java
Docker in LXD
How to check the logs in the Docker container
ChatWork4j for using the ChatWork API in Java
How Docker works ~ Implement the container in 60 lines
Try using the COTOHA API parsing in Java
I tried installing the Docker Integration plugin in IntelliJ
SSL in the local environment of Docker / Rails / puma
Copy and paste the file contents in Ubuntu's Docker container
Launch the Rails app locally in production mode (API Server)
Install by specifying the version of Django in the Docker environment
Setting the baseURL in the axios module of Docker environment Nuxt
Install docker and docker-compose on ubuntu in the shortest process
Display API definition in Swagger UI using Docker + Rails6 + apipie
I compared the build times of various Dockerfiles in Rust
[Docker] The story that an error occurred in docker-compose up
Install yarn in docker image
The API looks like this!
Understand the basics of docker
Install the plugin in Eclipse
Decomposing the Docker run command. .. ..
I tried the Docker tutorial!
npm error in docker tutorial
The languages that influenced Rust
JavaFX-Load Image in the background
Use MailHog for checking emails in the development environment (using Docker)
Use Docker and Keycloak to specify the access token and execute the API
The repository ... is not signed error in docker build apt-get update
Let's consider the meaning of "stream" and "collect" in Java's Stream API.
Sample code to call the Yahoo! Local Search API in Java
Leverage Either for individual exception handling in the Java Stream API