Create a simple Numpy Jupyter Notebook using Docker

Machine learning and Data Science have become essential technologies in modern computing. Organizations adopt Data Analytics and Machine Learning techniques to predict sales and increase revenue. If you want to build machine learning models in a dynamic and contained environment, Docker containers provide an excellent solution.

You can build and run Machine Learning models easily inside Docker Containers with the help of Jupyter Notebooks. The containerized environment provides better version control of your Python or R libraries and packages used in Machine Learning projects.

In this article, we will discuss how to create a simple NumPy Jupyter Notebook inside a Docker Container. Following the same approach, you can install other useful libraries and include them in your Machine Learning projects.

Prerequisites

Ensure that Docker is installed and running on your system before proceeding with the following steps.

Step 1 − Run Jupyter Docker Image

Start by running the Docker Jupyter image by pulling it directly from Docker Registry. Use the following command to run the Jupyter image −

sudo docker run -d -p 8888:8888 jupyter/scipy-notebook

This command pulls the jupyter/scipy-notebook image and runs it in detached mode (-d), mapping port 8888 of the container to port 8888 of the host machine.

Step 2 − Verify Container Status

Check whether the Docker Container is running using the following command −

sudo docker ps -a

Copy the Container ID of the Jupyter Container from the output.

Step 3 − Get Jupyter Token

Retrieve the token associated with the Jupyter Notebook running inside the Docker Container −

sudo docker exec <container-id> jupyter notebook list

Replace <container-id> with your actual container ID. The output will look like this −

http://localhost:8888/?token=a37c45becfd981ffeb2fdca9b82419bd697e9a8b4b5bf25b :: /home/jovyan

Step 4 − Access Jupyter Notebook

Copy the localhost URL with the token and open it in your web browser. The Notebook is served from inside the Docker Container, and port 8888 is exposed to your host machine, making it accessible through your browser.

Step 5 − Create NumPy Notebook

The jupyter/scipy-notebook image includes essential Machine Learning packages like NumPy, SciPy, and others. Create a new Python 3 Notebook and give it a descriptive name.

Inside the notebook, enter the following NumPy code −

import numpy as np
np.mgrid[0:3, 0:3]

Execute the cell by pressing Shift + Enter. You should see a matrix output demonstrating NumPy's mesh grid functionality. Save the notebook after execution.

Step 6 − Test Persistence

Stop and restart the Docker Container to verify that the Jupyter Notebook persists −

sudo docker ps -a
sudo docker stop <container-id>
sudo docker start <container-id>

After restarting, access the Jupyter interface again using the same URL and token. Your previously created notebook should still exist, demonstrating data persistence within the container.

Key Benefits

Benefit Description
Isolation Projects run in isolated environments without affecting host system
Reproducibility Consistent environment across different machines and deployments
Version Control Better management of library versions and dependencies
Resource Management Controlled resource allocation for training large models

Conclusion

Docker containers provide an excellent platform for running Jupyter Notebooks with machine learning libraries like NumPy. This containerized approach ensures consistent environments, better version control, and easier deployment of machine learning projects. The combination of Docker and Jupyter Notebooks creates a powerful development ecosystem for data science workflows.

Updated on: 2026-03-17T09:01:38+05:30

531 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements