Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/richfitz/stevedore

:cloud::rowboat::whale::cloud: Docker client for R
https://github.com/richfitz/stevedore

Last synced: about 2 months ago
JSON representation

:cloud::rowboat::whale::cloud: Docker client for R

Awesome Lists containing this project

README

        

# stevedore

[![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
[![R build status](https://github.com/richfitz/stevedore/workflows/R-CMD-check/badge.svg)](https://github.com/richfitz/stevedore/actions)
[![codecov.io](https://codecov.io/github/richfitz/stevedore/coverage.svg?branch=master)](https://app.codecov.io/github/richfitz/stevedore?branch=master)
[![](https://www.r-pkg.org/badges/version/stevedore)](https://cran.r-project.org/package=stevedore)

A docker client for R

```{r, prep, include = FALSE}
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](https://raw.githubusercontent.com/richfitz/stevedore/master/demo/hello.gif)

## 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

- *package authors* might want a clean environment to test their code (similar to travis)
- *a developer using an external database* in a package or in an analysis script might use docker to create disposable copies of the database, or to create a copy isolated from their production database
- *a researcher* might use docker to do a reproducible analysis, or preserve the artefacts that were created along with a copy of the environment that created them

These are discussed further in the [applications vignette](https://richfitz.github.io/stevedore/articles/examples.html)

## Usage

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

```{r, create}
docker <- stevedore::docker_client()
docker
```

With this you can run containers:

```{r, run}
docker$container$run("alpine:3.1", c("echo", "hello world"))
```

Or run containers in the background

```{r, background}
docker$container$run("bfirsh/reticulate-splines", detach = TRUE)
```

You can manage containers

```{r, list}
docker$container$list()
id <- docker$container$list(limit = 1L)$id
container <- docker$container$get(id)
container
```

And control containers

```{r, control}
container$inspect()$config$image
container$logs()
container$stop(t = 0)
container$remove()
```

And manage images

```{r, images}
head(docker$image$list())
```

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

```{r, help}
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](https://docs.docker.com/develop/sdk/#api-version-matrix) and each version includes a [machine-readable API specification](https://docs.docker.com/engine/api/v1.29). 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:

```{r, inspect}
container$inspect(reload = FALSE)
```

## Windows support

Windows support for docker through this package is currently incomplete. The [appveyor build](https://ci.appveyor.com/project/richfitz/stevedore) 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`](https://cran.r-project.org/package=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

```r
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

* container logs with `follow = TRUE`: completely unsupported
* container run - works completely with `detach = TRUE`, and with `detach = FALSE` works but prints output only at the end (not streaming output)
* image build - works but information printed only at end of build rather than streaming
* image pull - works but information printed only at end of pull
* exec start - works but information printed only at end of execution

### Going forward

The support in this package is a duplicate of the micropackage [`httppipe`](https://github.com/richfitz/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](https://github.com/richfitz/stevedore/blob/master/development.md) 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

```r
install.packages("stevedore")
```

On windows you will also need `reticulate`

```r
install.packages("reticulate")
```

You will also need a python installation and the `docker` and `pypiwin32` packages, which can be installed with pip (see [above](#windows-support)).

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

```{r, available}
stevedore::docker_available()
```

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

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

## Licence

MIT © [Rich FitzJohn](https://github.com/richfitz).

Please note that this project is released with a [Contributor Code of Conduct](https://github.com/richfitz/stevedore/blob/master/CONDUCT.md). By participating in this project you agree to abide by its terms.