Installing Jupyter Notebook on Docker

Jupyter Notebook is a widely used open-source web application that allows you to create and share live code, equations, visualizations, and narrative text documents. It’s a tool beloved by data scientists, researchers, and developers for its interactive capabilities. Running Jupyter Notebook on Docker adds a layer of flexibility by isolating the environment, ensuring consistency across systems, and simplifying dependency management. This article provides a step-by-step guide to installing Jupyter Notebook on Docker, complete with an example and a clear explanation.

Install Jupiter notebook on Docker

What is Docker and Why Use It for Jupyter Notebook?

Docker is a containerization platform that packages applications and their dependencies into lightweight containers. These containers run consistently across different environments, eliminating the "it works on my machine" problem.

Using Docker for Jupyter Notebook offers several advantages

  • Isolation: Each container runs in a separate environment, avoiding dependency clashes.
  • Reproducibility: You can easily share your environment setup with others by sharing the Docker image.
  • Simplicity: It’s straightforward to deploy and manage Jupyter environments.
  • Flexibility: Multiple versions of Jupyter can coexist on the same system.

Prerequisites

Before proceeding, ensure you have:

  1. Docker Installed: Download and install Docker from the official site.
  2. Basic Docker Knowledge: Familiarity with running Docker commands will be helpful.
  3. A Stable Internet Connection: Required to pull images from Docker Hub.

Step 1. Install Docker

If Docker is not already installed on your machine, follow these steps:

  1. Visit the Docker Desktop page.
  2. Download the installer suitable for your operating system (Windows, macOS, or Linux).
  3. Follow the installation instructions and start Docker after installation.

To verify Docker is installed, open a terminal or command prompt and run:

docker --version

You should see the Docker version number displayed.

Docker Version number

Step 2. Pull the Jupyter Docker Image

Docker Hub hosts prebuilt images for Jupyter Notebook. These images include everything you need to run Jupyter out of the box.

  1. Open your terminal or command prompt.
  2. Pull the jupyter/base-notebook image from Docker Hub:
    docker pull jupyter/base-notebook
    

    Docker Hub

Alternatively, you can choose specialized images like:

  • jupyter/minimal-notebook: Includes essential tools for data science.
  • jupyter/scipy-notebook: Adds scientific libraries like NumPy and SciPy.
  • jupyter/datascience-notebook: Packed with popular data science libraries such as pandas, scikit-learn, and TensorFlow.
    For example:
    docker pull jupyter/scipy-notebook
    
    Docker Pull Jupiter

Step 3. Run the Jupyter Notebook Container

After pulling the Docker image, you can run the Jupyter Notebook container. Here’s how:

  1. Run the following command to start the container:

    docker run -p 8888:8888 jupyter/base-notebook

    Run the Jupyter Notebook ContainerJupiter

    • The -p 8888:8888 flag maps the container's port 8888 to your local machine’s port 8888.
    • Replace jupyter/base-notebook with the name of the image you pulled if using a different variant.
  2. When the container starts, it generates a URL with a token, which allows access to the Jupyter Notebook interface. For example:

    http://127.0.0.1:8888/?token=<your-token>

    Jupiter Notebook interface

  3. Copy the URL from the terminal and paste it into your browser. You’ll see the Jupyter Notebook interface.Install Jupiter notebook on Docker

Step 4. Mount Local Directories (Optional)

You may want to access files from your local machine in the Jupyter environment. To do this, mount a local directory as a volume:

docker run -p 8888:8888 -v /path/to/your/local/folder:/home/jovyan/work jupyter/base-notebook
  • Replace /path/to/your/local/folder with the absolute path to the folder you want to mount.
  • Files in this folder will be accessible under /home/jovyan/work inside the container.

Step 5. Customize Your Docker Container (Optional)

If the base image doesn’t include all the packages you need, you can create a custom Dockerfile to extend it.

  1. Create a Dockerfile in your working directory:

    FROM jupyter/base-notebook RUN pip install matplotlib seaborn

    This example adds matplotlib and seaborn for data visualization.

  2. Build the custom image:

    docker build -t custom-jupyter-notebook .
  3. Run the custom container:

    docker run -p 8888:8888 custom-jupyter-notebook

Step 6. Stop and Remove the Container

  1. To stop the running container:
    docker ps
    

    Stop and Remove the Container

  2. This lists all running containers. Copy the CONTAINER ID for the Jupyter Notebook container, then run:
    docker stop <CONTAINER ID>

    Stop and Remove the Container

  3. To remove the container:
    docker rm <CONTAINER ID>
    Remove the Container

Example. Running Jupyter Notebook with Data Science Libraries

Let’s run a Jupyter Notebook preloaded with data science libraries:

  1. Pull the jupyter/datascience-notebook image:

    docker pull jupyter/datascience-notebook
  2. Start the container with a local directory mounted:

    docker run -p 8888:8888 -v $(pwd):/home/jovyan/work jupyter/datascience-notebook
  3. Access the notebook in your browser using the provided URL. You can now write Python code with libraries like pandas and matplotlib ready to go.

Common Issues and Troubleshooting

  1. Port Conflict: If port 8888 is already in use, specify a different port:

    docker run -p 9999:8888 jupyter/base-notebook

    Access it at http://127.0.0.1:9999.

  2. Token Not Found: If you lose the token, restart the container and check the logs for the URL.

  3. Permission Errors: Ensure your local folder has the correct permissions when mounting it as a volume.

Conclusion

Running Jupyter Notebook on Docker is an efficient way to create an isolated and reproducible environment for your projects. By following the steps outlined above, you can set up a robust environment tailored to your needs, whether you're working on machine learning, data analysis, or any other field.


Similar Articles
Ezmata Technologies Pvt Ltd
You manage your core business, while we manage your Infrastructure through ITaaS.