where to write and save the dockerfile? - r

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.

Related

Copying R Libraries to Docker Image from Local Libraries

I created a renv for my Shiny app. As you know that when you build a dockerfile, it has to install every package and its dependencies. If there are many packages, it takes long time.
I already installed packages my local machine. Also, the app will run on the remote machine. Is there any chance to copy the all libraries without restoring the renv? My local machine is macOS and my remote machine is Ubuntu. Due to the different Operating Systems might not work to use libraries. Probably, there are different dependencies between macOS and Ubuntu for the R packages.
Which is the best way to install R packages in a Dockerfile? Should I wait long docker building every time?
Directory
FROM openanalytics/r-base
# system libraries of general use
RUN apt-get update && apt-get install -y \
sudo \
nano \
automake \
pandoc \
pandoc-citeproc \
libcurl4-openssl-dev \
libcairo2-dev \
libxt-dev \
libssl-dev \
libssh2-1-dev \
libssl1.1 \
libmpfr-dev \
# RPostgres
libpq-dev \
libssl-dev \
# rvest
libxml2-dev \
libssl-dev
# Install Packages
# RUN R -e "install.packages(c('janitor','DBI','RPostgres','httr', 'rvest','xml2', 'shiny', 'rmarkdown', 'tidyverse', 'lubridate','shinyWidgets', 'shinyjs', 'shinycssloaders', 'openxlsx', 'DT'), repos='https://cloud.r-project.org/')"
# copy the app to the image
RUN mkdir /root/appfile
COPY appfile /root/appfile
# renv restore
RUN Rscript -e 'install.packages("renv")'
RUN Rscript -e "renv::restore(lockfile = '/root/appfile/renv.lock')"
EXPOSE 3838
CMD ["R", "-e", "shiny::runApp('/root/appfile')"]
Thanks.

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)"]

Not found bin/sh Docker Image

I am trying to deploy my Shiny app on my own server, but I cannot solve this issue. This is my docker image
FROM rocker/tidyverse
## App folder
WORKDIR /home/admin/shinyAppCovid/
ADD /shinyApp /shinyApp/
ADD /dependencias.txt /usr/local/bin/
RUN apt-get update && apt-get install -y \
sudo \
pandoc \
pandoc-citeproc \
libcurl4-gnutls-dev \
libcairo2-dev \
libxt-dev \
libssl-dev \
libssh2-1-dev
# expose port
EXPOSE 8080
# Download and install library
RUN R -e 'install.packages(read.csv("/usr/local/bin/dependencias.txt")[,1], depencencies=TRUE, repos="http://cran.rstudio.com/")'
# run app on container start
CMD ['/usr/bin/R', "-e", "shiny::runApp("/shinyApp/shiny", host = '0.0.0.0', port = 8080)"]
The problem is that when i a try to run a container this is what i have
/bin/sh: 1: [/usr/bin/R,: not found
I changed the last line for jus R and is not working I am still having the same issue, How can I do?

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

Dockerizing an app that uses mxnet package in 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.

Resources