A small Docker container for using R and TensorFlow as an enterprise API
This repository creates a production-ready docker image that uses R and the keras and plumber R packages to create a neural network powered REST API. The package keras provides the ability to create neural networks, while plumber allows it to run R as a web service. The docker container is designed to be:
Check out our blog post on the T-Mobile open source website.
To use this repository, first download and install docker. Once docker is installed and the repository is downloaded, go to the repository folder and open a terminal, then run the following commands:
docker build -t r-tensorflow-api . docker run --rm -it -p 80:80 r-tensorflow-api
The first command builds the docker image (which may take up to 30 minutes to compile the R libraries), and the second runs the container.
If the image successfully built, you should be able to go to http://127.0.0.1/names in your browser and see a random 20 pet names generated from a neural network. The names are based on pet licenses from the City of Seattle.
To make your own API, you only need to modify the R code in the
srcfolder. The code in there is split into several files:
main.R[required] - this is what gets run by the API, and only contains code to start plumber. You should not need to modify this.
rest_controller.R[required] - this file specifies the endpoints for your web service. You should modify this heavily as you create your own endpoints.
setup.R[required] - this script is run when building the image to install the R libraries. If any libraries fail to install, the build aborts. You should modify this to include the libraries you need. If you want to install packages from GitHub, you can install devtools and then call it from within this script.
runtime_functions.R- this file contains any additional functions that are needed when you run the service. It's a good place to store functions to keep the
parameters.R- this file contains parameters needed for the model. It's a good place to put values that are need for both building the model and during runtime.
train_model.R- this script trains the model and saves it. It is not run as part of the container, it's only here as a reference. The repository already includes the output of the script (the model as an hdf5 object), but if you wanted to rebuild them you only need to run this file.
The dockerfile does the following steps:
rocker/r-verdocker image - This image was chosen as a balance of ease of maintenance (compared to a pure debian or alpine base) and size of the image (compared to
rocker/tidyversewhich also includes RStudio). Unfortunately this means many R packages have to be manually installed. If you are finding the images builds too slowly for your tastes, switch to using
rocker/tidyverseas the base image.
install.packages()for) you need to list in the
setup.Rfile. Instead of using an R script to install the libraries, you could do it directly from within the dockerfile using
install2.r. We chose to split this out so that data scientists have fewer reasons to touch the dockerfile.
main.Rscript, which has the plumber web server code.
To run the HTTPS version, use these commands:
docker build -f Dockerfile.https -t r-tensorflow-api-https . docker run --rm -it -p 443:443 r-tensorflow-api-https
If you try to test it by going to https://127.0.0.1/names you'll get a warning that the certificates are invalid. You'll want to replace the https/server.cert and https/server.key file with your valid certificates before deploying.
The dockerfile has several differences from the HTTP one:
000-default.conf) and setting Apache 2 to only listen to port 443 and not port 80.
run-r-and-redirect.shscript which is executed when the image is run. This script first starts the apache2 server then runs the
main.RR script. Thus, the docker container has two programs running simultaneously: R and Apache 2.
From the City of Seattle on the pet license data:
The data made available here has been modified for use from its original source, which is the City of Seattle. Neither the City of Seattle nor the Office of the Chief Technology Officer (OCTO) makes any claims as to the completeness, timeliness, accuracy or content of any data contained in this application; makes any representation of any kind, including, but not limited to, warranty of the accuracy or fitness for a particular use; nor are any such warranties to be implied or inferred with respect to the information or data furnished herein. The data is subject to change as modifications and updates are complete. It is understood that the information contained in the web feed is being used at one's own risk.
From RStudio for using some of the R Keras example code as a framework for our model:
the keras library is copyright 2017: RStudio, Inc; Google, Inc; François Chollet; Yuan Tang