Deploy your Shiny App to Azure App Service

Deploy your Shiny app to Azure App service

When you developed your Shiny application it’s time to show it to the world and deploy your app. But how do you deploy a Shiny application? Often deployment can be quite adventure! Luckily, you can deploy your Shiny app to Azure App Service with ease. With this tutorial you will get it done in just 10 minutes! 

About Azure App Service

Azure App Service is a cloud-based platform by Microsoft Azure, designed to simplify the deployment of web applications. It provides a managed environment where developers can easily deploy, manage, and scale their web apps without worrying about infrastructure management.

With Azure App Service, developers can focus on writing code and building great apps, while Azure takes care of the underlying infrastructure, including server maintenance, scaling, and security updates. Whether you’re deploying a simple website, a RESTful API, or a sophisticated web application, Azure App Service provides a straightforward and hassle-free deployment experience, making it ideal for developers of all skill levels.

Since Shiny is just a web application, it is perfectly possible to deploy your Shiny app to Azure App Service too.

Docker and Azure Container Registry

Your application needs to run somewhere, and Azure needs to understand how to serve your Shiny application. To achieve that, you can use Docker and Azure Container Registry.

Docker and Azure Container Registry (ACR) offer powerful tools for streamlining deployment workflows on Azure App Service. Docker, allows developers to package their applications and their dependencies into lightweight, portable containers. These containers provide consistency across different environments, making it easier to deploy applications reliably.

ACR complements Docker by providing a secure and scalable platform for storing and managing Docker images. With ACR, you can simply push your Docker images to a private registry hosted on Azure, and you can access them whenever you need. Additionally, ACR integrates seamlessly with Azure App Service, allowing developers to deploy their Docker-based applications directly from their container registry to their App Service instances.

Somehow, the Docker image needs to end up in ACR. To achieve that, you can use GitHub Actions. This steps ensures that whenever you make a change to the main branch, the image gets updated to reflect the changes. Super convenient! 

To start you need to create a new Container Registry in Azure. The name you give your registry makes up the ACR endpoint. Once the Container Registry is deployed, head over to “Access Keys”, make sure to enable them for the admin user, and copy the username and first password into your GitHub repository under “Secrets”. You will need to create ACR_ENDOINT (the url), ACR_USERNAME, and ACR_PASSWORD.

Time to run our app! Our app is going to run in a Docker container, so we need to make sure that the container contains R and all the necessary packages to run the application. Let’s assume you have a Shiny application that works with renv (an R package dependency manager). In that case, a Dockerfile could look like this:

				
					FROM rocker/r-ver:4.2.0

ENV RENV_VERSION=v1.0.2
RUN R -e "install.packages('remotes')"
RUN R -e "remotes::install_github('rstudio/renv@${RENV_VERSION}')"
RUN R -e "options(renv.config.repos.override = 'https://packagemanager.posit.co/cran/latest')"

COPY . /app

WORKDIR /app

RUN R -e "renv::restore()"

EXPOSE 3838

CMD ["R", "-e", "shiny::runApp('./app.R', host='0.0.0.0', port=3838)"]

				
			

This Dockerfile needs to be build into an image, and we will make that happen with GitHub Actions. To set up an action, you will need to create a .yaml file specifying the runtime and all the steps. In our case, a GitHub Action .yaml file could look like this (located in .github/workflows):

				
					name: Azure Container Registry

on:
  push:
    branches:
      main

env:
  IMAGE_NAME: shiny-azure-app-service

jobs:
  build:
    name: Build container image
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
      - name: Log into registry
        uses: docker/login-action@v3
        with:
          registry: ${{ secrets.ACR_ENDPOINT }}
          username: ${{ secrets.ACR_USERNAME }}
          password: ${{ secrets.ACR_PASSWORD }}
      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          push: true
          tags: ${{ secrets.ACR_ENDPOINT }}/${{ env.IMAGE_NAME }}



				
			

The above workflow will build the Docker image on Ubuntu using the Dockerfile, it will login to ACR, and it will push the image to ACR. Note that we’re using the GitHub repository secrets here for the name of registry (ACR_ENDPOINT), username (ACR_USERNAME) and password (ACR_PASSWORD). To reference a variable from the secrets, you can use ${{ secrets.XXXX }}.

Push the code to the main branch on GitHub and the action will be automatically triggered. You can check if your image ends up correctly in ACR by checking out the “Repositories” section of your ACR in the Azure Portal. This should contain the name of your image. In this example that is “shiny-azure-app-service”.

You can find the full code, including the demo app, on GitHub.

Azure Container Registry + Azure App Service

Once your image is present as a repository in ACR, it’s time to connect Azure App Service to it. You can simply create a new App Service, choose “Web App”, and in the “Docker” set-up section you choose “Azure Container Registry” as Image Source. Alternatively, you can also use Docker Hub or a private Docker repository. 

Deploy your App Service and once done, inspect the resource, head over to “Configuration” and create an application setting called WEBSITES_PORT and set it 3838. Once you did, you will be able to access the URL that is in “Overview”. 

And that’s how you deploy your Shiny app to Azure App Service!

Costs

There are costs involved with setting up the Azure Container Registry. You pay per day for storage, and you pay per second for build time 💰. With the lowest tier this will result in approximately 5 USD per month, depending on your set region and your build times. Azure App Service has a free tier (F1), which allows 60 CPU-minutes per day and 1GB RAM, and 1GB storage. The first basic App Service tier (B1) starts from 0,018 USD per hour. You can get more information about pricing here.

Watch this video on YouTube

Want to see everything in action? You can watch this 10-minute tutorial on how to deploy your Shiny app to Azure App Service.

Plumber API to Azure App Service

Not looking to have your Shiny app deployed, but an R Plumber API? You can do that in a very similar manner too! Check out this article on how to deploy a Plumber API to Azure App Service, and this follow-up article on how to add authentication to your (Plumber) API.

Wrap up

In this article and video you hopefully learned a bit more about Shiny and Azure App Service.

☕️ Was this useful to you and would you like to support me? You can buy me a coffee!

I provide R and Shiny consultancy. Do you need help with a project? Reach out and let’s have a chat!

Leave a Reply

Your email address will not be published. Required fields are marked *