In this new post, I like to explain how to configure RStudio in Azure with an Ubuntu virtual machine. In the last few months, I worked a lot on R, RStudio and the integration with Azure. I have created those posts:
- Getting started with R
- Deploying dockerized R/Shiny Apps on Microsoft Azure
- Deploy ShinyApps with Azure and Docker
Also, I worked on the integration between R/Plumber, the library for creating API with R, and Azure using the API Management Service
In Azure I have a very expensive virtual machine with Ubuntu 20.04 because I need a power machine for long and complex calculation. I shutdown the machine when I finish my stuff. When I restart the machine, I have to open again the connection with the Azure Container Repository (ACR). Then, restart the Docker container with RStudio.
So, I don’t want to do everything manually and I started to try to run automatically all the command at the startup. I tried and I found difficult to configure services in Ubuntu, mostly because Ubuntu removed some commands.
After few days, I found the way and I want to share it with you. From the beginning…
Configure the Virtual Machine (VM)
As I said, I created a virtual machine in Azure with Ubuntu 20.04. I don’t explain how to create a virtual machine in this post. If you need help, see this post. So, the machine is ready and I have access to it via SSH.
So, the first thing I want to set up is:
- Docker
- RDP (Remote Desktop Protocol) to connect to the virtual machine
- Connection with Azure Container Registry
- Start RStudio
For that, I prepare a Bash script to run. To create a script to execute, you can follow these steps. There is a simple editor and its name is Nano. It is easy to use and it is already installed. First, open a SSH connection with the virtual machine. Then, I’m going to create the file first.sh
typing this command
nano first.sh
In this file I added all the command I need to set up all the above applications. This is the script:
sudo apt-get update
sudo apt install docker.io
sudo systemctl start docker
sudo systemctl enable docker
sudo apt-get -y install xfce4
sudo apt-get -y install xrdp
sudo systemctl enable xrdp
echo xfce4-session >~/.xsession
sudo service xrdp restart
sudo docker login youracr.azurecr.io --username yourusername --password yourpassword
sudo docker run -d -p 8787:8787 -e USER=rstudio -e PASSWORD=mypassword youracr.azurecr.io/rstudio
So, you end up to have a screen like the following image.

Now, you have to tell Ubuntu that this file is executable. For that, you have to run this command:
chmod +x first.sh
Now, you run the script with this command
./first.sh
Another option is as follows to execute shell script with one of the following commands:
sh script-name-here.sh
bash script-name-here.sh
Now, the virtual machine is ready. If I restart the machine, Docker doesn’t start. So, next step is to configure a service that start at the startup to execute Docker.
Configure a service at the startup with Ubuntu 20.04
So, when I started, I didn’t know that Ubuntu removed chkconfig
to configure a new service or that it requires file with LBS information. I won’t tell you all the story but basically, for every Ubuntu there is some different configuration to apply. The solution with Ubuntu 20.04 is pretty straightforward: I can add the command in /etc/rc.local
So, move to the etc
folder and with nano
editor open the file rc.local
(probably using sudo
)
sudo nano /etc/rc.local
This executes the commands as root. To execute commands as a specific user, use sudo -i -u
(-i
to also run the login shell). For example, to establish a persistent SSH tunnel, where myhost
is definde in johndoe
s ~/.ssh/config
file:
sudo -i -u johndoe autossh -nNT -L 1234:localhost:1234 myhost
Sometimes, the file rc.local
didn’t exists. So, you have a blank file in nano. For that, you have to add at the beginning of the file the following code called Shebang line.
#!/bin/bash

I added the last line in the file because when Docker runs the second line but a container already exists, it raises an error. So, the third line runs Docker with the local image.
How to start the container
A full example of the /etc/rc.local
is the following one
sudo docker login youracr.azurecr.io --username yourusername --password yourpassword
sudo docker rm rstudio
sudo docker run -d -p 8787:8787 -e USER=rstudio -e PASSWORD=mypassword youracr.azurecr.io/rstudio
So, when the machine restarts, Docker logins to the ACR, remove the rstudio
instance, if it exists, and then runs the new instance.
Check the permission of rc.local
Last thing is about /etc/rc.local
: be sure it is executable:
sudo chown root /etc/rc.local
sudo chmod 755 /etc/rc.local

If you have to change the rc.local
file and you applied the 755
you have to change it:
sudo chmod 777 /etc/rc.local
Ensure /etc/rc.local is executable:
sudo chown root /etc/rc.local
sudo chmod 755 /etc/rc.local