stevedore

Project Status: Active – The project has reached a stable, usable state and is being actively developed. R build status codecov.io

A docker client for R

knitr::opts_chunk$set(error = FALSE)
has_internet <- function() {
  !is.null(suppressWarnings(utils::nsl("www.google.com")))
}
if (has_internet()) {
  d <- stevedore::docker_client()
  try(d$image$remove("alpine:3.1"), silent = TRUE)
}

hello world example of stevedore

Background

What is docker? Docker is a platform for "containerising" applications - running them in isolation from one another, removing differences of how they are built, what they are built from, and what resources they need (disk, ports, users, etc). It's similar conceptually to virtualisation, but much more light weight.

Why would one want to use docker from R? Whenever you need to control external processes from an R script or package, it might be useful to interact with this process from in containers using docker

These are discussed further in the applications vignette

Usage

The main function in the package is docker_client; this will construct an object with which we can talk with the docker server.

docker <- stevedore::docker_client()
docker

With this you can run containers:

docker$container$run("alpine:3.1", c("echo", "hello world"))

Or run containers in the background

docker$container$run("bfirsh/reticulate-splines", detach = TRUE)

You can manage containers

docker$container$list()
id <- docker$container$list(limit = 1L)$id
container <- docker$container$get(id)
container

And control containers

container$inspect()$config$image
container$logs()
container$stop(t = 0)
container$remove()

And manage images

head(docker$image$list())

Some of these functions have many arguments, but stevedore includes help inline:

docker$container$create

as well as via an help() method on each object (e.g., docker$help(), docker$container$help()) which will display help for the API version that you are using.

Approach

The Docker API is versioned and each version includes a machine-readable API specification. Rather than manually write wrappers that fit the output docker gives, stevedore generates an interface directly from the spefification. Currently stevedore supports docker API versions r stevedore:::DOCKER_API_VERSION_MIN to r stevedore:::DOCKER_API_VERSION_MAX (defaulting to r stevedore:::DOCKER_API_VERSION_DEFAULT).

This approach means that the output will be type-stable - there is no inference on what to return based on what the server chooses to return. With a given API version, the same fields will always be returned. Some of this information is very rich, for example, for the backgrounded container above:

container$inspect(reload = FALSE)

Windows support

Windows support for docker through this package is currently incomplete. The appveyor build tests only the parts of the package that don't actually call docker due to difficult-to-debug issues with container compatibility on that platform.

Current situation

The support for windows is not as comprehensive as for other platforms (but I'm not sure how common using docker is on windows yet). The reason for this is that curl (and the underlying libcurl library) do not support communicating over "named pipes" which is how docker works on windows 10.

The package includes a small wrapper around the python sdk's docker support. On the R side this requires the reticulate package. It also requires that you have a python installation that includes both the docker package and pypiwin32 - once python is installed you can add these packages to python with

pip install docker pypiwin32

(or pip3 instead of pip to use python3).

You can check that everything is configured by running

stevedore:::httppipe_available(verbose = TRUE)

which will return TRUE if everything is OK, and otherwise print some information about errors loading the package. In the case of error consult the reticulate documentation (vignette("versions", package = "reticulate") will be especially useful). Improvements to installation documentation and process are welcome!

Limitations

The primary limitation of the httppipe interface is that streaming connections are not supported. This affects the following methods

Going forward

The support in this package is a duplicate of the micropackage httppipe. This implements the minimum functionality with windows named pipes to support docker. I would love for someone to help port this to a python-free package. This probably requires a bit of C/C++ and knowledge of the win32 API.

Development and testing

See the development guide if you want to get started developing stevedore - it provides pointers to the core objects.

Package limitations

Endpoints that require "http hijacking" are not fully supported (primarily attach) but the foundations are there to support this - stdin is likely to be a major hassle though and I'm not sure if it's possible from within R's REPL.

Installation

stevedore can be installed from CRAN using

install.packages("stevedore")

On windows you will also need reticulate

install.packages("reticulate")

You will also need a python installation and the docker and pypiwin32 packages, which can be installed with pip (see above).

Once installed, find out if everything is set up to use docker by running

stevedore::docker_available()

To install the development version from GitHub, you can use remotes:

remotes::install_github("richfitz/stevedore", upgrade = FALSE)

Licence

MIT © Rich FitzJohn.

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.



richfitz/stevedore documentation built on July 22, 2023, 1:13 p.m.