|
| 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