# Deploy with Docker Compose

import { Callout } from 'nextra/components'

This document provides instructions for deploying the entire Autoflow application using Docker Compose.

## Prerequisites

- Set up a TiDB cluster, you can use either:
  - [TiDB Cloud Serverless](https://docs.pingcap.com/tidbcloud/tidb-cloud-quickstart)(recommended)
  - [TiDB Self-Managed](https://docs.pingcap.com/tidb/stable/production-deployment-using-tiup)(>=v8.4).
- Install [Docker Compose](https://docs.docker.com/compose/install/).

## Deploy

1. Clone the repository:

    ```bash
    git clone https://github.com/pingcap/autoflow.git;
    cd autoflow/;
    ```

2. Copy and edit the `.env` file:

    ```bash
    cp .env.example .env
    vim .env # or use another text editor to edit this file
    ```

    Replace the following placeholders with your own values:
    - `SECRET_KEY`: you can generate a random secret key using:
      ```python
      `python3 -c "import secrets; print(secrets.token_urlsafe(32))"`
      ```
    - `TIDB_HOST`, `TIDB_USER`, `TIDB_PASSWORD` and `TIDB_DATABASE`: you can get these values from the TiDB cluster you set up before.
      - Note: if you are using a self-managed TiDB cluster, you need to set `TIDB_SSL=false`.
    - `EMBEDDING_MAX_TOKENS`: set them according to the embedding model you choose before, it can not be changed after the deployment.(We will remove it in the future, and move it to the admin panel)

3. Migrate the database schema:

    ```bash
    docker compose run backend /bin/sh -c "alembic upgrade head"
    ```

4. Bootstrap the database with initial data:

    ```bash
    # Use default admin credentials (admin@example.com with random password)
    docker compose run backend /bin/sh -c "python bootstrap.py"

    # Or specify a custom admin email
    docker compose run backend /bin/sh -c "python bootstrap.py --email new-admin@example.com"
    ```

    Running the bootstrap script creates an admin user. You can find the username and password in the output.

    ```bash
    # Reset admin password (random generated)
    docker compose run backend /bin/sh -c "python bootstrap.py -r"

    # Or specify a new password
    docker compose run backend /bin/sh -c "python bootstrap.py -r --password <new_password>"
    ```

5. Start the services:

    If you are using a SaaS embedding model, start the services with the following command:

    ```bash
    docker compose up
    ```

    If you want to use the built-in local embedding reranker, start the services with the following command:

    ```bash
    docker compose --profile local-embedding-reranker up
    ```

6. Done! Now you can open your browser and visit [http://localhost:3000](http://localhost:3000) locally

## Configuration

After you deploy the application, you need to initialize the application by following [quick start guide](./quick-start.mdx).

1. Set up the default [LLM model](./llm.mdx) in the **Models > LLMs** page.

2. Set up the default [Embedding model](./embedding-model.mdx) in the **Models > Embedding Models** page.

3. Add a new [Knowledge Base](./knowledge-base.mdx) in the **Knowledge Bases** page.

4. Configure default [Chat Engine](./chat-engine.mdx) and set up the new knowledge base as the retrieval database.

## Upgrade

This section will help you upgrade pingcap/autoflow to the new version.

Suppose you want to upgrade pingcap/autoflow from 0.3.0 to 0.3.1. Follow these steps:

1. Edit your docker-compose.yml file to use the new image version.

    ```yaml
    services:
      backend:
        image: tidbai/backend:0.3.1
      frontend:
        image: tidbai/frontend:0.3.1
      background:
        image: tidbai/backend:0.3.1
    ```

2. Pull the new image:

    ```bash
    docker compose pull
    ```

3. Migrate the database schema:

    ```bash
    docker compose run backend /bin/sh -c "alembic upgrade head"
    ```

4. Recreate the docker containers:

    ```bash
    docker compose up -d --force-recreate
    ```

5. Check the logs to ensure everything is working correctly:

    ```bash
    docker compose logs -f
    ```

6. Done!
