3

How is it possible to deploy a shiny app via Shiny Server in a Docker container deployed to Elastic Beanstalk?

Theoretically it is possible to run R/Shiny (Server) on AWS Elastic Beanstalk with Docker containters. Unfortunately, I couldn't see any tutorial on this topic and my attempts failed miserably.

It is possible to deploy Shiny Server on EC2 instances with autoscaling (been there, done that) so EB shouldn't be to much of a problem. But again, my experience and knowledge is limited.

berkorbay
  • 443
  • 7
  • 22
  • 1
    I got so close yesterday but it failed on nginx part. I can share Dockerfile GH gist if you like. Rest is template shiny example (geyser histogram) – berkorbay Apr 30 '20 at 05:30

2 Answers2

3

It appears that doing it was straightforward. Here is a minimal Dockerfile for the example Shiny Server run.

FROM rocker/shiny:3.6.3
USER shiny
EXPOSE 3838
CMD ["/usr/bin/shiny-server.sh"]

You can directly upload from ElasticBeanstalk interface.

Here are two very important basic mistakes that may be trouble in EB deployments

  • Whole installation from Dockerfile should not exceed 300 seconds (I had this error because of R tidyverse installations).
  • If you upload a folder, make sure there is no parent folder to your files (esp. Dockerfile).
berkorbay
  • 443
  • 7
  • 22
  • Hi berkorbay, I've hit problems around the tidyverse installation as well. Would you have any recommendations around this? I was just going to start using data.table instead for production. Splitting tidyverse into just dplyr etc. Wasn't getting me anywhere. – Aaron Gorman Sep 23 '21 at 11:52
  • 1
    @AaronGorman a shortcut solution might be using already installed tidyverse docker images such as https://hub.docker.com/r/rocker/shiny-verse. – berkorbay Sep 29 '21 at 14:10
3

Here's a sketch of the full procedure, including installing additional R packages.

Put your shiny apps in a directory called apps. Multiple apps can live in multiple subdirectories of apps.

Make a file called Dockerfile.base with the following contents.

FROM rocker/shiny
# Install more R packages like this:
RUN . /etc/environment && R -e "install.packages(c('ROCR', 'gbm'), repos='$MRAN')"

Build it locally and push to AWS ECR. Follow AWS's instructions but here's a sketch.

# region="us-west-1"
# aws_account_id=123456789
aws ecr get-login-password --region $region | docker login --username AWS --password-stdin ${aws_account_id}.dkr.ecr.${region}.amazonaws.com
docker build -t rshiny-base Dockerfile.base
docker tag rshiny-base:latest ${aws_account_id}.dkr.ecr.${region}.amazonaws.com/rshiny-base:latest
docker push ${aws_account_id}.dkr.ecr.${region}.amazonaws.com/rshiny-base:latest

Create a new Dockerfile with the following contents. Note that it is copying your apps into the image.

FROM <aws_account_id>.dkr.ecr.<region>.amazonaws.com/rshiny-base
USER shiny
COPY apps /srv/shiny-server
EXPOSE 3838
CMD ["/usr/bin/shiny-server.sh"]

Git-commit, create an Elastic Beanstalk app, and deploy. Here's a sketch:

eb init
eb create shiny

I wrote the full procedure up in a blog post at https://www.highonscience.com/blog/2021/06/02/shiny-apps-elastic-beanstalk/.

Will High
  • 410
  • 3
  • 6