[PYTHON] For those of you who don't know how to set a password with Jupyter on Docker

Introduction

I want to try machine learning → Tensorflow, right? I want to try it interactively on the Web → Jupyter, right? I want to build infrastructure easily → Docker, right? So, I tried it, but I got stuck in where to set the password, so I will write the solution

Assumed reader

Those who apply to the following

――I'm an infrastructure engineer, but I want to try machine learning --Docker I've been touching it recently, but I don't know the details --Python I'm touching it for a while, but I don't know much --Jupyter Is that okay? I don't know

Reasons to use Docker

Basically I would like to try it on my current MacBook Air, but obviously I don't have enough resources. So, if you want to do it firmly, the best solution is on the cloud such as AWS or GCP. I want to hit it locally and then bring it to the cloud, so the easiest and most popular thing is Docker. Building Jupyter is a hassle, so it's really easy to use a container that contains everything. (If you try to put it in Ansible or Chef, it will get stuck in various ways ...)

Get image

First, get the image. There are various types of Jupyter images below, so choose one according to your purpose. https://github.com/jupyter/docker-stacks

I want to use tensorflow this time, so I will use the following. https://github.com/jupyter/docker-stacks/tree/master/tensorflow-notebook

docker pull let's pull

$ docker pull jupyter/tensorflow-notebook
Unable to find image 'jupyter/tensorflow-notebook:latest' locally
latest: Pulling from jupyter/tensorflow-notebook
75a822cd7888: Pulling fs layer 
f74dd7a42a1f: Pulling fs layer 
7b03c47d9699: Pull complete 
ecef67eb4d88: Pull complete 
bd6c8cee2aa9: Pull complete 
8ecf884c467a: Pull complete 
7dc68d7c7ffd: Pull complete 
3369a456e292: Pull complete 
e626c4a34300: Pull complete 
619d5c673fb7: Pull complete 
eff746881b74: Pull complete 
fae5187ea9e1: Pull complete 
e1fd76e4adb2: Pull complete 
b5d2ba91f6a5: Pull complete 
5804e217f19f: Pull complete 
7719a427e59f: Pull complete 
33ea9d473883: Pull complete 
d28a865b24f6: Pull complete 
7e2fa13ce090: Pull complete 
15b63cba13fd: Pull complete 
adcb147a9782: Pull complete 
ce56ab01574f: Pull complete 
aa6a6ae0b01e: Pull complete 
2c54a9d73fcd: Pull complete 
47c2872f3870: Pull complete 
Digest: sha256:dd7b9e81c167647a87300043f21e3b37eee6d463815b86db9976c299cd4cc046
Status: Downloaded newer image for jupyter/tensorflow-notebook:latest
101e77006b40935607969dbad87e4540719ec55e66168d507f0ea8bb04ffe713

important point

You may get an error as shown below.

$ docker pull jupyter/tensorflow-notebook
Unable to find image 'jupyter/tensorflow-notebook:latest' locally
latest: Pulling from jupyter/tensorflow-notebook
75a822cd7888: Pulling fs layer 
f74dd7a42a1f: Pulling fs layer 
f74dd7a42a1f: Pull complete 
ecef67eb4d88: Download complete 
bd6c8cee2aa9: Download complete 
8ecf884c467a: Download complete 
7dc68d7c7ffd: Download complete 
3369a456e292: Download complete 
e626c4a34300: Download complete 
619d5c673fb7: Download complete 
eff746881b74: Download complete 
fae5187ea9e1: Download complete 
e1fd76e4adb2: Download complete 
b5d2ba91f6a5: Downloading 96.62 MB/660.8 MB
5804e217f19f: Download complete 
7719a427e59f: Downloading 126.5 MB/281.6 MB
33ea9d473883: Download complete 
d28a865b24f6: Downloading 314.1 MB/314.1 MB
7e2fa13ce090: Waiting 
15b63cba13fd: Waiting 
adcb147a9782: Waiting 
ce56ab01574f: Waiting 
aa6a6ae0b01e: Waiting 
2c54a9d73fcd: Waiting 
47c2872f3870: Waiting 
docker: write /var/lib/docker/tmp/GetImageBlob222925529: no space left on device.
See 'docker run --help'.

