If you plan to bring vetiver to a public or private cloud rather than Posit Connect, Docker containers are a highly portable solution. Using vetiver makes Dockerfile creation easy by generating the files you need from your trained models.
Import data
For this demo, we will use data from Tidy Tuesday to predict the number of YouTube likes a television commercial played during the Super Bowl will get, based on qualities such as if the ad included any animals, if the ad was funny, if the ad had any elements of danger, etc.
With data in hand, the next step is feature engineering and model estimation. We put these two steps into a single function, such as a pipeline or workflow, and will deploy these pieces together. (Why are we doing this?)
import pinsboard = pins.board_rsconnect( server_url=server_url, # load from an .env file api_key=api_key, # load from an .env file allow_pickle_read=True)vetiver.vetiver_pin_write(board, v)
library(pins)board <-board_connect() # authenticates via environment variablesvetiver_pin_write(board, v)
Here we are using board_connect(), but you can use other boards such as board_s3(). Read more about how to store and version your vetiver model.
Using local boards with Docker
Local boards such as board_folder() will not be immediately available to Docker images created by vetiver. We don’t recommend that you store your model inside your container, but (if appropriate to your use case) it is possible to edit the generated Dockerfile and COPY the folder and model into the container. Alternatively, you can mount the folder as a VOLUME.
Learn more about why we recommend storing your versioned model binaries outside Docker containers in this talk.
Create Docker artifacts
To build a Docker image that can serve your model, you need three artifacts:
the Dockerfile itself,
a requirements.txt or renv.lock to capture your model dependencies, and
an app.py or plumber.R file containing the information to serve a vetiver REST API.
You can create all the needed files with one function.
You have now created all the files needed to build your Docker image!
Build and run your Dockerfile
It is time to build and run your container. Building the Docker container can potentially take a while, because it installs all the packages needed to make a prediction with this model. Use the command line (not R or Python) to build your Docker container:
docker build -t superbowlads .
Tip
If you are on an ARM architecture locally and deploying an R model, use --platform linux/amd64 for RSPM’s fast installation of R package binaries.
Now run! To authenticate to your board (to get the pinned vetiver model from, for example, Posit Connect), pass in a file supplying environment variables.
docker run --env-file .env -p 8080:8080 superbowlads
Tip
R users likely will store their environment variables in a file called .Renviron instead of .env.
The Docker container is now running locally! You can interact with it, such as by using a browser to visit http://0.0.0.0:8080/__docs__/