# How to Set Up Docker Deployment with Redis and MongoDB for TradingAgents-CN

> Quickly deploy TradingAgents-CN using Docker Compose for a full trading system stack including FastAPI, Vue 3, MongoDB, and Redis in under five minutes. Learn how to set up your Docker deployment now.

- Repository: [hsliuping/TradingAgents-CN](https://github.com/hsliuping/tradingagents-cn)
- Tags: how-to-guide
- Published: 2026-02-16

---

**Deploy TradingAgents-CN using Docker Compose to run a complete trading system stack with FastAPI, Vue 3, MongoDB persistence, and Redis caching in under five minutes.**

TradingAgents-CN is an open-source quantitative trading platform that uses a containerized, front-back-end separation architecture. This guide walks you through the complete **Docker deployment with Redis and MongoDB for TradingAgents-CN**, referencing the actual [`docker-compose.yml`](https://github.com/hsliuping/TradingAgents-CN/blob/main/docker-compose.yml), Dockerfiles, and configuration files from the `hsliuping/TradingAgents-CN` repository.

## Architecture Overview

The Docker setup orchestrates four core services on a dedicated bridge network named **tradingagents-network**:

- **backend** – FastAPI application built from `Dockerfile.backend` and exposed on port `8000`.
- **frontend** – Vue 3 static site served by Nginx (port `3000` on host mapped to `80` in container).
- **mongodb** – Persistent MongoDB 4.4 instance for all trading data storage.
- **redis** – Redis 7 Alpine instance for caching and task queues.

The backend mounts host directories `./logs`, `./config`, and `./data` into `/app` for persistence, while MongoDB and Redis use named Docker volumes (`mongodb_data`, `redis_data`) to survive container restarts.

## Prerequisites

Before deploying, ensure your environment meets these requirements:

1. **Docker Engine** >= 20.10 and **Docker Compose** >= v2.0 installed.
2. **Git** to clone the repository.
3. At least **4GB RAM** and **10GB disk space** available for images and volumes.

## Configuration Files Deep Dive

### docker-compose.yml Structure

The [`docker-compose.yml`](https://github.com/hsliuping/TradingAgents-CN/blob/main/docker-compose.yml) file in the repository root defines service dependencies, health checks, and networking. Key features include:

- **Health checks** using `curl` for the backend, `redis-cli ping` for Redis, and MongoDB's built-in ping command.
- **Service dependencies** with `condition: service_healthy` ensure the backend waits for databases to be ready.
- **Environment variable injection** via `env_file: .env` for all services.

### Backend Dockerfile Specifics

`Dockerfile.backend` builds the `tradingagents-backend:v1.0.0-preview` image with several production considerations:

- Installs system dependencies including **pandoc**, **wkhtmltopdf**, and Chinese fonts for PDF report generation.
- Copies [`requirements.txt`](https://github.com/hsliuping/TradingAgents-CN/blob/main/requirements.txt) and installs Python dependencies.
- Sets `WORKDIR /app` and exposes port `8000`.
- Uses a non-root user for security in the final stage.

### Frontend Dockerfile

`Dockerfile.frontend` creates the `tradingagents-frontend:v1.0.0-preview` image:

- Builds the Vue 3 + Vite application in a Node.js environment.
- Copies the built static files to an **Nginx** Alpine image.
- Exposes port `80` inside the container, mapped to host port `3000`.

### Environment Variables (.env.docker)

The repository provides `.env.docker` as a template. Critical variables include:

- `TRADINGAGENTS_MONGODB_URL` – Connection string pointing to `mongodb` service.
- `TRADINGAGENTS_REDIS_URL` – Connection string pointing to `redis` service.
- `DOCKER_CONTAINER=true` – Flag indicating containerized execution.
- `VITE_API_BASE_URL=http://localhost:8000` – Frontend API endpoint configuration.

## Step-by-Step Deployment Guide

Follow these steps to deploy the complete stack:

1. **Clone the repository**

   ```bash
   git clone https://github.com/hsliuping/TradingAgents-CN.git
   cd TradingAgents-CN
   ```

2. **Prepare environment variables**

   ```bash
   cp .env.docker .env
   # Edit .env if you need to change default passwords or API keys

   ```

3. **Launch the core services**

   ```bash
   docker compose up -d
   ```

   This command builds the backend and frontend images, pulls MongoDB and Redis, creates the `tradingagents-network`, and starts all containers with health-check dependencies.

4. **Start optional management tools** (optional)

   ```bash
   docker compose --profile management up -d
   ```

   This adds **redis-commander** on port `8081` and **mongo-express** on port `8082`.

## Verifying the Deployment

Confirm each component is healthy using these checks:

- **Backend API**: `curl http://localhost:8000/api/health` should return `{"status":"ok"}`.
- **Frontend**: Navigate to `http://localhost:3000` to view the Vue 3 interface.
- **Redis**: `docker exec -it tradingagents-cn-redis-1 redis-cli -a tradingagents123 ping` returns `PONG`.
- **MongoDB**: `docker exec -it tradingagents-cn-mongodb-1 mongosh -u admin -p tradingagents123 --eval "db.adminCommand('ping')"` returns `{ ok: 1 }`.

## Connecting to Redis and MongoDB

Use these connection patterns in your Python backend code:

**Redis connection** (using the official client):

```python
import redis

redis_client = redis.from_url(
    "redis://:tradingagents123@redis:6379/0",
    decode_responses=True
)

```

**MongoDB connection** (using Motor for async support):

```python
from motor.motor_asyncio import AsyncIOMotorClient

client = AsyncIOMotorClient(
    "mongodb://admin:tradingagents123@mongodb:27017/tradingagents?authSource=admin"
)
db = client.tradingagents

```

## Optional Management UIs

The [`docker-compose.yml`](https://github.com/hsliuping/TradingAgents-CN/blob/main/docker-compose.yml) includes a `management` profile for database administration interfaces:

- **redis-commander**: Web UI for Redis data inspection available at `http://localhost:8081`.
- **mongo-express**: Web-based MongoDB admin interface available at `http://localhost:8082`.

Start these only when needed to reduce the attack surface in production environments.

## Summary

- **TradingAgents-CN** uses a four-service Docker architecture: **backend** (FastAPI), **frontend** (Vue 3/Nginx), **MongoDB**, and **Redis**.
- The [`docker-compose.yml`](https://github.com/hsliuping/TradingAgents-CN/blob/main/docker-compose.yml) file defines health checks, persistent volumes, and the `tradingagents-network` bridge for service discovery.
- Deployment requires only `git clone`, `cp .env.docker .env`, and `docker compose up -d` to run the full stack locally.
- Connection URLs use service names (`mongodb`, `redis`) as hostnames, with authentication enabled via the `.env` file.
- Optional management tools (redis-commander, mongo-express) are available under the `management` profile.

## Frequently Asked Questions

### What versions of MongoDB and Redis does TradingAgents-CN require?

TradingAgents-CN specifies **MongoDB 4.4** and **Redis 7 Alpine** in the [`docker-compose.yml`](https://github.com/hsliuping/TradingAgents-CN/blob/main/docker-compose.yml) file. These versions are tested for compatibility with the FastAPI backend and Motor async driver. You can modify the image tags in [`docker-compose.yml`](https://github.com/hsliuping/TradingAgents-CN/blob/main/docker-compose.yml) if you need different versions, but ensure the authentication and connection string formats remain compatible.

### How do I persist data when restarting the Docker containers?

The [`docker-compose.yml`](https://github.com/hsliuping/TradingAgents-CN/blob/main/docker-compose.yml) declares named volumes `mongodb_data` and `redis_data` for the databases, ensuring data survives container restarts. The backend persists logs, configuration, and application data through bind mounts (`./logs`, `./config`, `./data` mapped to `/app`). To completely reset the environment, run `docker compose down -v`, which removes both containers and named volumes.

### Can I deploy TradingAgents-CN without the frontend or management tools?

Yes. The [`docker-compose.yml`](https://github.com/hsliuping/TradingAgents-CN/blob/main/docker-compose.yml) separates services into profiles. Running `docker compose up -d` starts only the core services: backend, MongoDB, and Redis. The frontend starts only if explicitly included or if you run the default profile that includes it. The management tools (redis-commander and mongo-express) require the `--profile management` flag and are completely optional for production deployments.

### Where are the Docker configuration files located in the repository?

All Docker-related files reside in the repository root. The main orchestration file is [`docker-compose.yml`](https://github.com/hsliuping/TradingAgents-CN/blob/main/docker-compose.yml). Build instructions for the FastAPI backend are in `Dockerfile.backend`, while the Vue 3 frontend build is defined in `Dockerfile.frontend`. Environment variable templates are provided in `.env.docker`, which you should copy to `.env` before starting the stack. Database initialization scripts are located in [`scripts/mongo-init.js`](https://github.com/hsliuping/TradingAgents-CN/blob/main/scripts/mongo-init.js).