# A Case Study on Virtual HPC Container Clusters and Machine Learning Applications
## 4 ML on Docker
### Anomaly Detection Application
#### 4.2.2 Design and Environment  
The application directory app, located in the same location as the compose.yml file of the virtual cluster, contains the folder src with Python scripts, the file requirements.txt with a list of required dependencies, and the folder data with training data. One of the files in this folder, used during this research, is named data.csv. It stores exactly 1820 samples taken from five containers, translating to 1805 training sequences with two attributes and a length of four time steps.  
The script for building the container image with the anomaly detection application is presented in Listing 1. It employs a three-stage division, as used in the presented Dockerfiles. The resulting image extends a chosen cluster node image. In the first stage, the dependencies needed in the second stage and the final image are installed: the Python interpreter version 3.9.2, and tools for building and installing the Python modules (distutils) used by the TensorFlow library.  
In the second stage, Python's package manager pip, the virtual environment tool venv, and cmake, necessary for building the Horovod module, are installed. Using venv, a virtual environment is created in the standard project folder /home/user/mpi, and its location is added to the beginning of the PATH environment variable. This ensures that tools from this environment have priority over those in the global namespace when invoked. Then, the requirements.txt file containing the application's dependency list is copied to the container. The package manager pip reads it and downloads the indicated packages. Horovod must be installed separately as it requires manual activation of MPI and TensorFlow support using environment variables prefixed with HORovOD_WITH. Additionally, one of the source files of the Horovod module required a small correction (line 20). Horovod automatically determines the installed MPI implementation based on the text displayed by the command mpirun -version. In newer versions of MPICH, this command displays, among other things, the word HYDRA, the name of the new process manager, which Horovod does not yet recognize. The if condition responsible for detecting the MPI implementation was modified so that Horovod also accepts this current manager.  
In the last stage, the PATH environment variable from the second stage is restored to reactivate the virtual environment. A command exporting this extended variable is added to the /etc/profile shell profile, ensuring it is always available, even after logging in via SSH.  
**Listing 1.** Dockerfile for the anomaly detection.  
ARG PROJECT_PATH=\"home/user/mpi\"  
ARG BASE_DISTRO=\"pikrog/mpich-node:1.1-bullseye-fast\"  
**HOOM ${BASE_DISTRO} AS base**  
**RUN apt-get install -y python3=3.9.2-3 python3-distutils**  
**HOOM base AS install**  
**RUN apt-get install -y python3-pip python3-venv cmake**  
ARG PROJECT_PATH  
**WORKDIR ${PROJECT_PATH}**  
**RUN python3 -m venv ~${PROJECT_PATH}/venv\"  
**ENV PATH=\"${PROJECT_PATH}/venv/bin:${PATH}\"**  
**COPY** app/requirements.txt.  
**RUN** pip install -r requirements.txt  
ARG HOROVOD_VERSION=\"0.27.0\"  
**RUN** HOROVOD_WITH_MPI=1 HOROVOD_WITH_TENSORFLOW=1 pip install ~  
horovod[keras]==${HOROVOD_VERSION}\"  
**RUN** sed -i \"$/elif_MPTCH'_in_output:/elif_MPTCH'_or_HYDRA'_in_output:/\" \"${PROJECT_PATH}/venv/lib/python3.9/site-packages/horovod/runner/mpi_run.py\"  
**HOOM** base  
ARG PROJECT_PATH  
**ENV PATH=\"${PROJECT_PATH}/venv/bin:${PATH}\"**  
**RUN** echo \"export_PATH=${PATH}\" >> /etc/profile**  
**WORKKDIR ${PROJECT_PATH}**  
**COPY** --from=install --chown=user [\"${PROJECT_PATH}/venv\", \"./venv \"]  
**COPY** --chown=user app.  
Finally, from the second stage to the third, the prepared virtual environment is copied, and the remaining application files (scripts and training data) are transferred from the host system.