[This article was first published on The Jumping Rivers Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.This is part one of a two part series on {vetiver}.
IntroductionIn our previous blog, we provided an overview of MLOps and the{vetiver} package, creating anddeploying a simple model locally. In this post, we’ll show you how todeploy a model to production using PositConnect,SageMaker, and Docker.
What is DockerDocker is an open-source platform that allowsdevelopers to build, deploy, and run containers. These containers bundleapplication source code with the operating system libraries anddependencies needed to run that code.
Previously, we discussed deploying a ShinyApplicationusing Docker. Similarly, we can deploy a set of APIs to access ourmodel.
Data comes in all shapes and sizes. It can often be difficult to know where to start. Whatever your problem, Jumping Rivers can help.
Creating a Docker fileThe {vetiver} package simplifies creating a Dockerfile. We simply run:
vetiver::vetiver\_prepare\_docker( pins::board\_connect(), "colin/k-nn", docker\_args = list(port = 8080))
This command accomplishes several tasks:
{renv}package to create a list of R package dependencies required to runyour model.plumber.R containing the necessary code todeploy an API, essentially just vetiver_api().The Dockerfile includes several components. The first component sets theR version, specifies the package repository, and crucially, installs thenecessary system libraries.
FROM rocker/r-ver:4.4.0ENV RENV\_CONFIG\_REPOS\_OVERRIDE https://packagemanager.rstudio.com/cran/latestRUN apt-get update -qq && apt-get install -y --no-install-recommends \ ...
The second component copies the renv.lock file and installs the requiredR packages:
COPY vetiver\_renv.lock renv.lockRUN Rscript -e "install.packages('renv')"RUN Rscript -e "renv::restore()"
Finally, we have the plumber/API section
COPY plumber.R /opt/ml/plumber.REXPOSE 8080ENTRYPOINT ["R", "-e", "pr <- plumber::plumb('/opt/ml/plumber.R'); pr$run(host = '0.0.0.0', port = 8080)"]
which runs the API on port 8080.
The container is built via
docker build --tag my-first-model .
The --tag flag allows you to name your Docker image. You can inspectyour stored Docker images with:
docker image listREPOSITORY TAG IMAGE ID CREATED SIZEmy-first-model latest 792af21c775a About a minute ago 1.33GB
To run the image, use
docker run --rm --publish 8080:8080 my-first-model
Posit Connect / Sage MakerWe can also trivially publish the model to Posit Connect via
vetiver::vetiver\_deploy\_rsconnect(board = pins::board\_connect(), "colin/k-nn")
Similarly, we can publish to SageMaker using the functionvetiver_deploy_sagemaker().
For updates and revisions to this article, see the original post
To leave a comment for the author, please follow the link and comment on their blog: The Jumping Rivers Blog.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.Continue reading: Vetiver: Model Deployment