2020-11-25

Jupyter and Maven

After upgrading Jupyter Lab, I was editing Java files before compiling them with Maven.  To my surprise, I started getting error messages like:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project foo: Compilation failure: Compilation failure: 

[ERROR] /build/src/main/java/foo/bar/.ipynb_checkpoints/Foo-checkpoint.java:[36,8] class Foo is public, should be declared in a file named Foo.java

[ERROR] /build/src/main/java/foo/bar/Foo.java:[36,8] duplicate class: foo.bar.Foo

This happened because Jupyter Lab is auto-saving files (nice!) and Maven is attempting to compile them (not nice).  After much digging around and experimentation, I added the following to my pom.xml file as a child of the project element:

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                      <excludes>
                            <exclude>**/.ipynb_checkpoints/**</exclude>
                      </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

Running Jupyter Lab in Docker

Pre-requisites: Docker, Bourne shell

 I upgraded my Jupyter setup today and I though I would publish the script I am using to launch it:

#!/bin/sh
cd $HOME/Documents/jupyter
docker pull jupyter/datascience-notebook
docker build -t jupyter - <<EOD
FROM jupyter/datascience-notebook
RUN pip install --upgrade pip
RUN pip install rdflib
ENTRYPOINT ["jupyter", "lab", "--ip=0.0.0.0", "--allow-root"]
EOD
nohup docker run \
    --rm \
    --name jupyter \
    -v "$HOME"/.jupyter:/home/joyvan/.jupyter \
    -v "$PWD":/home/jovyan/work \
    -v $HOME/Documents/GitHub:/home/jovyan/work/GitHub \
    -p 0.0.0.0:8888:8888 \
    jupyter &
cat <<EOT
To find out the token, use:
    docker exec jupyter jupyter notebook list
To set a password, use:
    docker exec -it jupyter jupyter notebook password
To stop use:
    docker exec jupyter jupyter notebook stop
EOT

This launches a docker image that makes Jupyter Lab available on port 8888.  It maps three "volumes" into this image:
  • $HOME/.jupyter: This stores your Jupyter settings like theme and password.
  • $HOME/Documents/jupyter: Where you will keep your Jupyter Notebooks, found under work in the Jupyter file browser.
  • $HOME/Documents/GitHub: Where you keep your GitHub checkouts, found under work/GitHub in Jupyter.
Jupyter requires a token or password to access it.  You can determine the token with the command:

docker exec jupyter jupyter notebook list

It is more convenient to set a password, which you can do with the command:

docker exec -it jupyter jupyter notebook password

If you need to stop Jupyter, you can do so with the command:

docker exec jupyter jupyter notebook stop

A final tip: If you need to access other services on your localhost (say services from other Docker containers mapped to local ports) from Jupyter, using localhost or 127.0.0.1 will not work because Jupyter is running inside Docker.  Instead you should use host.docker.internal as the hostname instead.