Skip to main content
  1. Posts/

Create My Own `JupyterLab` workspace using Docker

Maybe this article will be close to TID(Today I Did)🤔.

Summary #

If you develop a project involving both Python and JavaScript and you want to write some code for testing tiny logic or function in each language, you can handle and test it using JupyterLab. 🧑‍💻

We use Docker to create a workspace for JupyterLab, while we use it don’t need to care about any dependencies (e.g. Python, Node.js, etc.) and we can use it on any OS. 🐳

It builds and starts as a web application, so if you deploy it on an exposed server, you can access it from anywhere and also collaborate with your team. 🌐

Details #

before starting, we will use jupyter/docker-stacks/base-notebook for the base image.

Directory structure #

1
2
3
4
5
6
7
8
9
.
├── Dockerfile
├── docker-compose.yml
├── requirements.txt  
├── config # config directory
│ └── jupyter_notebook_config.py  
└── workspace # workspace directory
├── python
└── javascript

Files #

Dockerfile #

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# Tag base image

FROM jupyter/base-notebook:ubuntu-22.04 as base

# Install from requirements.txt file

COPY --chown=${NB_UID}:${NB_GID} requirements.txt /tmp/
RUN pip install --quiet --no-cache-dir --requirement /tmp/requirements.txt && \
 fix-permissions "${CONDA_DIR}" && \
    fix-permissions "/home/${NB_USER}"

# Tag ijs image

FROM base as ijs

# Install and Start iJavascript kernel

RUN npm install -g ijavascript
RUN ijsinstall

In the above Dockerfile, we use base as a base image and install some packages from the requirements.txt file, which should locate in the same directory as Dockerfile. You can add some packages to the file if you want.

Then, we use ijs as a new image target and install and start the iJavascript kernel. In the docker-compose file below, we will use this image target.


docker-compose.yml #

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
version: "3"

# define networks

networks:
  jupyter_net:
    driver: bridge

services:
  jupyter:
    container_name: jupyter # build image from Dockerfile above
    build:
      context: .
      dockerfile: Dockerfile
      target: ijs # target image
    volumes: # mount workspace volume
      - ./workspace:/home/user/work # mount jupyter server config file
      - ./config/jupyter_server_config.py:/etc/jupyter/jupyter_server_config.py
    ports:
      - 8888:8888
    command: # you can pass config using files and command line
      jupyter-lab --allow-root --LabApp.token=''
    networks:
      - jupyter_net

In the docker-compose.yml file, we define a service named jupyter and build an image from the Dockerfile above.

We mount the workspace volume and the jupyter server config file to the container. And also, define a network named jupyter_net and connect the container to it.

We can handle jupyter server config using files and the command line, but I prefer to use the config file.


jupyter_server_config.py #

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
...

# set jupyter server config below

c = get_config() # noqa: F821
c.ServerApp.ip = "0.0.0.0"
c.ServerApp.open_browser = False

# disable xsrf check for vscode jupyter extension

c.ServerApp.disable_check_xsrf = True

# already set from the docker-compose command

# c.ServerApp.allow_root = True

# c.ServerApp.token = ""

...

In the jupyter_server_config.py file, we set the jupyter server config.
full code is here.


Run and test #

If ready to run, we can run the following command.

1
2
3
4
5
6
7
# Using docker-compose to build and start the container

$ docker compose up -d

# Check the container status in the log

$ docker compose log -f

if you run a jupyterlab server successfully, then in your web browser, you can access the jupyter lab at http://0.0.0.0:8888/lab.

Thanks to the jupyter docker stacks 🙇, we can use the jupyter lab with the following kernels. 🎯

References #