Skip to content

Commit 9b4ac06

Browse files
kiwimarcMarc Cummings
andauthored
Adding apptainer support (#28)
* Added .gitignore and README * Updated README for formatting * Adding apptainer scripts --------- Co-authored-by: Marc Cummings <[email protected]>
1 parent aba7e85 commit 9b4ac06

File tree

11 files changed

+180
-0
lines changed

11 files changed

+180
-0
lines changed

apptainer/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.img
2+
*.sif

apptainer/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Apptainer for ezBIDS
2+
3+
This guide provides steps to convert Docker containers into Apptainer images and run them efficiently.
4+
5+
## Converting Docker to Apptainer
6+
7+
### Step 1: Build and Stop Docker Containers
8+
Run the following command to build and start the Docker containers:
9+
```sh
10+
docker-compose up --build
11+
```
12+
13+
Once the containers are running, stop them with:
14+
```sh
15+
docker-compose down
16+
```
17+
18+
### Step 2: Export Docker Images
19+
Save the running images to `.tar` files:
20+
```sh
21+
docker save -o api_image.tar brainlife_ezbids-api
22+
docker save -o handler_image.tar brainlife_ezbids-handler
23+
docker save -o ui_image.tar brainlife_ezbids-ui
24+
docker save -o telemetry_image.tar brainlife_ezbids-telemetry
25+
```
26+
For more information, refer to the Docker documentation: [Docker Save Command](https://docs.docker.com/reference/cli/docker/image/save/)
27+
28+
### Step 3: Convert Docker Images to Apptainer Images
29+
Use the following commands to convert each exported Docker image to an Apptainer `.sif` image:
30+
```sh
31+
apptainer build api.sif docker-archive:api_image.tar
32+
apptainer build handler.sif docker-archive:handler_image.tar
33+
apptainer build ui.sif docker-archive:ui_image.tar
34+
apptainer build telemetry.sif docker-archive:telemetry_image.tar
35+
```
36+
37+
## Usage
38+
39+
### Starting the Containers
40+
Run the following command to start the Apptainer containers:
41+
```sh
42+
./run_apptainer.sh
43+
```
44+
45+
### Stopping the Containers
46+
To stop the Apptainer containers, execute:
47+
```sh
48+
./stop_apptainer.sh
49+
```

apptainer/dev_apptainer.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env bash
2+
3+
cd ..
4+
5+
set -ex
6+
7+
# Parse arguments for authentication flag
8+
BRAINLIFE_AUTHENTICATION=true
9+
while getopts "d" flag; do
10+
case $flag in
11+
d)
12+
BRAINLIFE_AUTHENTICATION=false
13+
;;
14+
\?)
15+
;;
16+
esac
17+
done
18+
19+
export BRAINLIFE_AUTHENTICATION
20+
21+
# Update git submodules
22+
git submodule update --init --recursive
23+
24+
# Install dependencies for api and ui
25+
(cd api && npm install)
26+
(cd ui && npm install)
27+
28+
# Create necessary directories
29+
mkdir -p /tmp/upload
30+
mkdir -p /tmp/workdir
31+
32+
# Prepare husky (if using it for git hooks)
33+
npm run prepare-husky
34+
35+
# Generate keys (assuming this is a necessary setup step)
36+
./generate_keys.sh
37+

apptainer/mongo_host

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
127.0.0.1 localhost mongodb

apptainer/run_apptainer.sh

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env bash
2+
3+
./dev_apptainer.sh
4+
5+
if [ ! -e "mongodb.sif" ]; then
6+
apptainer build mongodb.sif docker://mongo:4.4.15
7+
fi
8+
9+
if [ ! -e "api_overlay.img" ]; then
10+
apptainer overlay create api_overlay.img
11+
fi
12+
13+
# Maybe not needed?
14+
# Does the handler create data else where than tmp?
15+
if [ ! -e "handler_overlay.img" ]; then
16+
apptainer overlay create handler_overlay.img
17+
fi
18+
19+
if [ ! -e "mongo_overlay.img" ]; then
20+
apptainer overlay create mongo_overlay.img
21+
fi
22+
23+
# Start MongoDB container
24+
echo "Starting MongoDB container..."
25+
apptainer instance run --hostname mongodb --overlay mongo_overlay.img mongodb.sif mongodb ./start_mongodb.sh
26+
27+
# Wait for MongoDB to be ready
28+
echo "Waiting for MongoDB to be ready..."
29+
sleep 10 # Adjust this based on your needs (or add a health check here)
30+
31+
# Start the API container
32+
echo "Starting API container..."
33+
apptainer instance run --overlay api_overlay.img --no-mount /etc/hosts --bind mongo_host:/etc/hosts api.sif api ./start_api.sh
34+
35+
# Wait for API to be ready
36+
echo "Waiting for API to be ready..."
37+
sleep 5 # Adjust based on your setup
38+
39+
# Start the Handler container
40+
echo "Starting Handler container..."
41+
apptainer instance run --overlay handler_overlay.img --no-mount /etc/hosts --bind mongo_host:/etc/hosts handler.sif handler ./start_handler.sh
42+
43+
# Wait for Handler to be ready (optional)
44+
echo "Waiting for Handler to be ready..."
45+
sleep 5 # Adjust as needed
46+
47+
# Start the UI container
48+
echo "Starting UI container..."
49+
apptainer instance run --env "VITE_APIHOST=http://localhost:8082" ui.sif ui ./start_ui.sh
50+
51+
# Start Telemetry container (if in development profile)
52+
if [ "$PROFILE" == "development" ]; then
53+
echo "Starting Telemetry container..."
54+
apptainer instance run telemetry.sif telementry ./start_telementry.sh
55+
fi
56+
57+
# Inform user that containers are running
58+
echo "All containers are running"

apptainer/start_api.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
3+
cd ..
4+
cd api/
5+
./dev.sh

apptainer/start_handler.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
3+
cd ..
4+
cd handler/
5+
pm2 start handler.js --attach --watch --ignore-watch "ui **/node_modules"

apptainer/start_mongodb.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
3+
mongod --dbpath /data/

apptainer/start_telementry.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
3+
cd ..
4+
cd telementry/
5+
npm start

apptainer/start_ui.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
3+
cd ..
4+
cd ui/
5+
npm run dev

0 commit comments

Comments
 (0)