Skip to content

Docker with R and keras

Docker with R and keras

Having a stable environment is super useful for development. Creating one within a Docker container in many cases is a good idea. Here I show how to create a container for development with R (and RStudio instance), with installed keras and tensorflow packages. This can be particularly useful if you wish to build a forecasting lstm (long short-term memory neural networks) model, which I showed how to do here.

HOW TO

Let’s start with an official rocker tidyverse image. In order to install keras, we need to include tensorflow and its dependencies, like python 3. Additionally I will include R devtools package dependencies.

Next we need to set up python environment to be able to actually run tensorflow.

Then we can proceed with tensorflow and keras installations.

As this container is aimed for providing a forecasting solution, I will include some of the useful R packages in it, like:

  • forecast – for creating predictions
  • timetk – for time series creation
  • dplyr – for data manipulations

To do so, and to be able to control package versions, I decided to use R renv package. That requires having a renv.lock file in place, containing a list of said packages and their dependencies. Check here if you do not know how to start with renv.

I created my renv.lock file by installing packages of choice in the “empty” container. Then I recreated the container. Specified packages are installed with an R script install_packages.R.

Lastly, I specify the R libpath, in order to be able to install additional packages in the container, if need be.

And this is how the Dockerfile looks like:

Contents of renv.lock file: