This repository contains a lightweight Beowulf cluster implementation using Docker containers and OpenMPI. This setup creates one master node and two worker nodes connected in a private Docker network, suitable for developing and testing MPI applications locally.
- Lightweight Docker containers based on Debian Bullseye Slim.
- OpenMPI pre-installed and configured.
- Passwordless SSH authentication between nodes for the
mpiuser. - Shared volume (
mpi_apps) for easy access and modification of MPI applications from the host. - Example applications included (C and C++).
- Ready for VS Code Remote-SSH connection.
- Docker
- Docker Compose
- Git (for cloning the repository)
- VS Code with the "Remote - SSH" extension (optional, for IDE integration)
-
Clone this repository:
git clone https://github.com/AliHamzaAzam/docker-mpi-cluster.git cd docker-mpi-cluster -
Build and start the cluster: This command builds the Docker images (if they don't exist) and starts the master and worker containers in the background.
docker-compose up -d --build
-
Connect to the master node as
mpiuser: It's important to connect asmpiuser, notroot, to run MPI commands correctly.docker exec -it -u mpiuser mpi_master bash -
Run the setup script (inside the container): This script generates the necessary MPI hostfile and tests SSH connectivity between nodes.
bash /home/mpiuser/setup.sh
(You only need to run this once after starting the containers, unless you restart them).
-
Compile the example MPI applications (inside the container): Navigate to the shared applications directory and use the Makefile.
cd /home/mpiuser/mpi_apps make clean # Optional: remove old executables make
-
Run the example applications (inside the container):
- C Example:
mpirun --hostfile /home/mpiuser/hostfile -np 3 /home/mpiuser/mpi_apps/hello_world
- C++ Example (prints hostname and IP):
mpirun --hostfile /home/mpiuser/hostfile -np 3 /home/mpiuser/mpi_apps/MPI
- OpenMP Example (runs on the master node):
The
makecommand already compiled the OpenMP test. Run it directly:/home/mpiuser/mpi_apps/openmp_test
- C Example:
- Place your MPI source code (C or C++) in the
mpi_appsdirectory on your host machine. - These files will automatically appear in
/home/mpiuser/mpi_appsinside all containers thanks to the shared volume. - Modify the
mpi_apps/Makefileto add rules for compiling your own applications. - Compile and run your applications from within the
mpi_mastercontainer (connected asmpiuser).
You can connect VS Code directly to the mpi_master container for a seamless development experience:
- Ensure the "Remote - SSH" extension (
ms-vscode-remote.remote-ssh) is installed in VS Code. - Open the Command Palette (Cmd+Shift+P or Ctrl+Shift+P).
- Select
Remote-SSH: Connect to Host.... - Choose
+ Add New SSH Host.... - Enter the command:
ssh mpiuser@localhost -p 2222 - Select your SSH configuration file (e.g.,
~/.ssh/config). - Connect to the newly added host (e.g.,
localhoston port 2222). - When prompted for the password, enter
mpiuser. - Once connected, open the folder
/home/mpiuser/mpi_appswithin VS Code to access your shared application files.
- To add more worker nodes, modify the
docker-compose.ymlfile and update thesetup.shscript accordingly. - To change the number of slots per node, edit the
setup.shscript where thehostfileis generated.
For instructions on extending this cluster across multiple physical machines, see the multi_laptop_setup.md file.
Note: The multi-laptop (multi-node) setup has not been tested yet. The current setup has been verified on a single machine using multiple containers. If you try this on multiple laptops or hosts, please share your experience or any issues you encounter!
To stop and remove the containers and network:
docker-compose down