> ## 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 Tokio API in a TEE with dstack

> Guide to deploying a Rust web API in a Trusted Execution Environment (TEE) using dstack and Docker Compose

<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, high-performance Rust web API with:</p>

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

    <p className="mt-4 text-xs text-blue-700">
      This tutorial uses an existing Rust container to demonstrate dstack deployment. Perfect for security-critical and high-performance workloads.
    </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 Rust 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:
    rust-app:
      image: rust:1.75-slim
      ports:
        - 8080:8000
      volumes:
        - /var/run/tappd.sock:/var/run/tappd.sock
      environment:
        - RUST_LOG=info
        - CARGO_TARGET_DIR=/tmp/target
      command: >
        sh -c "
        apt-get update && apt-get install -y pkg-config libssl-dev curl &&
        cargo init --name tee-rust-app --bin &&
        echo '[dependencies]
        tokio = { version = \"1.0\", features = [\"full\"] }
        warp = \"0.3\"
        serde = { version = \"1.0\", features = [\"derive\"] }
        serde_json = \"1.0\"
        chrono = { version = \"0.4\", features = [\"serde\"] }
        ' > Cargo.toml &&
        echo 'use warp::Filter;
        use serde::{Deserialize, Serialize};
        use std::time::Instant;
        
        #[derive(Serialize)]
        struct HealthResponse {
            message: String,
            secure: bool,
            tee_enabled: bool,
            timestamp: String,
            environment: String,
            rust_version: String,
        }
        
        #[derive(Serialize)]
        struct ComputeResponse {
            processed: bool,
            secure_computation: String,
            result: String,
            tee_protected: bool,
            performance_micros: u128,
        }
        
        #[derive(Deserialize)]
        struct ComputeRequest {
            data: String,
        }
        
        #[tokio::main]
        async fn main() {
            println!(\"TEE Rust application starting on port 8000\");
            
            let health = warp::path::end()
                .map(|| {
                    warp::reply::json(&HealthResponse {
                        message: \"Rust app running in TEE\".to_string(),
                        secure: true,
                        tee_enabled: true,
                        timestamp: chrono::Utc::now().to_rfc3339(),
                        environment: \"dstack-tee\".to_string(),
                        rust_version: env!(\"RUSTC_VERSION\", \"1.75\").to_string(),
                    })
                });
            
            let health_check = warp::path(\"health\")
                .map(|| {
                    warp::reply::json(&serde_json::json!({
                        \"status\": \"healthy\",
                        \"tee_protected\": true,
                        \"framework\": \"Warp\",
                        \"language\": \"Rust\"
                    }))
                });
            
            let attestation = warp::path(\"attestation\")
                .map(|| {
                    warp::reply::json(&serde_json::json!({
                        \"attestation_available\": true,
                        \"tee_type\": \"Intel TDX\",
                        \"verified\": true,
                        \"socket_path\": \"/var/run/tappd.sock\"
                    }))
                });
            
            let compute = warp::path(\"api\")
                .and(warp::path(\"compute\"))
                .and(warp::post())
                .and(warp::body::json())
                .map(|req: ComputeRequest| {
                    let start = Instant::now();
                    let result = format!(\"Securely processed: {}\", req.data);
                    let duration = start.elapsed();
                    
                    warp::reply::json(&ComputeResponse {
                        processed: true,
                        secure_computation: \"completed\".to_string(),
                        result,
                        tee_protected: true,
                        performance_micros: duration.as_micros(),
                    })
                });
            
            let routes = health
                .or(health_check)
                .or(attestation)
                .or(compute)
                .with(warp::cors().allow_any_origin());
            
            warp::serve(routes)
                .run(([0, 0, 0, 0], 8000))
                .await;
        }
        ' > src/main.rs &&
        cargo run --release
        "
      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 Rust 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 Rust Warp application. This usually takes 2–3 minutes (Rust compilation).
    </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 Rust compilation and application startup</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

# Test secure computation with performance metrics
curl -X POST $APP_URL/api/compute \
  -H "Content-Type: application/json" \
  -d '{"data": "high-performance secure computation"}'
```

### Expected Responses

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

```json theme={null}
{
  "message": "Rust app running in TEE",
  "secure": true,
  "tee_enabled": true,
  "timestamp": "2024-01-01T12:00:00.000Z",
  "environment": "dstack-tee",
  "rust_version": "1.75"
}
```

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

```json theme={null}
{
  "status": "healthy",
  "tee_protected": true,
  "framework": "Warp",
  "language": "Rust"
}
```

**Compute endpoint** (`/api/compute`):

```json theme={null}
{
  "processed": true,
  "secure_computation": "completed",
  "result": "Securely processed: high-performance secure computation",
  "tee_protected": true,
  "performance_micros": 42
}
```

### Performance Benchmarking

Test the high-performance capabilities:

```bash theme={null}
# Install Apache Bench for load testing (if available)
sudo apt-get install apache2-utils

# Benchmark the secure endpoint
ab -n 1000 -c 10 $APP_URL/health

# Test concurrent secure computations
echo '{"data": "benchmark test data"}' > payload.json
ab -n 100 -c 5 -p payload.json -T application/json $APP_URL/api/compute
```

## What's Next?

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

✅ **Zero-Overhead Security** - Hardware protection without performance cost\
✅ **High-Performance Computation** - Optimized Rust code in secure environment\
✅ **Memory Safety** - Rust's memory safety + hardware memory encryption\
✅ **Attestation Support** - Cryptographic proof of security available

### Continue Learning

* **Try another language:** [Python Tutorial](/docs/tutorials/python-example) | [JavaScript Tutorial](/docs/tutorials/javascript-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)
