Build a jupyterhub server using the jupyterhub of dockerhub

Posted by pherrick on Tue, 08 Mar 2022 16:14:31 +0100

preface

Not xiaobaiwen, not xiaobaiwen, not xiaobaiwen!!!!
It needs a certain foundation.
This is only my stepping on the pit, not every step is very detailed. But the key points are very detailed.

Why do we use jupyterhub?

1. What is jupyterhub?

jupyterhub is * * * *, please Baidu by yourself. It is actually a jupyter with login. Support multi person online login.

2. What is jupyter?

Please Baidu for details. Simply understand that jupyter is a visual language editor. Generally speaking, it is to do data analysis, mining, machine learning, etc. Visual presentation results.

3. What is jupyterab?

Please continue to Baidu.

4. Summary

This Baidu, please continue Baidu. In a word, jupyterhub is the target we need to install.

The construction of jupyerhub points to the north

Installing jupyterhub using docker

1.1 why use docker?

I'm afraid that your random installation will lead to the chaos of the main server. I won't even fix it then. It's really not good to use docker. Delete the docker container and rebuild it.

1.2 installing docker

The network grabs a lot and Baidu itself.

1.3 docker downloading jupyterhub image

docker pull jupyterhub/jupyterhub

jupyterhub reference page

These are not difficulties. The details are also introduced on the reference page.

1.4 operating vessel

# Here -p 2082:8000 2082 is the primary server port and 8000 is the container server port. A port mapping is made
# If you expose the 2082 port to the outside, you can access it according to ip:2082
docker run -id -p 2082:8000 --name jupyterhub jupyterhub/jupyterhub jupyterhub

There's no problem running like this. But there's no service at all.

So let's solve these small problems.

1.5 enter the container to add a jupyterhub user

jupyterhub's default multi-user management uses linux user management.
So we can now add users to the container.

  • First, let's go into the container
    docker exec -it jupyterhub /bin/bash
    
  • Again, we add a new linux user – user1
    useradd user1
    
  • Then we change the password for user1 (there is no password by default, and I don't know the mechanism, so I change the password directly)
    passwd user1
    
    Then enter the password in the prompt box that appears, such as
    user1123
    user1123
  • Continue our crucial step
    The linux user's private folder is located in the / home directory
    For example, if I am user, our default login path is / home/user1, but we do not log in. The default login of docker container is root, so / home/user1 will not be generated in the / home folder, so we need to generate the directory ourselves
    mkdir /home/user1
    
    Even so, it doesn't work because linux has a permission mechanism. You can output the following code directly without paying attention to it
    cd /home
    chown user1 user1 -R
    
  • Still a crucial step
    Because jupyterhub is not installed with notebook by default, an error will occur in spaner (I can't remember this stupid setting. Installing jupyterhub results in not installing jupyter?)
    The solution is to install jupyter.
    pip install jupyterlab -i https://pypi.douban.com/simple
    Here I install jupyterab, which will install jupyterab by default. If you don't understand, follow my advice.

It's almost over here

If you still have questions, you might as well exit from the container
exit
Then restart the jupyterhub container. If not, don't toss around. If there is a problem, enter the following command on the main server

docker restart jupyterhub
docker exec -id jupyterhub jupyterhub

Now you can visit
route http://ip:2082
If you cannot access it, please follow the following steps:

  1. Is the firewall of linux turned off
  2. Whether the cloud service provider's network allows port 2082 (in fact, the above port 2082 can be replaced, but I am used to using this port)
  3. Leave a message about other problems because I haven't encountered them.

nginx reverse proxy jupyerhub service

1.1 what is nginx?

Baidu, later supplement (probably not)

1.2 what are the benefits of nginx?

I'm Baidu. The reason why I act as an agent is that I want to use ssl, and it's more comfortable to have a domain name. Remember the port or something. It's a bother~

1.3 reverse agent

The main reason is that the problem of websocket needs to be considered in nginx proxy, otherwise the kernel will not be connected. But the direct access port can.

1.4 nginx related codes

http {
    include       mime.types;
    default_type  application/octet-stream;
    # Here is the processing of websocket by nginx
    map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }
		server {
        listen 443 ssl;
        server_name www.baidu.com; # This is your domain name
        ssl_certificate /etc/letsencrypt/live/baidu.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/baidu.com/privkey.pem;
        access_log /home/nginx/log/drive.log; # access log, which I use when debugging, can be deleted
        location / {
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_pass http://localhost:2082; #  Because the docker container mapping 2082, you have to change it here
        }

    }
	# Jump route. There are at least five ways to jump from 80 to 443. Here is one
    server {
        listen 80;
        server_name www.baidu.com;
        rewrite ^(.*)$ https://www.baidu.com$1 permanent;
    }
}

Topics: Python Big Data Docker jupyter Nginx