# Deploying a dockerised shiny app to Azure In this brief tutorial we'll cover how to deploy a dockerised shiny application to the azure app service. ## Setup You will need: - Docker installed - A command line environment (WSL, Linux or Mac Terminal) - A copy of https://github.com/Sparrow0hawk/shiny-docker-azr either via `git clone` or by [downloading a .zip](https://github.com/Sparrow0hawk/shiny-docker-azr/archive/refs/heads/main.zip) - An azure account - Within your Ubuntu windows subsystem for linux you will need: - [VSCode text editor](https://code.visualstudio.com/) installed - The [Azure App Service VSCode extension](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-azureappservice) installed - The [Docker VSCode extension](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-docker) installed ## Building a local docker image First we need to build the docker image locally before we set up things on Azure. To do this we need to start a terminal and ensure the Docker Daemon is running (you can check this by ensuring that Docker Desktop is in the running state). In a terminal: ```bash= # change directory into the shiny-docker-azr repo $ cd shiny-docker-azr # here we invoke docker build to build the image # we specify the build context as `.` meaning in the current directory # this uses the dockerfile present # we then name the docker image and give it a tag with the `-t` argument # with the format container_name:tag_name $ docker build . -t shiny-test:latest ``` Docker will build our container image and if successful you'll see the final line: ```bash= Successfully tagged shiny-test:latest ``` ## Testing our container We can now test run the Docker image to make sure it runs as expected. We do this by using the `docker run` command. ```bash= # here we use `docker run` with the option `-p` # this sets port forwarding so that we can reach the shiny app # outside of the container at localhost:3838 $ docker run -p 3838:3838 shiny-test:latest ``` When this runs it won't return the prompt but will serve the application on http://localhost:3838/ We can interrupt `docker run` by pressing CTRL+C to stop the container. If it runs and you can see: ![](https://i.imgur.com/zebI5S9.png) Everything is running as expected! Yay!🎉 ## Deploying to Azure The simplest way to deploy to azure is via the Visual Studio Code editors Azure App Service extension. This allows you to manage the deployment from a graphical user interface. 1. Create a container registry on Azure which you can do via the Azure portal following these steps (but don't go further thant he Log in to Registry step) - https://docs.microsoft.com/en-gb/azure/container-registry/container-registry-get-started-portal 2. Follow the steps here for deploying the docker container to the azure app service - https://docs.microsoft.com/en-gb/azure/app-service/quickstart-custom-container?tabs=dotnet&pivots=container-linux This should get you to a place where you've successfully deployed the shinyapp to Azure and can visit the web page. ## Notes on deploying Shiny-fakts ### Deploying a database After issues with Azure MariaDB service I used Azure MySQL service (v8.0) instead. I would configure the service with the following settings: - using v8.0 of MySQL - Region set to UK South - Keep all the default settings for now - On the networking tab: - tick the box to allow access to Azure services - Add your current IP to the firewall range For networking we want essentially to be able to dump the data from your machine and then just have your container app talk to the database and that's it. Therefore, we want to aim to keep the firewall as closed as possible. This may take some fiddling to get exactly right but I was able to get the app configured using the specific IP addresses that the Azure App service uses. You can find this information for your app in the Azure portal when you select your App: ![](https://i.imgur.com/RNukZAq.png) ## Uploading your data into the database To do this you'll need a mysql command line client or similar. If you use a command line client you can upload the database with the following command: ```bash $ mysql -h msqldatabase_URL -u DB_USER@SRV_NAME -p'DB_PASSWD' -e 'use fakts_2016; source fakts_2016.sql;' ``` Where: - `msqldatabase_URL` is the full URL available on the portal of your database server - `DB_USER` is the user account you created for the database - `SRV_NAME` is the data base server name - `DB_PASSWD` is the user password for the database you created