DEV Community

Cover image for Serving Jupyter Notebook with Docker
eleloi
eleloi

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

EXPOSE 8888

# 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='0.0.0.0'"
Enter fullscreen mode Exit fullscreen mode

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

version: "3.3"

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

volumes:
  jupyter-data:
    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.

Conclusion

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.

Cheers!

Top comments (0)