Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jupyter password and Docker

For changing / setting the password of a Jupyter server, I follow the instructions here:

http://jupyter-notebook.readthedocs.io/en/latest/public_server.html#preparing-a-hashed-password

I do this in my local ipython environment. One thing to note is that somehow I get different hashes every time I re-run the passwd() command for the same password, but I assume that's intended behavior.

Anyway. I get the hash, and then I have a line like this in a Dockerfile:

ENV PW_HASH="u'sha1:salt:hash'"

and in the start-up script for the jupyter notebook I have

echo "c.NotebookApp.password = ${PW_HASH}" >> ${CONFIG_PATH}

and then jupyter notebook --allow-root -y --no-browser --ip=0.0.0.0 --config=${CONFIG_PATH}

However, if I then run the docker container via

docker run -it -p 8888:8888 <container-name>

while it does start up jupyter and allows me to connect in my browser via localhost:8888, it won't accept the password I just set via its hash.

Strangely, it does work when I add the additional step of the SSL certificates (and go to https://localhost:8888). What's going on here?

PS: I know that having a password but no SSL is sketchy. I'm just testing it out step by step and wonder why it won't work without the SSL part.

like image 904
Lagerbaer Avatar asked Feb 19 '18 22:02

Lagerbaer


2 Answers

Found two possible solutions, I tested both and both works.

jupyter core : 4.6.3
jupyter-notebook : 6.0.3

Set password

"add option -e PASSWORD=password to set the environment variable. The set password is then the password for the jupyter login." [1] when docker run or

environment:
    - PASSWORD=password

when using Docker Compose. Then just enter password

Set token

"… There already exists an easy way of setting the token when starting one of the Jupyter notebook Docker containers: -e JUPYTER_TOKEN="easy; it's already there". In fact, things are even easier if you export JUPYTER_TOKEN='easy' in the local environment, and then start the container with docker run --rm -d --name democontainer -p 9999:8888 -e JUPYTER_TOKEN jupyter/base-notebook (which is equivalent to -e JUPYTER_TOKEN=$JUPYTER_TOKEN). You can then autolaunch into the notebook with open http://localhost:9999?token=${JUPYTER_TOKEN}. H/t @minrk for that…" [2]

when using Docker Compose

environment:
    - JUPYTER_TOKEN=easy

then run just http://localhost:9999?token=easy or automate more ...

[1] login password required to access jupyter notebook running in nvidia-docker container

[2] https://blog.ouseful.info/2019/02/05/on-not-faffing-around-with-jupyter-docker-container-auth-tokens/

like image 57
mimros Avatar answered Sep 20 '22 00:09

mimros


Create a hashed password, using the ipython terminal

from notebook.auth import passwd
passwd()

It will promote you to enter the passwd twice, and create the hashed password, and add following lines in the Docker file

RUN jupyter notebook --generate-config
RUN echo "c.NotebookApp.password='sha1:***'">>/root/.jupyter/jupyter_notebook_config.py

Since I am the only person using the notebook, I just sticked to the root.

like image 25
Nati Avatar answered Sep 18 '22 00:09

Nati