Sunday, February 10, 2019

Running Jupyter Notebook from Docker Container (Installation from Scratch) - Part 1


  1. Note:This is an exhaustive illustration of how to install Jupyter Notebook inside a Docker Container. If you want to save time and just install Jupyter inside
    Docker without getting into the nitty gritty of Linux, Part 2 of the blog with same title is a better option

    Step 1. Create a Directory at an appropriate place in your Linux Environment with 755 privileges (Not advisable for production) 
mkdir -m 755 DockerExample 


  1. Step 2. Go inside the newly created Dir. This Directory would act as a  shared folder with the new Docker Container: 
cd DockerExample 


  1. Step 3. Now as our original goal is to run the Jupyter Notebook from the Docker container, we need to expose the port of Jupyter server to the host. Run below command in your terminal 
sudo docker run -v$(pwd):/sharedfolder --name=jNotebook -p 9294:8888 -i -t ubuntu 



  1. Now you must be inside Docker Container. Validate "Sharedfolder" folder by
     ls –a
    "sharedfolder" is the folder which is shared with Host OS/ 

  1. Step 4. Execute following cmd : 
apt-get update 
apt-get install python 
apt-get install python 3.7 
ln -s /usr/bin/python3.7 /usr/bin/python3 

  1. Step 5. Then validate the executable python path 
ls -l /usr/bin/python* 

  1. Step 6. In the above screenshot we can see that our python executable is pointing to Python 2.7. As we want to work with Python 3, lets change this behavior.  To make this change, we need to modify the .bashrc file and need to add a python alias (as seen in the below screen shot)
cd ~/ 
apt-get install vim 
vim .bashrc 
I 
Insert 
alias python=python3 


Esc 
:wq 
source ~/.bashrc 

  1. Step 7. Execute Python –version to validate python 3 

Step 8. Install python set up tools and pip  

apt-get install curl
apt-get install python3-distutils 
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py 
python get-pip.py 

Step 9. Install Jupyter Notebook


pip3 install jupyter  
pip3 install --upgrade "ipython[all]" 
  1. OR
apt-get install jupyter 

 Step 10. Run Jupyer Notebook
cd  ../..  
cd sharedfolder  
jupyter notebook --ip 0.0.0.0 --no-browser --allow-root 


On your host OS, open a web browser and go to: localhost:9294 . Set the password by giving token id in the above screenshot 


And here is my working Jupyter Notebook running from Docker Container 

No comments:

Post a Comment