Docker Containers
MPI Programming
Automated SSH
Containerized Applications
Network Programming

How to run an MPI program across multiple docker containers without manually ssh'ing

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Message Passing Interface (MPI) is a standardized and portable message-passing system used to function on various parallel computing architectures. In a modern development environment, Docker containers are often employed because they can encapsulate the MPI environment, ensuring consistency regardless of the underlying host system. Running an MPI program across multiple containers involves setting up several docker containers configured to communicate through MPI without needing to manually use SSH.

1. Setting up Docker Containers for MPI

To run MPI programs across multiple Docker containers, set up a custom Docker image that includes the MPI library. You can use existing MPI Docker images or create your own from a base image.

Dockerfile Example:

dockerfile
1FROM ubuntu:20.04
2
3# Install dependencies
4RUN apt-get update && apt-get install -y --no-install-recommends \
5    build-essential \
6    openssh-client openssh-server \
7    mpich
8
9# Setup SSH for MPI communication
10RUN mkdir /var/run/sshd
11RUN echo 'root:root' | chpasswd
12RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
13EXPOSE 22
14
15# Set up a working directory
16WORKDIR /app
17ADD . /app
18
19# Set default command
20CMD ["bash"]

This Dockerfile creates an image based on Ubuntu, installs MPI (using MPICH in this example), and configures SSH, which is necessary for the MPI processes to communicate across containers.

2. Running MPI Containers with Docker Compose

Docker Compose can be used to manage the deployment of multiple containers on a single host or across a Swarm. A minimal docker-compose.yml configuration for MPI might look like this:

docker-compose.yml Example:

yaml
1version: '3.7'
2
3services:
4  mpi_head:
5    build: .
6    ports:
7      - "22"
8    volumes:
9      - .:/app
10    command: ["sh", "-c", "service ssh start; sleep infinity"]
11
12  mpi_node:
13    build: .
14    depends_on: 
15      - mpi_head
16    command: ["sh", "-c", "service ssh start; sleep infinity"]
17

This configuration establishes mpi_head as the master node and mpi_node as the worker node.

3. Executing MPI Programs Across Containers

To execute an MPI program across the containers managed by Docker Compose:

Launching MPI Programs:

bash
1# Start all containers
2docker-compose up -d
3
4# Node configuration (assuming one master and two workers)
5docker-compose scale mpi_node=2
6
7# Execute MPI program (running on mpi_head)
8docker exec mpi_head mpiexec -n 3 -hosts mpi_head,mpi_node_1,mpi_node_2 /app/my_mpi_program

The mpiexec command initiates the MPI program across the machines specified in the -hosts option, distributing the execution across the available nodes.

4. Key Points Summary

FeatureDescription
DockerfileSets up MPI and SSH within container.
Docker ComposeManages multi-container MPI setup.
MPICHExample of MPI library used.
mpiexec commandUsed to run the MPI program across different nodes defined in the Docker environment.

5. Enhanced Considerations

  • Performance: Understand the networking performance between containers can vary and might not match the performance of native MPI environments.
  • Scalability: Depending on the system’s Docker configuration and host resources, the number of nodes can be scaled.
  • Security: MPI and SSH configurations should be secured pursuant to best practices, especially if the containers might be exposed to more extensive networks or internet.

By carefully setting up the Docker environment with MPI, managing Docker containers with Docker Compose, and executing MPI programs without manual SSH login, developers and researchers can harness the power of MPI within a robust, containerized environment.


Course illustration
Course illustration

All Rights Reserved.