Install and run SNAP from Docker images
Container and Docker Fundamentals
Docker is a tool that allows developers to easily deploy their applications in a sandbox (called containers) to run on the host operating system i.e. Linux. The key benefit of Docker is that it allows users to package an application with all of its dependencies into a standardized unit for software development.
Unlike virtual machines, containers do not have high overhead and hence enable more efficient usage of the underlying system and resources.
A comprehensive introduction to Docker and containerization can be found at Docker Curriculum.
Create and run a container with SNAP on a Linux host
The ‘snap13_python’ Docker image
The following steps describe how to create and deploy a Docker image (named ‘snap13_python’) and run a corresponding Docker container which contains a Python environment (Python version 3.10) together with a SNAP 13 installation with Snappy/SNAPISTA. In priniple, apart from a slightly different set of installed packages, the Python environment in this container is a ‘Docker representation' of the 'esa-snap’ Python kernel which has been provided within the Copernicus CDSE Jupyter Lab environment.
The host machine where to run the container can, in principle, be any Linux machine with Docker installed (see below). In ideal case, the machine should provide a desktop environment such as a local Linux desktop computer, or a VM with a GUI which is remotely accessible e.g. via RDP. This would allow to use SNAP/Python in notebooks in a Jupyter Lab environment from a Web browser. This was shown above for CDSE and is also assumed in the steps below. However, within the container the SNAP-Python APIs could also be accessed and used from the command line running a Python prompt or dedicated Python scripts.
Terminology
To create and deploy a Docker container, a Docker image needs to be built manually or downloaded from a registry such as Docker Hub or the Red Hat http://quay.io Container Registry. The image is the blueprint of the application (here: Python, SNAP) and forms the basis of the container. In return, the container is created from the image by a docker run command provided by the Docker installation.
Installation of the Docker software
The installation of the Docker software is a multi-step procedure and is not described here in detail. A step-by-step guide can again be found at Docker Curriculum.
In principle, a Docker installation and creation of images can be done on any machine and not even necessarily on Linux.
Creation of the Docker image
The Docker image has been created in advance from a Dockerfile , which is a simple text file that contains a list of commands that the Docker client calls while creating an image. These commands are similar or even equivalent to Linux commands. Details are not relevant for using the Docker image, but interested users can find the Dockerfileand also a couple of Jupyter example notebooks with small test data samples at the SNAP Docker repository.
Get the ‘snap13_python’ Docker image from the repository
The Docker image has been made publicly available on the Red Hat http://quay.io registry and can be retrieved by an Ubuntu user in a terminal window by the command:
$ docker pull quay.io/bcdev/snap13_python
The download may take a little while, download progress is listed. After completion, the image should be visible in the list of images:
$ docker images
Then the image can be run in a container with:
$ docker run --rm -p 8888:8888 quay.io/bcdev/snap13_python
Running the command as sudo might be necessary. Various log output is written, i.e., a Jupyter Lab server is automatically being started at container startup. The Jupyter Lab environment is then served at the URL printed to the terminal. It has the form
$ http://127.0.0.1:8888/lab?token=<token>
Access and run SNAP gpt from command line within the container
On the host, open a new terminal window and get the Docker container ID from
$ docker ps
Then type:
$ docker exec -it <CONTAINER_ID> /bin/bash
This will provide a bash command prompt within the container, like:
(base) mambauser@676cc734d67a:~$
The SNAP software is located in /home/mambauser/esa-snap, thus gpt can be used as in any other Linux environment:
(base) mambauser@676cc734d67a:~$ /home/mambauser/esa-snap/bin/snap/gpt -h
(base) mambauser@676cc734d67a:~$ /home/mambauser/esa-snap/bin/snap/gpt -h <operator>
(base) mambauser@676cc734d67a:~$ /home/mambauser/esa-snap/bin/snap/gpt <my_graph.xml>
etc.
Run Python from command line within the container
As gpt, you can also run Python from command line inside the Docker container:
(base) mambauser@676cc734d67a:~$ which python
/opt/conda/bin/python
(base) mambauser@676cc734d67a:~$ python
(base) mambauser@676cc734d67a:~$ python
Python 3.10.14 | packaged by conda-forge | (main, Mar 20 2024, 12:45:18) [GCC 12.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import esa_snappy
>>> import snapista
>>> ...Access Jupyter Lab in a Web browser
Open a Web browser on the host machine with the URL listed in the previos step. A Jupyter Lab web page should be displayed like this:
Access and run the SNAP Python functionalities in a Jupyter Lab environment from a web browser
For a quick test of the environment, open a new notebook on that Jupyter Lab web page and run some Snappy/SNAPISTA imports in a cell, like:
If this is successful, other notebooks using Snappy/SNAPISTA can be created or imported from outside into the container and run in this environment.
Copy files from the host computer to the container or vice versa
With a Jupyter Lab environment available as shown above, you can conveniently use its upload/download functionality. From command line on the host it works like:
From container to host:
$ docker ps
$ docker cp <CONTAINER_ID>:/file/path/within/container /host/path/to/dest_folder
From host to container:
$ docker cp /file/path/on/host <CONTAINER_ID>:/path/to/dest_folder
The ‘snap13’ Docker image
In addition to the Docker image including Python as described above, a second Docker image named ‘snap13’ is provided which does not contain a Python installation, but allows to launch the SNAP Desktop GUI within a running container. However, for this setup the following points should first be noted:
In general we do not recommend to run the SNAP GUI from a SNAP installation inside a Docker container. The Docker image/container represents an additional layer which produces overhead and may cause additional problems, and in return it does not really provide any benefit, thus we always recommend to install SNAP from the standard installers on a host with GUI capabilities. Running SNAP GUI inside a Docker container should just be regarded as a workaround if GUI capabilities are (for whatever reason) not available on the considered host.
To get the GUI started in a container at all, the setup is a bit more complex. Further prerequisites are required, see below.
The GUI operation inside a container has obviously some limitations. A currently known issue is that the WorldWind Analysis tools are not working because underlying native libraries are not correctly accessed/used inside the container. This issues has been investigated but could not yet be resolved.
The ‘snap_13’ Docker image has also been made publicly available on the Red Hat http://quay.io registry and can again be retrieved by an Ubuntu user in a terminal window by the command:
$ docker pull quay.io/bcdev/snap13
After completion, the image should again be visible in the list of images:
$ docker images
Before the ‘snap_13’ image can be run in a container, the following prerequisites are required to launch the SNAP GUI inside the container:
$ xhost +local:docker # establishes connection to the X server
$ export XAUTHORITY=$HOME/.Xauthority # ensures that authentication for X sessions is set up
Now the image can be run in a container with a couple of additional parameters:
$ docker run --rm -ti --net=host -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix -v $XAUTHORITY:/tmp/.XAuthority -e XAUTHORITY=/tmp/.XAuthority quay.io/bcdev/snap13
Again, running the command as sudo might be necessary. After successful execution, a bash session is set up inside the container. The SNAP software is located in /opt/esa-snap, and the SNAP Desktop GUI can be started with:
mambauser@676cc734d67a:~$ /opt/esa-snap/bin/snap