Featured image of post Expose Your Jupyter

Expose Your Jupyter

Run your jupyter on server and visit it remotely.

In this semester, I chose a data science unit, which caused me to use jupyter. I have never used jupyter and got in touch with data science before. After knowing what is jupyter notebook, I thought that running it on cloud is better than local. Fortunately, the official has already released different kinds of docker images of jupyter notebook for different purposes. This post is going to introduce how I deploy jupyter notebook on my home server and expose it to the Internet.

The post assumes that the jupyter server will only serve for the owner instead of multi-user.

# Run Jupyter on Docker

# Image Choosing

If you still don’t sure which image you should use, you can check here.

For example, our lecturer said we need to use pandas in this unit, then I search keyword pandas on the webpage. I found jupyter/scipy-notebook is the way to go.

If you have no experience like me, I will recommend the lightest one.

# Create Working Directory

Since jupyter runs in docker, you need to prepare a folder that your jupyter server has read/write authority.

By the way, jovyan is the default user of jupyter and its docker image.

1
2
3
4
# create the working directory
mkdir -p /{directory on your server}/jupyter/jovyan/.jupyter
# ensure jupyter server have the read/write authority
chmod 777 -R /{directory on your server}/jupyter/

# Run the Container

Then run your jupyter server on docker.

1
2
3
4
5
6
docker run -d \
    --name jupyter-server \
    --mount type=bind,source=/home/jovyan,target=/{directory}/jupyter/jovyan \
    --net=host \
    --restart=unless-stopped \
    jupyter/scipy-notebook:notebook-6.5.4

# Jupyter Configuration

Then your jupyter server should works locally right now. You still need some configuration before use.

# Set Password

By default, jupyter is authorized by token. However, getting token is not convinent for remote access because you need to go inside of the container and execute a command to get the token. If you want to change the vertification mean to password, at first time, you still need to login with token.

Jupyter Password Setting

1
2
3
4
5
6
# go into the container
docker exec -it jupyter-server jupyter server list
# then you will get a link with token
# enter the token and your password to the input
# restart the jupyter server
docker restart jupyter-server

After that, you could login in with password.

# Allow Remote Access

If you check the working directory after setting password, you will find there is a new file called jupyter_server_config.json.

It should look like this:

1
2
3
4
5
{
  "IdentityProvider": {
    "hashed_password": "your encrypted password string"
  }
}

However, jupyter server cannot be accessed remotely by default. You can enable it in the configuration.

1
2
3
4
5
6
7
8
{
  "ServerApp": {
    "allow_remote_access": true
  },
  "IdentityProvider": {
    "hashed_password": "your encrypted password string"
  }
}

Don’t forget to restart your jupyter:

1
docker restart jupyter-server

# Expose on the Internet

# Rathole Configuration

Then, your jupyter server is ready to be exposed. This time I will use the same tool as the last time, rathole. (If you don’t know what it is, you can check here)

# Reverse Proxy

Jupyter includes http and websocket, so you need to proxy for both.

Here is my configuration for reverse proxy using nginx:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
location / {
    # the port depends on your configuration of rathole
    proxy_pass http://127.0.0.1:8888; 
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    # websocket headers
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";

    proxy_buffering off;
}
comments powered by Disqus
Hosted by Cloudflare
Built with Hugo
Theme Stack designed by Jimmy