2

I would like to run plumber API on Docker container.

I tried the following two dockerfiles:

The first Dockerfile

FROM rstudio/plumber
MAINTAINER Docker User <docker@user.org>
RUN R -e "install.packages('broom')"
RUN mkdir -p ~/application

# copy everything from the current directory into the container
COPY "/" "application/"
WORKDIR "application/" 

# open port 80 to traffic
EXPOSE 80

# when the container starts, start the main.R script
ENTRYPOINT ["Rscript", "execute_plumber.R"]

The second Dockerfile:

FROM rocker/r-ver:4.0.2

# install the linux libraries needed for plumber
RUN apt-get update -qq && apt-get install -y \
  libssl-dev \
  libcurl4-gnutls-dev

# install plumber
RUN R -e "install.packages('plumber')"
RUN mkdir -p ~/application

# copy everything from the current directory into the container
COPY "/" "application/"
WORKDIR "application/" 

# open port 80 to traffic
EXPOSE 80

# when the container starts, start the main.R script
ENTRYPOINT ["Rscript", "execute_plumber.R"]

Both of them resulted in the following error:

* Downloading renv 0.12.0 from CRAN archive ... OK
* Installing renv 0.12.0 ... Done!
Successfully installed and loaded renv 0.12.0.
Error in library(plumber) : there is no package called ‘plumber’
Execution halted

In case of the second Dockerfile I also tried to install different linux libraries such

git-core
libcurl4-openssl-dev
xml2 openssl

None of them helped.

I am running it on Azure. Any suggestions?

UPDATE In container build log file I can clearly see the plumber was successfully installed.

> install.packages('plumber')
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
also installing the dependencies ‘curl’, ‘Rcpp’, ‘later’, ‘BH’, ‘rlang’, ‘glue’, ‘R6’, ‘stringi’, ‘jsonlite’, ‘webutils’, ‘httpuv’, ‘crayon’, ‘promises’, ‘sodium’, ‘swagger’, ‘magrittr’, ‘mime’, ‘lifecycle’

...

* DONE (plumber)
user1700890
  • 7,144
  • 18
  • 87
  • 183

1 Answers1

2

In this output:

> install.packages('plumber')
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
also installing the dependencies ‘curl’, ‘Rcpp’, ‘later’, ‘BH’, ‘rlang’, ‘glue’, ‘R6’, ‘stringi’, ‘jsonlite’, ‘webutils’, ‘httpuv’, ‘crayon’, ‘promises’, ‘sodium’, ‘swagger’, ‘magrittr’, ‘mime’, ‘lifecycle’

...

* DONE (plumber)

It looks like packages are being installed into the R site library, but here:

* Downloading renv 0.12.0 from CRAN archive ... OK
* Installing renv 0.12.0 ... Done!
Successfully installed and loaded renv 0.12.0.
Error in library(plumber) : there is no package called ‘plumber’
Execution halted

The project's renv autoloader (normally part of the project .Rprofile) is being run, which would set up your project to use a private library path (isolated from your site library).

I suspect you could fix this by re-organizing the commands in your Dockerfile; e.g.

FROM rocker/r-ver:4.0.2

# install the linux libraries needed for plumber
RUN apt-get update -qq && apt-get install -y \
  libssl-dev \
  libcurl4-gnutls-dev

# create the application folder
RUN mkdir -p ~/application

# copy everything from the current directory into the container
COPY "/" "application/"
WORKDIR "application/" 

# open port 80 to traffic
EXPOSE 80

# install plumber
RUN R -e "install.packages('plumber')"

# when the container starts, start the main.R script
ENTRYPOINT ["Rscript", "execute_plumber.R"]

In other words, make sure install.packages("plumber") gets called after the working directory is set.

Kevin Ushey
  • 20,530
  • 5
  • 56
  • 88
  • Thank you, I will give a try, but I think `COPY` command will fail, since `RUN mkdir -p ~/application` is executed only later and directory does not exist yet – user1700890 Dec 16 '20 at 21:30
  • 1
    Sorry, yes -- that command should be moved earlier. I'll edit my response. – Kevin Ushey Dec 16 '20 at 21:45
  • 1
    Thank you, your suggestion worked. Container is now running, now I need to be able to reach my API, but this is different question – user1700890 Dec 17 '20 at 00:22