...

/

Docker Setup

Docker Setup

Learn how to set up the Dockerfile to run the Jupyter notebook on the Educative platform.

Docker on the Educative Platform

We use Docker to allow authors to create their custom environments to accommodate any language not supported by our platform.

Authors can create custom docker images that deploy on the Google Cloud and allow end-users to have access to these customized environments through our platform directly.

The Jargon

Base Image: An OS userspace.

Image: A read-only template with instructions for creating a Docker container.

Dockerfile: A simple file with no extension, named Dockerfile, is used to builds a Docker image, and specifies everything that will go in the application’s environment setup.

Container: An image when run, builds a container. This contains your application environment, basically where your application will be run.

Tarball

Press + to interact
-tarball
--Dockerfile
--config.py
--Files // Folder containing the all notebook files
---helloworld.ipynb
---plot.ipynb
  • Use your command line terminal to navigate to the directory where your Dockerfile is

    • Move all the .ipynb files your course needs into the Files directory.

    • Also move the config.py file into the main directory. The file has been provided below.

  • In your terminal, use the following command to turn these assets into a tarball:

Press + to interact
tar -czvf jupyter.tar.gz Dockerfile config.py Files

This command will take your Dockerfile, Files folder, and config.py file as inputs and create a single tarball file as output. The tarball file will be named jupyter.tar.gz.

As you can see, the Dockerfile must be nested directly inside the tarball (not inside any child folder).

This allows Educative to access the Dockerfile directly and create a docker container for our application’s environment.

Note: All files can be found for download in the appendix of this lesson.

Now we'll dive into the contents of our Dockerfile and config.py.

Dockerfile

Note: You have to perform this step locally (on your computer).

  • Create a Dockerfile by adding the following script to an empty text file saved as Dockerfile (with no extension):

# Base container image, which has the Ubuntu Linux distribution and Nodejs pre-installed
FROM ubuntu:20.04
# Install the following packages
RUN apt-get update && apt-get install software-properties-common -y &&\
add-apt-repository ppa:deadsnakes/ppa && apt-get update &&\
apt-get install python3.6 -y &&apt install python3-pip -y &&\
pip3 install --upgrade pip && pip3 install jupyter &&\
mkdir /usr/local/notebooks
# Install any Python modules required in the notebook
RUN pip3 install pandas && pip3 install numpy && pip3 install seaborn &&\
pip3 install matplotlib && pip3 install sklearn
# Add configuration file
ADD config.py /root/.jupyter/jupyter_notebook_config.py
# Add ipynb files
ADD ./Files /usr/local/notebooks/
The instructions in the Dockerfile are used to build a Docker image for your Jupyter notebooks

Explanation

Docker uses the instructions in your Dockerfile to build a Docker image. This image will be used to run your Jupyter notebooks. Let’s take a look at what our Dockerfile contains.

  • Line 2: We are using the latest version of ubuntu as the base image of our Dockerfile by using FROM ubuntu:20.04.

  • Lines 5–9: Ubuntu uses the package manager apt. This allows you to easily install any packages or software required in your environment.

    • apt-get update - This will check the dependencies of the packages you want and install any that are needed.

    • add-apt-repository ppa:deadsnakes/ppa

      • add-apt-repository: This will add the specified apt repository to the list.

      • ppa:deadsnakes/ppa: This will add the PPA deadsnakes repository, which contains the python3.6 package.

    • apt-get install python3.6: This will install Python 3.6.

    • install python3-pip -y: This will install the Python package manager pip which is needed in order to install JupyterLab.

    • pip3 install --upgrade pip: This will upgrade pip.

    • pip3 install jupyterlab: This will install the JupyterLab package for Python.

    • mkdir /usr/local/notebooks: This is the directory where you will add your ipynb files.

  • Lines 12 and 13: This will install any python modules that you need in your Jupyter notebooks.

  • Line 16: This will add the configuration file for your notebooks. It contains preferences such as the port number (set to 8080) and the notebook directory (/usr/local/notebooks).

  • Line 19: This will add the notebook books that are in your Files folder to the notebook directory (/usr/local/notebooks).

Uploading the tarball

Appendix

You can copy the above script into your Dockerfile, or you can download and use the Dockerfile provided below:

Dockerfile

The config.py file is also attached below:

config.py

For you convenience, the tarball has been provided below:

jupyter.tar.gz

Access this course and 1200+ top-rated courses and projects.