My first application¶
Jupyter notebook¶
The goal here is to launch a web microservice application to run Python Notebooks:
- Get the
jupyter/minimal-notebook
image which is based on Ubuntu (you can also use a more complete but much heavier image like thejupyter/scipy-notebook
):
If the tag (here lab-3.6.3
) is omitted, the downloaded image will be the one with the default tag: latest
; in this case there is no need to specify it in the docker run
command.
- Run the image in detached mode (option:
-d
) and with network port publishing (container/host port mapping; option-p port_of_the_host:port_inside_container
):
Warning
For security concerns, especially for laptop connnected on public network, it is important to limit the connections to localhost by adding the 127.0.0.1
in front of the mapping port 8888:8888
.
- Read the standard (
STDOUT
) and error (STDERR
) output (docker logs
command):
Connect to the notebook by cliking on the link given by the last command, open a terminal and try:
(base) jovyan@495f0be775ce:~$ curl -L https://gitlab.in2p3.fr/lpnhe/formations/containers/-/raw/main/files/python_codes/hello_world.py
- Install the
curl
command in the container by logging as root in the container (docker exec
command):
user@host : docker exec -it --user root myjupyter bash
(base) root@495f0be775ce:~# apt-get update
(base) root@495f0be775ce:~# apt-get install curl
- Run the python programs in the example above: go to the notebook webpage, open a terminal and then retry the command:
(base) jovyan@495f0be775ce:~$ curl -L https://gitlab.in2p3.fr/lpnhe/formations/containers/-/raw/main/files/python_codes/hello_world.py -o hello_world.py
(base) jovyan@495f0be775ce:~$ curl -L https://gitlab.in2p3.fr/lpnhe/formations/containers/-/raw/main/files/python_codes/pi.py -o pi.py
(base) jovyan@495f0be775ce:~$ python hello_world.py
(base) jovyan@495f0be775ce:~$ pip install numpy matplotlib
(base) jovyan@495f0be775ce:~$ python pi.py
Manage the microservice¶
The container can be turned off with:
In the same manner, it can be started (restarted) by replacing stop
with start
(restart
), but the identification token will be renewed:
docker container start myjupyter
docker logs myjupyter 2>&1 | grep "http://127.0.0.1:8888" | tail -n1
By adding the option --restart always
to the docker run
command, the container is always restarted if it is stopped. If it is manually stopped, it is restarted only when Docker daemon restarts or the container itself is manually restarted. It can be interested for a permanent use:
docker run -d --restart always \
-v /some/path/on/host/:/home/jovyan/work \
-p 127.0.0.1:8888:8888 \
jupyter/minimal-notebook
Remark
Note that the binding will not work correctly if your uid
is not 1000
. The jupyter notebook container supports some options passed as environment variables via the -e
option. For instance to launch a notebook with the entire home directory mounted inside the container: