> ## Documentation Index
> Fetch the complete documentation index at: https://phalanetwork.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Deploy a FastAPI Python Script in a TEE with dstack

> Deploy a Python application to dstack TEE infrastructure using existing containers

<div className="grid grid-cols-1 md:grid-cols-2 gap-4 mb-6">
  <div className="bg-blue-50 border border-blue-200 rounded-lg p-4">
    <h2 className="font-semibold text-blue-900 mb-2">What You'll Deploy</h2>
    <p className="mb-2">A secure Python Flask application with:</p>

    <ul className="list-disc list-inside text-sm text-blue-900 space-y-1">
      <li>Pre-built Flask web server</li>
      <li>TEE security verification</li>
      <li>Health monitoring endpoints</li>
      <li>No code changes required</li>
    </ul>

    <p className="mt-4 text-xs text-blue-700">
      This tutorial uses an existing Python container to demonstrate dstack deployment. Perfect for quick testing and learning.
    </p>
  </div>

  <div className="bg-green-50 border border-green-200 rounded-lg p-4">
    <h2 className="font-semibold text-green-900 mb-2">Prerequisites</h2>

    <ul className="list-disc list-inside text-sm text-green-900 space-y-1">
      <li>Access to a dstack host (see <a href="/docs/getting-started/install/app-jupiter-guide" className="underline">setup guide</a>)</li>
      <li>SSH tunnel to dstack dashboard</li>
      <li>Web browser for dashboard access</li>
      <li>5 minutes of your time</li>
    </ul>
  </div>
</div>

## Step 1: Open Tunnel & Access Dashboard

Connect to your dstack host and open the dashboard:

<div className="rounded border border-gray-300 p-4 flex flex-col space-y-3 w-full max-w-2xl mx-auto">
  <div className="flex items-center gap-2 mb-1">
    <span className="inline-flex items-center justify-center w-7 h-7 rounded-full border border-gray-400 text-gray-800 font-bold text-base bg-white">1</span>
    <h3 className="font-semibold text-gray-900 text-base tracking-tight">Open tunnel & access dashboard</h3>
  </div>

  <CodeBlock language="bash">
    {`ssh -L13680:localhost:13680 <user>@<host>`}
  </CodeBlock>

  <div className="text-xs text-gray-600">
    This command lets you access the dstack dashboard running on your remote host.
  </div>

  <div className="flex flex-col space-y-0.5">
    <span className="text-xs text-gray-600">Open in your browser:</span>
    <a href="http://127.0.0.1:13680/" className="text-gray-800 underline font-medium">[http://127.0.0.1:13680](http://127.0.0.1:13680)</a>
  </div>
</div>

## Step 2: Deploy Python Container

Navigate to your dstack dashboard and click **"Deploy a new instance"**.

### Add the Docker Compose Configuration

Copy this YAML and paste it into the "Docker Compose File" field:

<CodeBlock language="yaml">
  {`services:
    python-app:
      image: tiangolo/uvicorn-gunicorn-fastapi:python3.11
      ports:
        - 8080:80
      volumes:
        - /var/run/tappd.sock:/var/run/tappd.sock
      environment:
        - MODULE_NAME=main
        - VARIABLE_NAME=app
        - PORT=80
      command: >
        sh -c "
        echo 'from fastapi import FastAPI
        import os
        import time
        
        app = FastAPI(title=\"TEE Python App\", version=\"1.0.0\")
        
        @app.get(\"/\")
        def read_root():
            return {
                \"message\": \"Python app running in TEE\",
                \"secure\": True,
                \"tee_enabled\": True,
                \"timestamp\": time.time(),
                \"environment\": \"dstack-tee\"
            }
        
        @app.get(\"/health\")
        def health_check():
            return {
                \"status\": \"healthy\",
                \"tee_protected\": True,
                \"python_version\": \"3.11\",
                \"framework\": \"FastAPI\"
            }
        
        @app.get(\"/attestation\")
        def get_attestation():
            # In production, this would use the mounted tappd.sock
            return {
                \"attestation_available\": True,
                \"tee_type\": \"Intel TDX\",
                \"verified\": True,
                \"socket_path\": \"/var/run/tappd.sock\"
            }
        ' > /app/main.py && 
        uvicorn main:app --host 0.0.0.0 --port 80
        "
      user: root`}
</CodeBlock>

<div className="mt-8 max-w-2xl mx-auto">
  <div className="flex items-center space-x-2 mb-2">
    <span className="inline-flex items-center justify-center w-5 h-5 bg-blue-100 text-blue-600 rounded-full text-xs font-bold">2</span>
    <span className="text-sm text-gray-700 font-medium">Paste your Docker Compose YAML</span>
  </div>

  <div className="mt-2 text-xs text-gray-600 text-center">
    Click <strong className="text-blue-700">"Deploy"</strong> to start your Python application.
  </div>
</div>

## Step 3: Launch and Monitor

<div className="max-w-2xl mx-auto mt-8 space-y-6">
  <div className="rounded border border-gray-200 bg-white p-4 flex flex-col items-center">
    <div className="mt-3 text-base text-gray-800 font-medium text-center">
      1. Click <span className="text-blue-700 font-semibold">Launch</span> in the dashboard
    </div>

    <div className="mt-1 text-sm text-gray-600 text-center">
      dstack will start your Python FastAPI application. This usually takes 1–2 minutes.
    </div>
  </div>

  <div className="rounded border border-gray-200 bg-white p-4 flex flex-col items-center">
    <div className="mt-3 text-base text-gray-800 font-medium text-center">
      2. Monitor the deployment progress
    </div>

    <ul className="mt-2 text-sm text-gray-600 list-disc list-inside text-left space-y-1">
      <li>Check <span className="font-semibold">Serial Logs</span> for VM boot details</li>
      <li>View container status in the <span className="font-semibold">Containers</span> tab</li>
      <li>Wait for the application to show as "Running"</li>
    </ul>
  </div>
</div>

## Step 4: Test Your Deployment

After deployment, find your app's URL in the dashboard:

<CodeBlock language="text">
  https\://\<APP\_ID>-8080.app.\<host>:\<host\_port>
</CodeBlock>

### Test the Endpoints

```bash theme={null}
# Replace with your actual app URL
APP_URL="https://your-app-id-8080.app.your-host:port"

# Test main endpoint
curl $APP_URL/

# Test health check
curl $APP_URL/health

# Test TEE attestation endpoint
curl $APP_URL/attestation
```

### Expected Responses

**Main endpoint** (`/`):

```json theme={null}
{
  "message": "Python app running in TEE",
  "secure": true,
  "tee_enabled": true,
  "timestamp": 1703123456.789,
  "environment": "dstack-tee"
}
```

**Health endpoint** (`/health`):

```json theme={null}
{
  "status": "healthy",
  "tee_protected": true,
  "python_version": "3.11",
  "framework": "FastAPI"
}
```

## What's Next?

Your Python application is now running securely in a dstack TEE environment with:

✅ **Hardware Protection** - Application runs in Intel TDX secure environment\
✅ **Memory Encryption** - All data encrypted by CPU hardware\
✅ **Attestation Support** - Cryptographic proof of security available\
✅ **Production Ready** - HTTPS endpoints with automatic certificates

### Continue Learning

* **Try another language:** [JavaScript Tutorial](/docs/tutorials/javascript-example) | [Rust Tutorial](/docs/tutorials/rust-example)
* **Full deployment guide:** [Complete Installation](/docs/getting-started/first-deployment-steps/deploying-applications)
* **Architecture deep dive:** [How DStack Works](/docs/overview/what-is-dstack)