The cause is that the image is about 5G, so it seems that the capacity is insufficient. So, if you delete other images, it's OK (neural style is too big)

$ docker images
REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
poppen/trusty-ansible-serverspec   latest              e84a5c7341fd        10 weeks ago        691 MB
flyinprogrammer/serverspec         latest              1b34b62345b3        12 weeks ago        154.4 MB
somatic/torch-fast-neural-style    latest              97645d9c69e8        3 months ago        7.323 GB
amazon/amazon-ecs-agent            latest              a76825ffa321        3 months ago        10.84 MB
elenaalexandrovna/opencv-python3   latest              910fcd0fa477        4 months ago        594.7 MB
trafferty/docker-ipython-opencv    latest              686f355ae522        19 months ago       8.676 GB
$ docker rmi a76825ffa321 910fcd0fa477 97645d9c69e8
Untagged: amazon/amazon-ecs-agent:latest
Untagged: amazon/amazon-ecs-agent@sha256:391a45a5b69a8d9fe1844310c4cf90e82e31cb167a93eb94d5a49b2b2456ad46
Deleted: sha256:a76825ffa321686b2df70a0be52fe5ecd52006a347cee5f07635b188655a5e86
...

Start the container for hash value acquisition

$ docker run -it --rm -p 8888:8888 jupyter/tensorflow-notebook
1b11efc02512ab60a26909ced181a16e01db57160607bb5c54c2acacae30a20b
$ docker ps
CONTAINER ID        IMAGE                         COMMAND                  CREATED             STATUS              PORTS                    NAMES
fb31e1a2fda1        jupyter/tensorflow-notebook   "tini -- start-notebo"   6 seconds ago       Up 1 seconds        0.0.0.0:8888->8888/tcp   thirsty_kilby

At this rate, even if you access http: // localhost: 8888, you will only be looking at the password screen.

Get hash value

Launch bash in the container with docker exec

$ docker exec -it fb31e1a2fda1 /bin/bash

Run it from python, type in the password you want to set twice, and get the hash value of the password. In the example below, the hash value is sha1: hogehoge: fugafuga.

< python -c 'from notebook.auth import passwd;print(passwd())'               
Enter password: 
Verify password: 
sha1:hogehoge:fugafuga
jovyan@fb31e1a2fda1:~/work$ exit
exit

When attached

If you attach docker, it will attach to the standard input / output of start-notebook.sh that is executed when the container is started, and you cannot type python commands.

$ docker attach fb31e1a2fda1

★ I can't execute any command (just looking at the log)

ç^C[I 13:28:14.809 NotebookApp] Interrupted...
[I 13:28:14.810 NotebookApp] Shutting down kernels
$ 

Delete container

Delete this container as it is no longer used

$ docker ps
CONTAINER ID        IMAGE                         COMMAND                  CREATED             STATUS              PORTS                     NAMES
fb31e1a2fda1        jupyter/tensorflow-notebook   "tini -- start-notebo"   10 minutes ago      Up 10 minutes       0.0.0.0:32768->8888/tcp   tender_snyder
$ docker stop fb31e1a2fda1
fb31e1a2fda1
$ docker rm fb31e1a2fda1
fb31e1a2fda1
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

Start-up

Finally start the container with the password set Put the hash value you got earlier, sha1: hogehoge: fugafuga, into the value of the option NotebookApp.password in start-notebook.sh and execute it.

docker run -d -p 8888:8888 jupyter/tensorflow-notebook start-notebook.sh --NotebookApp.password='sha1:hogehoge:fugafuga'

Now you can go to http: // localhost: 8888 and enter the password you set above to log in. After that, please play with MNIST.

Digression

You can use the openssl command, right? I did the following, but it didn't work

