Skip to content

Docker API coverage #11

@hrbrmstr

Description

@hrbrmstr

Moving the discussion started in #10 to here. Some examples of using the Unix socket API using curl:

library(curl)
library(jsonlite)
library(openssl)

docker_auth <- function(username=Sys.getenv("DOCKER_USERNAME"),
                        password=Sys.getenv("DOCKER_PASSWORD"),
                        email=Sys.getenv("DOCKER_EMAIL"),
                        server_address=Sys.getenv("DOCKER_REGISTRY"),
                        encode=TRUE) {

  toJSON(
    list(
      username=unbox(username),
      password=unbox(password),
      email=unbox(email),
      auth=unbox(""),
      serveraddress=unbox(server_address)
    )
  ) -> auth_json

  if (encode) auth_json <- openssl::base64_encode(auth_json)

  auth_json

}

docker_handle <- function(verbose=FALSE) {
  h <- curl::new_handle()
  h <- curl::handle_reset(h)
  h <- curl::handle_setopt(h, UNIX_SOCKET_PATH = "/var/run/docker.sock")
  h <- curl::handle_setopt(h, VERBOSE = verbose)
  h <- curl::handle_setheaders(h,
                               `Content-Type` = "application/json",
                               `X-Registry-Auth` = docker_auth())
  h
}

# Auth check ------------------------------------------------------------------------
h <- docker_handle(TRUE)
handle_setopt(h, copypostfields = docker_auth(encode=FALSE))
res <- curl_fetch_memory(url = "http://v1.26/auth", handle = h)
str(fromJSON(rawToChar(res$content)))

# Docker info -----------------------------------------------------------------------
h <- docker_handle()
res <- curl_fetch_memory(url = "http://v1.26/info", handle = h)
str(fromJSON(rawToChar(res$content)))

# Docker version --------------------------------------------------------------------
h <- docker_handle()
res <- curl_fetch_memory(url = "http://v1.26/version", handle = h)
str(fromJSON(rawToChar(res$content)))

# Docker ping -----------------------------------------------------------------------
h <- docker_handle()
res <- curl_fetch_memory(url = "http://v1.26/_ping", handle = h)
cat(rawToChar(res$content))

# Data usage ------------------------------------------------------------------------
h <- docker_handle()
res <- curl_fetch_memory(url = "http://v1.26/system/df", handle = h)
str(fromJSON(rawToChar(res$content)))

# List images -----------------------------------------------------------------------
h <- docker_handle()
res <- curl_fetch_memory(url = "http://v1.26/images/json", handle = h)
str(fromJSON(rawToChar(res$content)))

# Search images ---------------------------------------------------------------------
h <- docker_handle()
res <- curl_fetch_memory(url = "http://v1.26/images/search?term=alpine&limit=10", handle = h)
str(fromJSON(rawToChar(res$content)))

# Pull an image ---------------------------------------------------
h <- docker_handle(verbose = TRUE)
handle_setopt(h, copypostfields = '')
res <- curl_fetch_memory(url = "http://v1.26/images/create?fromImage=alpine&tag=latest", handle = h)
cat(rawToChar(res$content))

# List running containers -----------------------------------------------------------
h <- docker_handle()
res <- curl_fetch_memory(url = "http://v1.26/containers/json", handle = h)
str(fromJSON(rawToChar(res$content)))

# all: true/false
# limit: int
# size: true/false
# filters: json
h <- docker_handle()
res <- curl_fetch_memory(url = "http://v1.26/containers/json", handle = h)
str(fromJSON(rawToChar(res$content)))

# Inspect a container ---------------------------------------------------------------

# size: true/false
h <- docker_handle(verbose = TRUE)
res <- curl_fetch_memory(url = "http://v1.26/containers/b834259b9e9ed70cd00a43c9a539ab0601dab68a5966e531fea76ec91bc76940/json", handle = h)
str(fromJSON(rawToChar(res$content)))

# List container processes ----------------------------------------------------------

# ps_args: string
h <- docker_handle(verbose = TRUE)
res <- curl_fetch_memory(url = "http://v1.26/containers/b834259b9e9ed70cd00a43c9a539ab0601dab68a5966e531fea76ec91bc76940/top", handle = h)
str(fromJSON(rawToChar(res$content)))

# Get container logs ----------------------------------------------------------------

# follow: true/false
# stdout: t/f
# stderr: r/f
# since: int
# timestamps: t/f
# tail: string
h <- docker_handle(verbose = TRUE)
res <- curl_fetch_memory(url = "http://v1.26/containers/b834259b9e9ed70cd00a43c9a539ab0601dab68a5966e531fea76ec91bc76940/logs?stdout=true&stderr=true", handle = h)
cat(rawToChar(res$content[res$content > 0x09]))


# Run a container -------------------------------------------------------------------
h <- docker_handle()
handle_setopt(h, copypostfields = '{"Image": "alpine", "Cmd": ["echo", "hello world"]}')
res <- curl_fetch_memory(url = "http:/v1.26/containers/create", handle = h)
str(fromJSON(rawToChar(res$content)))

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions