Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
| hpc:tutorials:singularity:docker [2024/04/19 12:31] – gelöscht - Externe Bearbeitung (Unbekanntes Datum) 127.0.0.1 | hpc:tutorials:singularity:docker [2024/04/19 13:46] (aktuell) – | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| + | ==== Using Docker Images ==== | ||
| + | == HOW THIS TUTORIAL WORKS == | ||
| + | This tutorial is written in order to give a small insight in using Docker image files with Singularity. | ||
| + | It will cover, connecting to cluster, pulling the image and starting a database for a small test script. | ||
| + | |||
| + | **It is written as shell script itself and might just be executed on the cluster!** | ||
| + | |||
| + | You may also just copy paste the commands for a better understanding. | ||
| + | |||
| + | Please check [[hpc: | ||
| + | |||
| + | Thx for reading \\ | ||
| + | Jan Eberhardt | ||
| + | |||
| + | < | ||
| + | #! /bin/bash | ||
| + | # 0. Login on Frontend (you probably already did that) | ||
| + | # Use your TUB account and host gateway.hpc.tu-berlin.de | ||
| + | # ssh "< | ||
| + | |||
| + | # 1. Get Docker Image | ||
| + | # Go to your home directory and download the image via singularity. | ||
| + | # You must load the singularity module beforehand. | ||
| + | module load singularity/ | ||
| + | |||
| + | # Pulling docker images is done by Singularity' | ||
| + | # Singularity will automatically download the latest version of the image and rewrite it to a Singularity image file (sif) as " | ||
| + | # Therefore you will need write permission in your current working directory (which is why we changed into home). | ||
| + | cd | ||
| + | singularity pull " | ||
| + | |||
| + | # 2. Create Python environment [if using Python] | ||
| + | # Most Python projects will use open source libraries installed by pip. Since normal users are not allowed to do so, it is recommended to install | ||
| + | # pip packages in user space or in a virtual python environment. We would discourage you from using user space for installation since most packages | ||
| + | # you will only use once in your life and it is therefore cleaner to get an unique environment for each project of yours. | ||
| + | |||
| + | # a) Load python module. | ||
| + | module load python/ | ||
| + | |||
| + | # b) Create the environment | ||
| + | py=" | ||
| + | python3 -m venv ${py} | ||
| + | |||
| + | # c) Install required pip packages and updates and create start script. | ||
| + | # You may change the next steps accordingly to your project. | ||
| + | |||
| + | # EXAMPLE PIP PACKAGE LIST | ||
| + | #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | ||
| + | cat << EOL > " | ||
| + | pymongo> | ||
| + | EOL | ||
| + | #<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | ||
| + | |||
| + | # EXAMPLE PYTHON SCRIPT | ||
| + | #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | ||
| + | cat << EOS > ~/ | ||
| + | #! python3 | ||
| + | |||
| + | from pymongo import MongoClient | ||
| + | from pprint import pprint | ||
| + | from sys import argv, executable | ||
| + | from datetime import datetime | ||
| + | |||
| + | print(" | ||
| + | print(" | ||
| + | print(" | ||
| + | |||
| + | db_name = " | ||
| + | col_name = " | ||
| + | |||
| + | client = MongoClient(" | ||
| + | print(" | ||
| + | db = client[db_name] | ||
| + | col = db[col_name] | ||
| + | |||
| + | post = { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | |||
| + | print(" | ||
| + | post_record = col.insert_one(post) | ||
| + | pprint(post_record); | ||
| + | EOS | ||
| + | #<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | ||
| + | |||
| + | " | ||
| + | " | ||
| + | |||
| + | # 3. Create DB directory | ||
| + | dd=" | ||
| + | mkdir -p " | ||
| + | |||
| + | # 4. Start Server and run | ||
| + | # Use mongodb_start.sbatch in order to allocate resources for and to start mongodb server: | ||
| + | # This script will open up a server on a node and close it after the Python script finishes. | ||
| + | # | ||
| + | # We use the --exclusive switch of SBATCH in order to secure that port 27017 (default mongodb) is not in use. | ||
| + | # If you do not like to use an exclusive node you will have to either accept the risk that the command fails or | ||
| + | # to build a Singularity image of your own. | ||
| + | #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | ||
| + | cat << EOF > ./ | ||
| + | #!/bin/bash | ||
| + | # | ||
| + | # Start MongoDB docker image | ||
| + | # | ||
| + | #SBATCH --job-name=MongoDBStart | ||
| + | #SBATCH --partition=standard | ||
| + | #SBATCH --nodes=1 | ||
| + | #SBATCH --cpus-per-task=4 | ||
| + | #SBATCH --exclusive | ||
| + | # | ||
| + | |||
| + | #1 prepare | ||
| + | module load singularity/ | ||
| + | #- start instance (not the server) | ||
| + | #- In that way we can use the instance command to stop the database when script finishes. | ||
| + | singularity instance start --bind " | ||
| + | #- start server (by runscript) | ||
| + | #- It will generate a lot of output, better redirecting that to oblivion (1>/ | ||
| + | #- Also this call will lock your shell, avoided by ending the command with "&" | ||
| + | singularity run instance:// | ||
| + | |||
| + | #2 run program | ||
| + | #- wait for database server to run | ||
| + | sleep 5 | ||
| + | #- run script | ||
| + | ${py}/ | ||
| + | |||
| + | #3 stop database after script finishes | ||
| + | singularity instance stop mongodb | ||
| + | EOF | ||
| + | #<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | ||
| + | sbatch ./ | ||
| + | </ | ||