$ echo [password] | openssl sha1

I thought sha1 was sha1, but why? Is the format different?

reference

Recommended Posts

For those of you who don't know how to set a password with Jupyter on Docker
How to set up a jupyter notebook on ssh destination (AWS EC2)
Join Azure Using Go ~ For those who want to start and know Azure with Go ~
Checklist on how to avoid turning the elements of numpy's array with for
[Introduction to Python] How to get the index of data with a for statement
For those who want to use Jupyter Notebook as soon as 1 second because they do not know the password
How to set a shared folder with the host OS in CentOS7 on VirtualBOX
The first step of machine learning ~ For those who want to implement with python ~
How to hold a hands-on seminar using Jupyter using docker
How to build a Django (python) environment on docker
For those who want to write Python with vim
A memo on how to overcome the difficult problem of capturing FX with AI
How to set up WSL2 on Windows 10 and create a study environment for Linux commands
How to know the number of GPUs from python ~ Notes on using multiprocessing with pytorch ~
I was in charge of maintaining the Fabric script, but I don't know.> <To those who
How to use GitHub on a multi-person server without a password
For those who want to start machine learning with TensorFlow2
How to display a list of installable versions with pyenv
How to register a package on PyPI (as of September 2017)
[Django Learned with the Devil's Blade] How to get a query set for forward / reverse reference
A modern environment building procedure for those who want to get started with Python right away
[Short sentence] easygui for those who want to use a simple GUI with Python very easily
You there who want to launch Rails on both the host and the client with Docker Compose!
How to quickly create a machine learning environment using Jupyter Notebook on macOS Sierra with anaconda
For those of you who glance at the log while learning with machine learning ~ Muscle training with LightGBM ~
How to set cron for regular Python scraping on Sakura server.
How to set a shortcut to switch full-width and half-width with IBus
A simple way to launch Jupyter Notebook / Lab and set a password
PyPI registration steps for those who want to make a PyPI debut
How to batch start a python program created with Jupyter notebook
A note on what you did to use Flycheck with Python
Here's a brief summary of how to get started with Django
How to create a label (mask) for segmentation with labelme (semantic segmentation mask)
How to run Jupyter and Spark on Mac with minimal settings
How to use jupyter notebook without polluting your environment with Docker
I don't know how to get query parameters on GAE / P
How to use Jupyter on the front end of supercomputer ITO
Node.js: How to kill offspring of a process started with child_process.fork ()
Neighborhood search for a set of (geographical coordinates) points on a sphere
Python techniques for those who want to get rid of beginners
It's time to install DB with Docker! DB installation for beginners on Docker
A memo for those who want quick socket communication with netcat
How to find if you don't know the Java installation directory
Commentary on unbiasedness and consistency for those who don't like it
How to run the practice code of the book "Creating a profitable AI with Python" on Google Colaboratory
What to do if you should have set an ssh key but are prompted for a password
How to delete a Docker container
[AtCoder for beginners] A story about the amount of calculation that you want to know very roughly
[EC2] How to take a screen capture of your smartphone with selenium
[Small story] How to save matplotlib graphs in a batch with Jupyter
For those who don't have Keras or TensorFlow on GPU on macOS Sierra
Understand how to display images on Jupyter (utilization of imshow / matplotlib of OpenCV)
[Introduction to Python] How to sort the contents of a list efficiently with list sort
I tried to draw a system configuration diagram with Diagrams on Docker
How to draw a vertical line on a heatmap drawn with Python seaborn
Links for handling jupyter notebooks on Docker on Ubuntu on VirtualBox on macOS with SSH
Verification of how to periodically execute a script on a Linux server on Windows
Try to solve a set problem of high school math with Python
Summary of how to build a LAMP + Wordpress environment with Sakura VPS
If you don't know how to draw the graph you want with matplotlib, it is convenient to look at the gallery.
How to deal with "^ [[A ^ [[B ^ [[C ^ [[D"] when you press the arrow keys when executing python on mac