Different ways to start a shiny app inside docker?

shiny
docker

#1

What are the different ways to run a shiny app in a docker container?

These are the options I am aware of:

  1. CMD ["/usr/bin/shiny-server.sh"]
  2. CMD ["Rscript", "app.R"]
  3. CMD ["R", "-e", "shiny::runApp()"]

I started out with the first one, but the database connection was not working, so I switched to the second one, but there images were not displayed correctly (maybe same issue as here image-not-showing-in-shiny-app-r), so I switched to the third one.

Now I'd like to understand more about the differences, so I can choose what is most appropriate in my use case.


#2

I'm not sure why you are seeing different behaviour with you database connections and images. But I can tell you what you are asking the CMD statement to do in each case.

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

This will start shiny-server and is the preferred way of running shiny apps for production. The biggest benefit is that you can have multiple R sessions, managed by shiny-server. For example if you have multiple users and need the application to scale. You can adjust the resources allocated to your docker container, and shiny-server will be able to make use of the additional resources.

You may not be able to access your database because usually shiny-server runs as the user shiny, and you database settings may be expecting a different user. You can can change the (linux) user that is used to run your app in the shiny-server configuration files.

  1. CMD ["Rscript", "app.R"]

You running your app as you would any other R script from the console. Thanks to the "single file app" structure this will run, and only in this case. Should you have an app with a server.R and ui.R file than you would not be able to call it like this. Having separate server.R and ui.R files is common in larger shiny apps, so this might not be the best way to go about it.

Also I'm not sure if this will set the current working directory correctly. And this is probably why it does not find your image (the process is unaware of your www folder).

  1. CMD ["R", "-e", "shiny::runApp()"]

This works, and sets the working directory of your app, but you are limited to only one R Process. Maybe that is enough for your use case, but as soon as you have more users you will likely want to move this to shiny server. Joe Cheng once explained the diferences in the shiny chat.


#3

Thank you for the insights @FvD!

Yes, this is probably the source of error, because when I just run app.R with the other two CMD commands, it works fine. I will have to look into this.

This is actually not a limitation I think, because you can easily put some of the code into different R scripts and source those. I have put everything into shiny modules and the app.R is just 30 lines of code, where the corresponding modules are called.


split this topic #4

2 posts were split to a new topic: Running a shiny app in an docker, error "Error in connection_create(names(opts), as.vector(opts)) : could not connect to server:"


closed #5

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.