Running your JuPyTeR notebooks on the cloud
Tags: jupyternotebooks, java, graalvm, cloud
Published: 2019-09-12 20:55:57

---
title: Running your JuPyTeR notebooks on the cloud
published: true
description: Learn and explore the world of notebooks i.e. Jupyter notebooks, how to install it, via a docker container and then do the same on the cloud
canonical_url: https://medium.com/neomatrix369/running-your-jupyter-notebooks-on-the-cloud-ed970326649f
cover_image: https://miro.medium.com/max/458/0*_73ULqxQfyJdht1Z.jpg
tags: jupyter notebooks, java, graalvm, cloud
---

# **Introduction**

On the back of my previous share on how to [build and run a docker container with Jupyter](https://github.com/neomatrix369/awesome-ai-ml-dl/tree/master/examples/JuPyteR#jupyter), I’ll be taking this further on how we can make this run on a cloud platform.

We’ll try to do this on [Oracle Cloud Infrastructure (OCI)](http://cloud.oracle.com/). In theory, we should be able to do everything in the blog on any VM or Baremetal instance. If you are new to Oracle Cloud, I would suggest getting familiar with the [docs](https://docs.cloud.oracle.com/iaas/Content/GSG/Concepts/baremetalintro.htm) and [Getting Started](https://docs.cloud.oracle.com/iaas/Content/GSG/Concepts/baremetalintro.htm) sections of the [docs](https://docs.cloud.oracle.com/iaas/Content/GSG/Concepts/baremetalintro.htm). You will also find several informative links at the bottom of this post, in the **Resources** section.

I found the [tutorial to setup a VM instance](https://docs.cloud.oracle.com/iaas/Content/GSG/Reference/overviewworkflow.htm) simple and useful — I recommend having a glance and following the steps. Take note of the pre-requisites before actually getting downing to creating a VM instance and ***ssh***-ing into it — it will involve Creating Compartments, Subnets, Security Lists, among other things before you can create a VM.

# **Signing up**

You will have noticed you have to have an account to be able to get access to the Cloud Dashboard and proceed.

You can sign up by going to [oracle.com](http://oracle.com/) and also to [cloud.oracle.com](https://myservices.us.oraclecloud.com/mycloud/signup?sourceType=_ref_coc-asset-opcSignIn&language=en) — recommend signing up via these portals. You might even be eligible for **FREE** credits once you do that (enough to spend your weekend running your favourite instances).

# **Setup**

**Dashboard — sign-in**
Once you are signed up, you sign-in via [cloud.oracle.com/sign-in](https://cloud.oracle.com/sign-in) which will take you to a page like this

![](https://miro.medium.com/max/1408/0*bmsehoRRIZ3BXe5F.png)


Follow the instructions as described in the [tutorial to setup a VM instance](https://docs.cloud.oracle.com/iaas/Content/GSG/Reference/overviewworkflow.htm)and give your VM and other resources names (use initials as a prefix) you can identify easily. This will kick off the request to create the VM (if all your entries are valid) — and in less than 15 seconds you should have a VM ready to be used.

Once the VM instance is created, make a note of the **Public IP Address** of the instance. All running VMs can be found on by going to the **Compute > Instance** on the navigation menu on the left:

![](https://miro.medium.com/max/1081/0*g0NsbLE6B-oy_Pva.png)


Select the running VM by clicking it:

![](https://miro.medium.com/max/1408/1*fSnyOupmBeemvNPVWMMFgw.png)


which will take you to the VM details page, where you can spot the **Public IP Address**:

![](https://miro.medium.com/max/1408/1*oBRoXojahvyHq11qL3XIxQ.png)


***Note: the Public IP Address will be different for every VM created, the above is temporary.***

**CLI**

OCI can be accessed with a command-line tool called **oci-cli** which can be [installed by following instructions](https://docs.cloud.oracle.com/iaas/Content/API/SDKDocs/cliinstall.htm) mention in the [CLI docs](https://docs.cloud.oracle.com/iaas/Content/API/Concepts/cliconcepts.htm?Highlight=CLI). Once installed the command to invoke it is called **oci** and you can invoke it by doing the below:

# **Actions to get on the cloud**

```
    $ oci --help
    Usage: oci [OPTIONS] COMMAND [ARGS]...
    Oracle Cloud Infrastructure command line interface, with support for
    Audit, Block Volume, Compute, Database, IAM, Load Balancing, 
Networking, DNS, File Storage, Email Delivery and Object Storage 
Services.
    Most commands must specify a service, followed by a resource type 
and then an action. For example, to list users (where $T contains the 
OCID of the current tenant):

    oci iam user list --compartment-id $T

    Output is in JSON format.

    For information on configuration, see
    https://docs.cloud.oracle.com/Content/API/Concepts/sdkconfig.htm.

    Options:
    <-- snipped -->

    Commands:
    <-- snipped -->
```

As such we won’t need the dashboard for the most part here onwards. We will also NOT be covering the use of the CLI tool in this post.

**Logging into the VM instance**
You can then ***ssh*** into the box (see [docs on connecting via ssh](https://docs.cloud.oracle.com/iaas/Content/Compute/Tasks/accessinginstance.htm?Highlight=ssh)) and proceed with rest of the actions below:

```bash
    ### Oracle Linux and CentOS images, user name: opc
    ### the Ubuntu image, user name: ubuntu
    $ ssh -i ~/.ssh/id_rsa ubuntu@132.145.78.136
    or
    $ ssh ubuntu@132.145.78.136
```

**Installing git**
For this blog post, we selected the **Canonical Ubuntu Linux** (*Canonical-Ubuntu-16.04–2019.08.14–0*) as our OS image, which comes with ***apt-get*** and ***git*** installed so we don’t need to do anything there.

# **Running Jupyter Notebooks**

**Cloning our repo**
We can clone our repo and perform the rest of the steps:

```bash
    $ git clone https://github.com/neomatrix369/awesome-ai-ml-dl
    $ cd awesome-ai-ml-dl/examples/JuPyteR
```

**Installing Docker**
The Docker docs for installing Docker on Ubuntu can be found on [the Docker site](https://docs.docker.com/install/linux/docker-ce/ubuntu/). A bash-script has also been provided to quicken the process, although the target OS here is Ubuntu 16.04 or higher:

```bash
    $ cd build-docker-image
    $ ./installDocker.sh
```

***Note:*** *in case you choose another OS image during VM creation, you will have to install Docker manually with the docs from Docker or modify the above script to make it work for the target OS.*

**Building the Jupyter Docker Image**

```bash
    $ cd build-docker-image
    $ sudo ./buildDockerImage.sh
```

*In this specific environment, we need to pass the* ***sudo*** *keyword before every docker command. You may not have to do that in your local environment or elsewhere.*

**Running the Jupyter notebook as a Docker container**

```bash
    $ cd [back into the project root folder]
    $ sudo ./runDockerContainer.sh
```

This will show you a console like this:

```bash
    <--- snipped --->
    OpenJDK Runtime Environment (build 9.0.4+11)
    OpenJDK 64-Bit Server VM (build 9.0.4+11, mixed mode)
    PATH=/home/jupyter/.local/bin:/opt/java/openjdk/bin:/usr/local/sbin:/usr
/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    ~~~ JDK9, Linux only: We are enabling JVMCI flags (enabling Graal as 
Tier-2 compiler) ~~~
    ~~~ Graal setting: please check docs for higher versions of Java and 
for other platforms ~~~
    JAVA_OPTS=-XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI 
-XX:+UseJVMCICompiler
    JAVA_TOOL_OPTIONS=-XX:+UnlockExperimentalVMOptions 
-XX:+UseCGroupMemoryLimitForHeap -XX:+UnlockExperimentalVMOptions 
-XX:+EnableJVMCI -XX:+UseJVMCICompiler
    Available kernels:
      python2    /home/jupyter/.local/share/jupyter/kernels/python2
      java       /usr/share/jupyter/kernels/java
    [I 13:39:35.993 NotebookApp] Writing notebook server cookie secret 
to /home/jupyter/.local/share/jupyter/runtime/notebook_cookie_secret
    [I 13:39:36.293 NotebookApp] Serving notebooks from local directory: 
/home/jupyter
    [I 13:39:36.294 NotebookApp] The Jupyter Notebook is running at:
    [I 13:39:36.295 NotebookApp] http://(81dde8675279 or 
127.0.0.1):8888/?token=bb0c81ef7e9f3932355b953163702aa2d9f75e18005e6e48
    [I 13:39:36.297 NotebookApp] Use Control-C to stop this server and 
shut down all kernels (twice to skip confirmation).
    [C 13:39:36.310 NotebookApp]
    To access the notebook, open this file in a browser:
            file:///home/jupyter/.local/share/jupyter/runtime/nbserver-
28-open.html
        Or copy and paste one of these URLs:
            http://(81dde8675279 or 127.0.0.1):8888/?
token=bb0c81ef7e9f3932355b953163702aa2d9f75e18005e6e48
```

Make a note of the URL, and replace the *127.0.0.1* with your **Public IP Address** i.e. *132.145.78.136*.
You can also see from the above logs we are using Java 9 (built on the [AdoptOpenJDK](http://adoptopenjdk.net/) farm) and enabling the [GraalVM compiler](https://github.com/oracle/graal/blob/master/compiler/README.md) as HotSpot’s [C2 compiler](http://openjdk.java.net/groups/hotspot/docs/HotSpotGlossary.html) (see [Switches to enable the GraalVM compiler in Java 9](https://github.com/neomatrix369/awesome-ai-ml-dl/blob/master/examples/JuPyteR/README.md#switches-to-enable-the-graalvm-compiler-in-java-9) to enabled the [GraalVM compiler](https://github.com/oracle/graal/blob/master/compiler/README.md)). It’s also because the [Java extension for Jupyter](https://github.com/SpencerPark/IJava)requires Java 9 or higher to work.

**Opening the Jupyter notebook in your browser**
Go to the browser and try to open this:

```bash
    http://132.145.78.136:8888/?token=bb0c81ef7e9f3932355b953163702aa2d9f75e18005e6e48
```

Aargh! It does NOT work!
That is because we haven’t opened up the port 8888 from within our cloud network (via **Ingress Rules,** read more about it [here](https://docs.cloud.oracle.com/iaas/Content/Network/Concepts/securityrules.htm?Highlight=egress)) to the outside world (public):

![](https://miro.medium.com/max/1388/0*40xrtw73GvkKkIgK.png)


We would need to add the above entry to the **Ingress Rules** section, you can get to **Ingress Rules** page via the navigation menu: **Networking** > **Virtual Cloud Networks** > **Virtual Cloud Network Details** > **Security Lists,** which brings you to the page with the **Default Security Lists**. ****On clicking the Security List that corresponds to your **Virtual Cloud Network (VCN)** you will land on the above **Ingress Rules** page.

In case, you are still not able to find it, search for the term ***security*** **using the search facility on any page in the Cloud Console (see the magnifying glass 🔍at the top of the page). This will show you all the **Default Security Lists** and clicking on it will bring you to the **Ingress Rules** page above (you might have just one Security List entry). ***Note: Ingress means traffic coming into the network/VM instance.***

Why port 8888, that’s because we set it up like that in the docker scripts, have a look at the [sources](https://github.com/neomatrix369/awesome-ai-ml-dl/tree/master/examples/JuPyteR#jupyter) to find out why and how.

Having done all of the above: voila! We see the Jupyter startup page in the browser:

![](https://miro.medium.com/max/33/0*iMaX8fhr3Kd_Jdrp.png?q=20)

![](https://miro.medium.com/max/1408/0*iMaX8fhr3Kd_Jdrp.png)


And you can see a Java-based notebook is available to play with! Try out the below by creating a new Java notebook in the browser:

![](https://miro.medium.com/max/1274/0*rquvjgoX8Ra1QT0K.png)


[https://github.com/ligee/kotlin-jupyter/raw/master/samples/ScreenShotInJupyter.png](https://github.com/ligee/kotlin-jupyter/raw/master/samples/ScreenShotInJupyter.png)
You are free to create Python notebooks as well, not just Java ones — this is the beauty of Jupyter notebooks.

# **Installing Jupyter on a bare-metal or VM environment**

For brevity, we didn’t cover this aspect, but if you look at the scripts associated with building and running the Jupyter instance, you will see that the docker build scripts build and run the instance with the help of individual scripts that can be executed on its own in this order:

    $ cd build-docker-image
    $ [install Java 9 SDK and set the PATH and JAVA_HOME]
    $ ./install-jupyter-notebooks.sh
    $ ./install-java-kernel.sh
    $ ./runLocal.sh

If you want to see how this would work, run the above scripts in the local environment of the instance, the rest of the instructions should work as expected.

# **Create custom image for reuse**

As we have been able to successfully run Jupyter Notebook from inside a VM instance, we can save this image for future re-use or share with others. Creating an image of the VM instance can be done via **Compute> Instances > Instance Details** the navigation menu, and Create Custom Image from the Actions drop-down menu:

![](https://miro.medium.com/max/1408/1*m0c2CNpEQ4DBVE8mP6If4Q.png)

![](https://miro.medium.com/max/713/1*zhaCtQeh4_h4IICLqRKRyw.png)


***Note:*** *whilst in the process of creating a custom image, your original VM instance is shut-down. This can take under a couple of minutes to complete depending on the size of the original VM instance.*
When successfully created, it becomes available among the list of Custom Images to choose from, the next time we go to create a new VM instance:

![](https://miro.medium.com/max/1408/1*waim__0FsC1ApNoRnqwj8w.png)

# **Power-user**

If all of this was piece of cake for you or you have survived without much hassle, then try out all the deep-dive stuff mentioned in the [README page here](https://github.com/neomatrix369/awesome-ai-ml-dl/tree/master/examples/JuPyteR#jupyter).
To be able to code in other languages in the Jupyter environment all you need is a Jupyter extension — it’s only a matter of installing and configuring. You can learn all about this [here](https://github.com/neomatrix369/awesome-ai-ml-dl/tree/master/examples/JuPyteR#get-started-manual-steps-via-cli).

# **Signing off**
![](https://miro.medium.com/max/1408/0*gRwXOOQ8q2TtHmnO.png)


In case you have created a notebook, it gets saved in the sub-directory called *jupyter/notebooks*, you can retrieve this using ***scp*** from your local machine (see [here](https://linuxize.com/post/how-to-use-scp-command-to-securely-transfer-files/) on how to do that).
Make sure you have ***signed out of both*** the [oracle.com](http://oracle.com/) and [cloud.oracle.com](http://cloud.oracle.com/) login sessions, it’s easy to forget one or the other. But before doing that please also have a look at the [Cleaning up of resources](https://docs.cloud.oracle.com/iaas/Content/GSG/Tasks/terminating_resources.htm)page in the docs — you don’t want your instance running forever while you are not looking at it!

![](https://paper-attachments.dropbox.com/s_E4A05684460F7D532506C16E09E20D37F6AFF25A9B5381BF1A442679901C1AD7_1568321451272_image.png)

# Conclusion

A good set of scripts (including Docker) and an easy-to-use cloud environment can help in many ways. In this case, enabling us to run a Jupyter notebook instance that can be shared publicly or privately depending on your network security settings.

The Jupyter environment is flexible and allows extending functionality via configurations and extensions.

We didn’t cover things like cloud security and partitioning of user instances — which is sort of out of scope for this post. Please look into this further, if they are important to you. Please do ensure it meets the necessary levels of security for your application or use-case. Check out the docs on [Security on the OCI docs page](https://docs.cloud.oracle.com/iaas/Content/Security/Concepts/security.htm) to learn more.

# **Resources**

**General**

- [Build and run a docker container with Jupyter](https://github.com/neomatrix369/awesome-ai-ml-dl/tree/master/examples/JuPyteR#jupyter)
- [Awesome Graal](https://github.com/neomatrix369/awesome-graal)
- [Awesome AI-ML-DL](https://github.com/neomatrix369/awesome-ai-ml-dl)

**Docker**

- [Docker Hub signup](https://hub.docker.com/signup)
- [Install Docker on Ubuntu 16.04 or higher](https://docs.docker.com/install/linux/docker-ce/ubuntu/)
- [Bash Script to build Docker container](https://github.com/neomatrix369/awesome-ai-ml-dl/blob/master/examples/JuPyteR/buildJuPyteRDockerImage.sh)

**OCI/Cloud**

- [Getting Started](https://docs.cloud.oracle.com/iaas/Content/GSG/Concepts/baremetalintro.htm)
- [Tutorial to setup a VM instance](https://docs.cloud.oracle.com/iaas/Content/GSG/Reference/overviewworkflow.htm)
- [Install CLI](https://docs.cloud.oracle.com/iaas/Content/API/SDKDocs/cliinstall.htm)
- [CLI docs](https://docs.cloud.oracle.com/iaas/Content/API/Concepts/cliconcepts.htm?Highlight=CLI)
- [Cleaning up of resources](https://docs.cloud.oracle.com/iaas/Content/GSG/Tasks/terminating_resources.htm)
- [Docs on connecting via ssh to OCI](https://docs.cloud.oracle.com/iaas/Content/Compute/Tasks/accessinginstance.htm?Highlight=ssh)
- New Sign-in: [https://cloud.oracle.com/en_US/sign-in](https://cloud.oracle.com/en_US/sign-in)
- Traditional Sign-in: [https://myaccount.cloud.oracle.com/](https://myaccount.cloud.oracle.com/)
- [Contact Support](https://docs.cloud.oracle.com/iaas/Content/GSG/Tasks/contactingsupport.htm)
- [Developer Tools](https://docs.cloud.oracle.com/iaas/Content/devtoolshome.htm?tocpath=Developer%20Tools%20%7C_____0)
- [Documentation](https://docs.cloud.oracle.com/iaas/Content/home.htm)
- [Oracle Cloud Community Forum](https://cloudcustomerconnect.oracle.com/resources/9c8fa8f96f/summary)
- [Oracle Cloud Compliance](https://cloud.oracle.com/en_US/cloud-compliance)
- [Oracle Cloud Infrastructure Blog](https://blogs.oracle.com/cloud-infrastructure/)

**Security**

- [Security Rules / Ingress Rules](https://docs.cloud.oracle.com/iaas/Content/Network/Concepts/securityrules.htm?Highlight=egress)
- [OCI Security docs](https://docs.cloud.oracle.com/iaas/Content/Security/Concepts/security.htm)

## About me

Mani Sarkar is a passionate developer mainly in the Java/JVM space, currently strengthening teams and helping them accelerate when working with small teams and startups, as a freelance software engineer/data/ml engineer, [more….](https://neomatrix369.wordpress.com/about)

**Twitter:** [@theNeomatrix369](https://twitter.com/@theNeomatrix369) | **GitHub:** [@neomatrix369](https://github.com/neomatrix369)