Tutorial: Jupyter Notebook inside Docker container
Motivation
- Currently on Ubuntu 22.04 where Python 3.10 is default
- For my Computer Vision assignment,
torch==1.8.1
andtorchvision==0.9.1
are required which are available with older versions of python - Installing older versions of python and managing pip was a mess!
- I timeshifted back to a week ago and coded up the below Dockerfile
Steps
- Install Docker
mkdir ~/Pydockerfiles && cd ~/Pydockerfiles
touch Dockerfile
touch run
touch requirements.txt
- Populate
requirements.txt
if you have any reference list chmod +x run
- Populate the two created files (code given below)
./run
- The notebook server link should open in the browser if all went well
Dockerfile
FROM python:3.8-slim-buster
WORKDIR /home
COPY requirements.txt requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
RUN pip install jupyter notebook
RUN apt-get update && apt-get install -y python3-opencv
CMD jupyter notebook --ip 0.0.0.0 --no-browser --allow-root
run
#!/bin/sh
docker build --tag python-docker .
docker run -it \
--rm \
-p 8888:8888 \
--name py-doc \
-v ~/{WORK_DIRECTORY_PATH}:/home/{WORK_DIRECTORY_NAME} \
python-docker
References
- Base for the initial Dockerfile: Medium article
- Getting jupyter notebook: LinkedIn article
- Accessing jupyter notebook via Docker: Stackoverflow post
- Issue with cv2-
ImportError: libGL.so.1: cannot open shared object file: No such file or directory
: Stackoverflow post - If you need to export notebook to pdf: Nbconvert Readthedocs