DEV Community

Cover image for Serving Jupyter Notebook with Docker

Posted on

Serving Jupyter Notebook with Docker

πŸ‘‹ Hi!

In this article, I'll walk you through the steps I took to revamp my aging Python applications and migrate them to a Docker container on a production server.

The Legacy Setup

Several years ago, I had a collection of Python applications that I shared with my coworkers using an old Windows server. This server hosted Jupyter Notebook with remote access enabled.

Today, to my surprise, I realised that some of these scripts were still being actively used by a group of users.

I decided to migrate the jupyter notebook server to a Docker container.

Creating the Dockerfile

We need a Dockerfile, which is essentially a recipe for building a Docker image. This file included all the necessary instructions to set up a Python environment, install the required libraries, and configure Jupyter Notebook.

# Use an official Python 3.11 image
FROM python:bookworm

# Need voila to serve the scripts as applications
RUN pip install voila
RUN pip install notebook

# Create the isolated dir on docker
RUN mkdir /jupyterbooks
WORKDIR /jupyterbooks


# Run jupyter with remote access enabled
CMD sh -c "jupyter notebook --no-browser --allow-root --IdentityProvider.token='yourSecretToken' --ServerApp.allow_origin='*' --ServerApp.port=8888 --ServerApp.allow_remote_access=True --ServerApp.ip=''"
Enter fullscreen mode Exit fullscreen mode

Also created a docker-compose.yml file to configure the container

version: "3.3"

    container_name: jupyter-service
      context: .
      dockerfile: Dockerfile
    image: jupyter-service
    restart: always
      - 8888:8888
      - jupyter-data:/jupyterbooks

    name: jupyter-data
Enter fullscreen mode Exit fullscreen mode

Copied the old scripts at the new docker jupyter-data volume with docker cp myNotebooks jupyter-service:/jupyterbooks/

And finally docker compose up -d does the trick.


I'ts amazing how two short files can configure such a service.

Now I can access the notebook with my token and the users can consume the applications without password through voila.


Top comments (0)