Skip to content

Commit b48df8c

Browse files
authored
Merge pull request #15 from Dstack-TEE/prelanch-script
Add example prelaunch-script
2 parents 4e2326b + a18b943 commit b48df8c

3 files changed

Lines changed: 56 additions & 0 deletions

File tree

prelaunch-script/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# DStack Pre-launch Script Example
2+
3+
This directory provides an example of a pre-launch script for the DStack Application. Introduced in Dstack v0.3.5 (as detailed in [#94](https://github.com/Dstack-TEE/dstack/pull/94)), this feature allows the application to perform preliminary setup steps before initiating Docker Compose. The pre-launch script's content is specified in the `pre_launch_script` section of the `app-compose.json` file. The `prelaunch.sh` script demonstrates how to manage container initialization and configure the environment prior to launching your application.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
busybox:
3+
image: busybox:latest
4+
command: sh -c "cat /etc/motd"
5+
volumes:
6+
- /tapp/motd:/etc/motd
7+
restart: no

prelaunch-script/prelaunch.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# This is an example of how to write a pre-launch script of DStack App Compose.
2+
3+
# The script is run in /tapp directory. The app-compose.json file is in the same directory.
4+
5+
set -e
6+
7+
# We fully handle the docker compose logic in this script.
8+
echo "Extracting docker compose file"
9+
jq -j '.docker_compose_file' app-compose.json >docker-compose.yaml
10+
echo "Removing orphans"
11+
tdxctl remove-orphans -f docker-compose.yaml || true
12+
echo "Restarting docker"
13+
chmod +x /usr/bin/containerd-shim-runc-v2
14+
systemctl restart docker
15+
16+
# Login docker account
17+
echo "Logging into Docker Hub"
18+
tdxctl notify-host -e "boot.progress" -d "logging into docker hub" || true
19+
if [ -n "$DOCKER_USERNAME" ] && [ -n "$DOCKER_PASSWORD" ]; then
20+
if ! echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin; then
21+
tdxctl notify-host -e "boot.error" -d "failed to login to docker hub"
22+
exit 1
23+
fi
24+
fi
25+
26+
# Use a container to setup the environment
27+
echo "Setting up the environment"
28+
tdxctl notify-host -e "boot.progress" -d "setting up the environment" || true
29+
docker run \
30+
--rm \
31+
--name dstack-app-setup \
32+
-v /tapp:/tapp \
33+
-w /tapp \
34+
-v /var/run/docker.sock:/var/run/docker.sock \
35+
curlimages/curl:latest \
36+
-s https://raw.githubusercontent.com/Dstack-TEE/meta-dstack/refs/heads/main/meta-dstack/recipes-core/base-files/files/motd -o /tapp/motd
37+
38+
echo "Starting containers"
39+
tdxctl notify-host -e "boot.progress" -d "starting containers" || true
40+
if ! docker compose up -d; then
41+
tdxctl notify-host -e "boot.error" -d "failed to start containers"
42+
exit 1
43+
fi
44+
45+
# Use exit to skip the original docker compose handling
46+
exit 0

0 commit comments

Comments
 (0)