Looking for help launching a dockerized shiny app built with the golem framework on an AWS EC2 instance
Asked Answered
F

0

7

Does anyone have resources or prior examples for launching a shiny dockerized app built with the golem framework on an AWS EC2 instance? Mainly, I could use some assistance configuring the Dockerfile, shiny-server.conf, and shiny-server.sh files.

The application is heavily dependent on a PostgreSQL database for managing user data. I've been able to deploy the app with a local docker container, but I'm not sure how to navigate the launch on an EC2 instance. This is mainly because the golem framework is a little different than the traditional structure of a Shiny app (server.r, ui.r, app.r). We need this application to be containerized because there's a global variable user ID that needs to remain contained, as well as at least four unique user datasets floating around each image. I'm not generally held to the AWS EC2 instance for serving the app, but this is what we've discussed on the project team.

Another element that I'm unsure of is the EC2 instance type. The image is ~2.5Gb (and growing). The app may hold >500Mb of data per user. We've got about $3k for hosting that is supposed to last at least two years (and include the RDS cost). Generally, I'm unsure if the price for the app given the size will work.

My project team is sailing into uncharted territories with this setup, so any advice is very welcome.

Here's my working Dockerfile that I believe should be set up for the AWS EC2 instance.

FROM rocker/r-ver:4.0.3
RUN apt-get update && apt-get install -y  git-core \
                                          sqlite3 libsqlite3-dev \
                                          libcurl4-openssl-dev \
                                          libgit2-dev \
                                          libicu-dev \
                                          libssh2-1-dev \
                                          libssl-dev \
                                          libxml2-dev \
                                          libpng-dev \
                                          libudunits2-dev \
                                          libgdal-dev \
                                          libproj-dev \
                                          libgeos-dev \
                                          make pandoc pandoc-citeproc \
                                          zlib1g-dev \
                                          && rm -rf /var/lib/apt/lists/*
RUN echo "options(repos = c(CRAN = 'https://packagemanager.rstudio.com/all/latest/'), download.file.method = 'libcurl')" >> /usr/local/lib/R/etc/Rprofile.site
RUN R -e 'install.packages("remotes")'
RUN R -e 'remotes::install_github("r-lib/remotes", ref = "97bbf81")'

RUN Rscript -e 'remotes::install_version("glue",upgrade="never", version = "1.4.2")'
RUN Rscript -e 'remotes::install_version("processx",upgrade="never", version = "3.4.4")'
RUN Rscript -e 'remotes::install_version("pkgload",upgrade="never", version = "1.1.0")'
RUN Rscript -e 'remotes::install_version("htmltools",upgrade="never", version = "0.5.0")'
RUN Rscript -e 'remotes::install_version("attempt",upgrade="never", version = "0.3.1")'
RUN Rscript -e 'remotes::install_version("testthat",upgrade="never", version = "3.0.0")'
RUN Rscript -e 'remotes::install_version("shiny",upgrade="never", version = "1.5.0")'
RUN Rscript -e 'remotes::install_version("config",upgrade="never", version = "0.3")'
RUN Rscript -e 'remotes::install_version("golem",upgrade="never", version = "0.2.1")'

RUN Rscript -e 'remotes::install_version("shinydashboard",upgrade="never", version = "0.7.1")'
RUN Rscript -e 'remotes::install_version("usmap",upgrade="never", version = "0.5.1")'
RUN Rscript -e 'remotes::install_version("rgeos",upgrade="never", version = "0.5-5")'
RUN Rscript -e 'remotes::install_version("tidyverse",upgrade="never", version = "1.3.0")'
RUN Rscript -e 'remotes::install_version("shinyWidgets",upgrade="never", version = "0.5.4")'
RUN Rscript -e 'remotes::install_version("leaflet",upgrade="never", version = "2.0.3")'
RUN Rscript -e 'remotes::install_version("sf",upgrade="never", version = "0.9-6")'
RUN Rscript -e 'remotes::install_version("DBI",upgrade="never", version = "1.1.0")'
RUN Rscript -e 'remotes::install_version("rpostgis",upgrade="never", version = "1.4.3")'
RUN Rscript -e 'remotes::install_version("ggplot2",upgrade="never", version = "3.3.2")'
RUN Rscript -e 'remotes::install_version("rgdal",upgrade="never", version = "1.5-18")'
RUN Rscript -e 'remotes::install_version("RSQLite",upgrade="never", version = "2.2.1")'
RUN Rscript -e 'remotes::install_version("shinyjs",upgrade="never", version = "2.0.0")'
RUN Rscript -e 'remotes::install_version("tigris",upgrade="never", version = "1.0")'
RUN Rscript -e 'remotes::install_version("sp",upgrade="never", version = "1.4-4")'
RUN Rscript -e 'remotes::install_version("RCurl",upgrade="never", version = "1.98-1.2")'
RUN Rscript -e 'remotes::install_version("DT",upgrade="never", version = "0.16")'
RUN Rscript -e 'remotes::install_version("rmarkdown",upgrade="never", version = "2.5")'
RUN Rscript -e 'remotes::install_version("knitr",upgrade="never", version = "1.30")'

RUN mkdir /build_zone
ADD . /build_zone
WORKDIR /build_zone
RUN R -e 'remotes::install_local(upgrade="never")'
EXPOSE 3838

COPY shiny-server.sh /usr/bin/shiny-server.sh
COPY shiny-customized.config /etc/shiny-server/shiny-server.conf

CMD R -e "options('shiny.port'=3838,shiny.host='0.0.0.0');my_app::run_app()"

RUN ["chmod", "+x", "/usr/bin/shiny-server.sh"]

CMD ["/usr/bin/shiny-server.sh"]

shiny-server.conf

# Instruct Shiny Server to run applications as the user "shiny"
run_as shiny;

# Define a server that listens on port 3838
server {
  listen 3838;
 
  # Define a location at the base URL
  location / {
   
    # Host the directory of Shiny Apps stored in this directory
    app_dir /srv/shiny-server/ga-reporter;
   
    # Log all Shiny output to files in this directory
    log_dir /var/log/shiny-server;
   
    # When a user visits the base URL rather than a particular application,
    # an index of the applications available in this directory will be shown.
    directory_index on;
  }
}

shiny-server.sh

#!/bin/sh

# ShinyServer: Make sure the directory for individual app logs exists
mkdir -p /var/log/shiny-server
chown -R shiny.shiny /var/log/shiny-server

# RUN ShinyServer
exec shiny-server >> /var/log/shiny-server.log 2>&1

Thanks in advance.

Fax answered 8/2, 2021 at 2:12 Comment(4)
If your app is docker based, have you considered using ECS instead of EC2?Crumley
We have not considered that option.Fax
Maybe worth checking it out. Its AWS service specially designed for running docker containers. AWS Fargate is serverless version of ECS.Crumley
ok, I'll look into this option. ThanksFax

© 2022 - 2024 — McMap. All rights reserved.