This repository is intended as an end-to-end example of deploying an R Shiny application using Docker. It also employs the renv R package to simplify package installation in the Docker image.
It is based on this blog post from STATWORX.
I you don't have it already, install Docker.
Then, move to folder you want to work in and clone this repository.
git clone https://github.com/DavidASmith/r-shiny-docker-renv.git
Explore the files.
cd r-shiny-docker-renv
ls
Note that there is a Dockerfile
which contains all the instructions to build an image of the R Shiny server and our app.
The example-app
folder contains the R Shiny app we want to deploy.
To run the app locally, we must follow these steps:
- Build the Docker image.
- Start the container.
- Open the app in the browser.
To build the image, navigate to the folder containing the Dockerfile and run the following command.
docker build -t my-shinyapp-image .
Wait for the build process to finish, then start the container by running the following.
docker run -d --rm -p 3838:3838 my-shinyapp-image
Open your web browser and go to http://localhost:3838/. The example app 'Old Faithful Geyser Data' is displayed.
Assuming you have already...
- Got an Azure project you can work in.
- Have created a resource group for the app.
We can deploy the dockerised app to Azure as a Webapp by performing the following steps:
- Create a container registry to store the image of our app.
- Build the image in the container registry.
- Deploy the image as a web app.
Use the following command to create a container registry to store the image of the app.
az acr create --name myregistry --resource-group mygroup --sku standard --admin-enabled true
Where:
myregistry
is what you want to name your container registry.mygroup
is the name of the resource group in which the registry will be created.
Next we need to build the image in the container registry. Run this from the folder which contains the Dockerfile.
az acr build --file Dockerfile --registry myregistry --image myimage .
Where:
myregistry
is the name of your container registry.myimage
is what you want to name the image in the registry.
To deploy the image as a Web App:
- In the Azure portal, navigate to the resource group you have been using in the steps above.
- Click + Add and select Web App. The Create Web App page is displayed.
- Enter a Name for your app. Note that this will form part of the URL on which the app can be accessed.
- Select Docker Container as the Publish option.
- Select Linux as the Operating System.
- Select the Region where your app will be hosted.
- Select an App Service Plan (or leave as default for this example).
- Click Next : Docker >.
- For the Image Source, select Azure Container Registry.
- Select the Registry you created in the steps above.
- Select the Image you created.
- Click Review + create.
After some time for deployment, you should be able to navigate to the newly created App Service in the portal. On the Overview page there is a URL. Go to this URL to view your app.