Create a simple Numpy Jupyter Notebook using Docker

Machine learning and Data Science have certainly become the new buzzword. Organizations are now trying to adopt Data Analytics and Machine learning techniques to predict their sales and to increase their revenue. No doubt, specializing machine learning techniques would surely give you an upperhand in today’s corporate world. If you want to build a machine learning model in a dynamic and contained environment, there can be no better option than using Docker containers.
You can build and run your Machine Learning models easily inside Docker Containers with the help of Jupyter Notebooks. In fact, the packed environment of a Docker Container provides better version control of your python or R libraries and packages that you would be using in your Machine Learning project.
In this article, we will discuss how to create a simple Numpy Jupyter Notebook inside a Docker Container. Following the same template, you can easily install other useful libraries and packages and include them in your Machine Learning project through Jupyter Notebooks.
Assuming that you have Docker installed on your system, let’s go ahead and see how to run a Jupyter Notebook inside the Docker Container.
To start with, run the Docker Jupyter image by pulling it directly from Docker Registry. You can use the following command to run the Jupyter image.
sudo docker run −d −p 8888:8888 jupyter/scipy-notebook
To check whether the Docker Container is running or not, you can use the following command −
sudo docker ps −a
Copy the Container Id of the Jupyter Container.
After that, you need to get the Token associated with the Jupyter Notebook running inside the Docker Container. To do this, you can use the following command −
sudo docker exec <container−id> jupyter notebook list
http://localhost:8888/?token=a37c45becfd981ffeb2fdca9b82419bd697e9
a8b4b5bf25b :: /home/raunak
This will generate a localhost URL along with the token Id. Copy the URL upto token Id and fire up your browser and open that link. The Notebook is being served inside the Docker Container and the port 8888 of the Docker Container is exposed to the port 8888 of the host machine. Hence, you can access the URL inside your host machine. This will open up the Notebook in the browser.
All the basic Machine Learning packages such as Numpy, scipy, etc. is already included inside the Docker Container.
Create a new Python3 Notebook and give it a name. Inside the notebook, type the following command.
import numpy as np
np.mgrid[0:3, 0:3]
Execute the notebook cell. You should see a matrix output. Now, save the notebook.
Stop the Docker Container and start it again to check whether the Jupyter Notebook still exists or not.
sudo docker ps −a
sudo docker stop <container−id>
sudo docker start <container−id>
You will find that the Jupyter Notebook you had created earlier still exists inside the Docker Container.
To conclude, it’s obvious that grabbing a skill such as Machine Learning or Data Science would surely boost your academic or corporate career however building projects using Machine Learning and deploying or running them inside Docker Containers not only gives you a perfect container and packaged environment, but it also helps you to ease up the process of training huge models as it provides you with ample resources and saves your system from taking up the load while you perform other tasks.
In this article, we saw the steps to run a Jupyter image from the Docker registry, how to access the notebook in the browser on your local machine, ran a numpy code snippet and verified whether the notebook persists on restarting the Container or not.