Dockerizing an app that uses mxnet package in R - r

I am dockerizing a shiny app that uses 'mxnet' package. After lots of efforts I concluded that I need to build and install the package instead of just installing it normally from the dmlc repos. Below is me simplified dockerfile that tries to build and install the mxnet:
FROM r-base:latest
RUN apt-get update && apt-get install -y \
sudo \
gdebi-core \
pandoc \
pandoc-citeproc \
libcurl4-gnutls-dev \
libcairo2-dev/unstable \
libxt-dev \
libssl-dev
# Download and install shiny server
RUN wget --no-verbose https://s3.amazonaws.com/rstudio-shiny-server-os- build/ubuntu-12.04/x86_64/VERSION -O "version.txt" && \
VERSION=$(cat version.txt) && \
wget --no-verbose "https://s3.amazonaws.com/rstudio-shiny-server-os- build/ubuntu-12.04/x86_64/shiny-server-$VERSION-amd64.deb" -O ss-latest.deb && \
gdebi -n ss-latest.deb && \
rm -f version.txt ss-latest.deb
# Here comes the installation of other required packages:
# .......
#*** Here comes the problamatic bit: building Installing mxnet
RUN sudo apt-get update
RUN sudo apt-get install -y build-essential git libatlas-base-dev libopencv-dev
RUN git clone --recursive https://github.com/dmlc/mxnet
RUN cd mxnet; make -j$(nproc)
RUN Rscript -e "install.packages('devtools', repo = 'https://cran.rstudio.com')"
RUN cd R-package
RUN Rscript -e "library('devtools'); library('methods'); options(repos=c(CRAN='https://cran.rstudio.com')); install_deps(dependencies = TRUE)"
RUN cd ..
RUN make rpkg
COPY shiny-server.conf /etc/shiny-server/shiny-server.conf
COPY /myapp/* /srv/shiny-server/
EXPOSE 80
COPY shiny-server.sh /usr/bin/shiny-server.sh
CMD ["/usr/bin/shiny-server.sh"]
After running this, I recieve an error saying:
can not cd to R-Package
Any help?

Try with
RUN cd mxnet; make -j$(nproc)\
&& Rscript -e "install.packages('devtools', repo = 'https://cran.rstudio.com')"
your RUN cd .. will not do what you expect, it is the same as opening a terminal, doing cd abc/def and in another terminal, which is in /home/$USER, doing cd .., you will not be in abc.
You should group your RUN commands in order to limit the number of layers in your image, see
https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/
Check also the WORKDIR directive in a Dockerfile
https://docs.docker.com/engine/reference/builder/#workdir
You can check what is correctly done (or not) by launching a shell
docker run -it your_image /bin/bash
and then check what is present or not.

Related

run docker-based rshiny app under a subfolder

I want to develop a Shiny App in docker container, and run this app under a subfolder. Till now I just made it run and opened it through localhost:3838, how can I open it through localhost:3838/myapp?
My dockerfile is as follows:
FROM rocker/shiny
# Install system requirements for index.R as needed
RUN apt-get update && apt-get install -y \
--no-install-recommends \
git-core \
libssl-dev \
libcurl4-gnutls-dev \
curl \
libsodium-dev \
libxml2-dev \
libicu-dev \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
ENV _R_SHLIB_STRIP_=true
COPY Rprofile.site /etc/R
RUN R -e "install.packages('shiny', repos='http://cran.rstudio.com/')"
RUN R -e "install.packages('remotes', repos='http://cran.rstudio.com/')"
COPY . /srv/shiny-server/
USER shiny
EXPOSE 3838
CMD ["/usr/bin/shiny-server"]
add I built and ran the container by the command:
docker build -t myapp .
docker run --rm -p 3838:3838 myapp

Docker image has error when running from R Studio

My Dockerfile contains the exact code as shown in the statworx website to build an image for a Shiny app. Whenever I run the following code however:
# Base image https://hub.docker.com/u/rocker/
FROM rocker/shiny:latest
# system libraries of general use
## install debian packages
RUN apt-get update -qq && apt-get -y --no-install-recommends install
libxml2-dev
libcairo2-dev
libsqlite3-dev
libmariadbd-dev
libpq-dev
libssh2-1-dev
unixodbc-dev
libcurl4-openssl-dev
libssl-dev
## update system libraries
RUN apt-get update &&
apt-get upgrade -y &&
apt-get clean
# copy necessary files
## app folder
COPY /example-app ./app
## renv.lock file
COPY /example-app/renv.lock ./renv.lock
# install renv & restore packages
RUN Rscript -e 'install.packages("renv")'
RUN Rscript -e 'renv::consent(provided = TRUE)'
RUN Rscript -e 'renv::restore()'
# expose port
EXPOSE 3838
# run app on container start
CMD ["R", "-e", "shiny::runApp('/app', host = '0.0.0.0', port = 3838)"]
docker build -t my-shinyapp-image .
I get the following error: failed to solve with frontend dockerfile.v0: failed to create LLB definition: dockerfile parse error line 7: unknown instruction: LIBXML2-DEV
If I remove the LIBXML2-DEV package it gives an error on the next one. Has anyone encountered this problem before?
Following the suggestion of #HubertL, here is the Dockerfile that will work:
# Base image https://hub.docker.com/u/rocker/
FROM rocker/shiny:latest
# system libraries of general use
## install Debian/Ubuntu packages
RUN apt-get update -qq && \
apt-get upgrade -y && \
apt-get -y --no-install-recommends install \
libxml2-dev \
libcairo2-dev \
libsqlite3-dev \
libmariadbd-dev \
libpq-dev \
libssh2-1-dev \
unixodbc-dev \
libcurl4-openssl-dev \
libssl-dev
## update system libraries
RUN apt-get update && \
apt-get upgrade -y && \
apt-get clean
# copy necessary files
## app folder
COPY /example-app ./app
## renv.lock file
COPY /example-app/renv.lock ./renv.lock
# install renv & restore packages
RUN Rscript -e 'install.packages("renv")'
RUN Rscript -e 'renv::consent(provided = TRUE)'
RUN Rscript -e 'renv::restore()'
# expose port
EXPOSE 3838
# run app on container start
CMD ["R", "-e", "shiny::runApp('/app', host = '0.0.0.0', port = 3838)"]

/usr/local/lib64/R/lib/libR.so: cannot open shared object file: No such file or directory

I am getting this error while making an image through docker on lambda.
** [ERROR] OSError: cannot load library '/usr/local/lib64/R/lib/libR.so': /usr/local/lib64/R/lib/libR.so: cannot open shared object file: No such file or directory**
Docker file:
FROM public.ecr.aws/lts/ubuntu:latest
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y wget
RUN apt-get install -y software-properties-common
RUN apt-get install -y gcc gfortran build-essential
RUN apt-get update
RUN apt-get install -y libcurl4-openssl-dev
RUN apt-get install -y zlib1g zlib1g-dev libbz2-dev liblzma-dev libpcre++-dev libpango1.0-dev \
xorg-dev \
libreadline-dev
RUN wget -c https://cran.r-project.org/src/base/R-3/R-3.5.0.tar.gz && tar -xf R-3.5.0.tar.gz
RUN cd R-3.5.0 && ./configure --with-readline=no --with-x=no && make && make install
RUN R -e "install.packages('remotes', repos = 'http://cran.us.r-project.org')"
RUN R -e "require(remotes)"
RUN R -e "remotes::install_version('text2vec', version = '0.5.1', repos = 'http://cran.us.r-project.org')"
RUN export LD_LIBRARY_PATH="/usr/local/lib64:$LD_LIBRARY_PATH"
COPY requirements.txt ./
RUN apt-get update && apt-get install -y \
python3.8 \
python3-pip
RUN pip3 install -r requirements.txt
RUN pip3 install \
awslambdaric
RUN ln -s /usr/lib64/R/lib/* /usr/local/lib64/R/lib/
COPY lambda_function.py ./
ENTRYPOINT [ "/usr/bin/python3", "-m", "awslambdaric" ]
CMD ["lambda_function.lambda_handler"]
Any help would be appreciated!
I cleaned up the first parts of your Dockerfile as
FROM public.ecr.aws/lts/ubuntu:latest
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y \
wget \
software-properties-common \
gcc gfortran build-essential \
libcurl4-openssl-dev \
zlib1g zlib1g-dev libbz2-dev liblzma-dev libpcre++-dev libpango1.0-dev \
xorg-dev \
libreadline-dev && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
RUN wget -c https://cran.r-project.org/src/base/R-3/R-3.5.0.tar.gz && \
tar -xf R-3.5.0.tar.gz && \
cd R-3.5.0 && \
./configure --with-readline=no --with-x=no --enable-R-shlib && \
make -j && make install
Putting all the apt-get into one RUN allows one to clean up by removing apt cache files. Using make -j builds R in parallel, which can speed up the build significantly.
I then built it, and launched the resulting image using bash to see that libR.so did indeed exist.
$ docker build .
...
Successfully built f1d83e97e8ef
$ docker run -it --rm f1d83e97e8ef bash
root#add29666d5d5:/# ls -l /usr/local/lib64/R/lib
total 21984
-rwxr-xr-x 1 root root 16815248 Sep 5 14:43 libR.so
-rwxr-xr-x 1 root root 462896 Sep 5 14:43 libRblas.so
-rwxr-xr-x 1 root root 5226296 Sep 5 14:43 libRlapack.so
libR.so exists and permissions (read and execute for all) are appropriate.
I don't think exporting LD_LIBRARY_PATH or creating a symbolic link should be necessary.

where to write and save the dockerfile?

I am a beginner in docker, my question can be considered somewhat obvious but where do I save and write the dockerfile?
example:
FROM openanalytics/r-base
LABEL maintainer "Tobias Verbeke <tobias.verbeke#openanalytics.eu>"
# system libraries of general use
RUN apt-get update && apt-get install -y \
sudo \
pandoc \
pandoc-citeproc \
libcurl4-gnutls-dev \
libcairo2-dev \
libxt-dev \
libssl-dev \
libssh2-1-dev \
libssl1.0.0
# system library dependency for the euler app
RUN apt-get update && apt-get install -y \
libmpfr-dev
# basic shiny functionality
RUN R -e "install.packages(c('shiny', 'rmarkdown'), repos='https://cloud.r-project.org/')"
# install dependencies of the euler app
RUN R -e "install.packages('Rmpfr', repos='https://cloud.r-project.org/')"
# copy the app to the image
RUN mkdir /root/euler
COPY euler /root/euler
COPY Rprofile.site /usr/lib/R/etc/
EXPOSE 3838
CMD ["R", "-e", "shiny::runApp('/root/euler')"]
where specifically does this file have to be written? Where specifically does he have to be saved? what format does it have to be saved?
The pattern I follow is to include the Dockerfile in the root of the project directory.
I also store it in the same repo as the project.

cannot install devtools in r shiny docker

I'm trying to build a docker image for my shiny app. Below is my dockerfile. When I build my images, everything else seems fine, except I got error message Error in library(devtools) : there is no package called ‘devtools’ Execution halted. I also tried devtools::install_github('nik01010/dashboardthemes') with no success. I have non clue why? What could go wrong? Do anyone know what is wrong with my dockerfile? Thanks a lot.
# Install R version 3.6
FROM r-base:3.6.0
# Install Ubuntu packages
RUN apt-get update && apt-get install -y \
sudo \
gdebi-core \
pandoc \
pandoc-citeproc \
libcurl4-gnutls-dev \
libcairo2-dev/unstable \
libxt-dev \
libssl-dev
# Download and install ShinyServer (latest version)
RUN wget --no-verbose https://s3.amazonaws.com/rstudio-shiny-server-os-build/ubuntu-12.04/x86_64/VERSION -O "version.txt" && \
VERSION=$(cat version.txt) && \
wget --no-verbose "https://s3.amazonaws.com/rstudio-shiny-server-os-build/ubuntu-12.04/x86_64/shiny-server-$VERSION-amd64.deb" -O ss-latest.deb && \
gdebi -n ss-latest.deb && \
rm -f version.txt ss-latest.deb
# Install R packages that are required
RUN R -e "install.packages(c('devtools', 'shiny','shinythemes','shinydashboard','shinyWidgets','shinyjs', 'tidyverse', 'dplyr', 'ggplot2','rlang','DT','lubridate', 'plotly', 'leaflet', 'mapview', 'tigris', 'rgdal', 'visNetwork', 'wordcloud2', 'arules'), repos='http://cran.rstudio.com/')"
RUN R -e "library(devtools)"
RUN R -e "install_github('nik01010/dashboardthemes')"
# Copy configuration files into the Docker image
COPY shiny-server.conf /etc/shiny-server/shiny-server.conf
COPY /app /srv/shiny-server/
# Make the ShinyApp available at port 80
EXPOSE 80
# Copy further configuration files into the Docker image
COPY shiny-server.sh /usr/bin/shiny-server.sh
CMD ["/usr/bin/shiny-server.sh"]
There are a few approaches you might try.
Easiest:
Use remotes::install_github instead of devtools. remotes has many fewer dependencies if you don't need the other functionality.
Second Easiest:
Use rocker/tidyverse image from Docker Hub instead of baseR image.
docker pull rocker/tidyverse
Change line 2:
FROM rocker/verse
Hardest:
Otherwise, you will need to figure out which dependencies you need to install inside your docker image before you can install devtools. It will likely be obvious if you try to install it interactively.
Make sure the container is running
Get the container name using docker ps
Start a shell with docker exec -it <container name> /bin/bash
Start R and try to install devtools interactively

Resources