in IT contribution python parenting linux ~ read.

Running scipy-notebook in docker publicly with a password

I wanted to create a single-user jupyter notebook accessible from the internet with persistent storage for notebooks. The solution was as follows:

version: "3"

services:  
  jupyter:
    image: jupyter/scipy-notebook
    command: ['start-notebook.sh', '--ip', '0.0.0.0', '--no-browser', "--NotebookApp.password='sha1:xxPASSWORD_GENERATED_BY_IPYTHON_IN_TERMINALxxx'", "--NotebookApp.allow_origin='*'"]
    environment:
      - CHOWN_HOME=yes
      - NB_UID=1000
    expose: 
      - 8888
    user: root
    volumes:
      - jupyter-persistence:/home/jovyan/work

  nginx:  
    image: nginx
    links: 
      - jupyter
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
    ports:
      - 8888:8888

volumes:  
  jupyter-persistence:

And the nginx.conf

user  nginx;  
worker_processes  1;

error_log  /var/log/nginx/error.log warn;  
pid        /var/run/nginx.pid;


events {  
    worker_connections  1024;
}


http {

  include /etc/nginx/mime.types;
  default_type application/octet-stream;

  map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }

  upstream jupyter {
      server jupyter:8888 fail_timeout=0;
  }


    server {
        listen 8888;

        client_max_body_size 50M;
        server_name {YOUR_PUBLIC_IP};

        # Expose logs to "docker logs".
        # See https://github.com/nginxinc/docker-nginx/blob/master/Dockerfile#L12-L14
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        location / {
            proxy_pass http://jupyter;

            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
     location ~* /(api/kernels/[^/]+/(channels|iopub|shell|stdin)|terminals/websocket)/? {
            proxy_pass http://jupyter;

            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 support
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;

        }
    }
}

Then you just need docker-compose up to bring it alive.