# Docker Development

This guide focuses on setting up a development environment for OpenAlgo  using Docker.

{% embed url="<https://www.youtube.com/watch?v=ixIJypG9oeg>" %}

### Prerequisites

* Docker Engine
* Docker Compose
* Git

## Essential .env Changes for Docker Setup&#x20;

### 1. Flask Host Configuration

```bash
# Change from 127.0.0.1 to 0.0.0.0 to allow external connections
FLASK_HOST_IP='0.0.0.0'  # Required for Docker
FLASK_PORT='5000'
```

### 2. WebSocket Configuration

```bash
# WebSocket server must bind to 0.0.0.0 inside Docker
WEBSOCKET_HOST='0.0.0.0'  # Required for Docker
WEBSOCKET_PORT='8765'
WEBSOCKET_URL='ws://localhost:8765'  # URL for clients connecting from host
```

### 3. ZeroMQ Configuration

```bash
# ZMQ must also bind to 0.0.0.0 for internal communication
ZMQ_HOST='0.0.0.0'  # Required for Docker
ZMQ_PORT='5555'
```

### Summary of Changes

#### From (Local Development):

```bash
FLASK_HOST_IP='127.0.0.1'
WEBSOCKET_HOST='127.0.0.1'
ZMQ_HOST='127.0.0.1'
```

#### To (Docker):

```bash
FLASK_HOST_IP='0.0.0.0'
WEBSOCKET_HOST='0.0.0.0'
ZMQ_HOST='0.0.0.0'
```

### Why These Changes?

1. **0.0.0.0 vs 127.0.0.1**:
   * `127.0.0.1` only allows connections from within the container
   * `0.0.0.0` allows connections from outside the container (host machine)
2. **WEBSOCKET\_URL**:
   * Remains as `ws://localhost:8765` because this is the URL clients use from the host machine
   * Docker maps the container's port to the host's localhost
3. **No other changes needed**:
   * All other settings (API keys, database URLs, etc.) remain the same
   * The docker-compose.yaml already maps the ports correctly

###

### Files Required

**1. Dockerfile**

```bash
FROM python:3.11-slim

WORKDIR /app

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Install system dependencies
RUN apt-get update \
    && apt-get install -y --no-install-recommends gcc python3-dev libpq-dev \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# Install Python dependencies
COPY requirements-nginx.txt .
RUN pip install --no-cache-dir -r requirements-nginx.txt
RUN pip install gunicorn eventlet>=0.24.1


# Copy project files
COPY . .

# Create directories and set permissions
RUN mkdir -p db logs && \
    chmod -R 777 db logs

# Command to run the application
CMD ["gunicorn", \
     "--bind", "0.0.0.0:5000", \
     "--worker-class", "eventlet", \
     "--workers", "1", \
     "--reload", \
     "--log-level", "debug", \
     "--access-logfile", "-", \
     "--error-logfile", "-", \
     "app:app"]
```

**2. docker-compose.yml**

```yaml
version: '3.8'

services:
  web:
    build: .
    ports:
      - "5000:5000"
    volumes:
      - .:/app
      - ./db:/app/db
    env_file:
      - .env
    environment:
      - FLASK_DEBUG=True
      - FLASK_ENV=development
      - DATABASE_URL=sqlite:///db/openalgo.db
    restart: unless-stopped
```

**3. .dockerignore**

```
**/__pycache__
**/*.pyc
**/*.pyo
**/*.pyd
.Python
env/
venv/
.env*
!.env.example
*.sqlite
.git
.gitignore
.docker
Dockerfile
README.md
*.sock
```

### Quick Start

1. **Create Environment File:**

   Copy `.sample.env` to `.env`:

   ```bash
   cp .sample.env .env
   ```
2. **Build and Start:**

   ```bash
   docker-compose up --build
   ```
3. **View Logs:**

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

### Development Features

* Hot reload enabled (code changes reflect immediately)
* Debug mode active
* Console logging
* Port 5000 exposed
* Volume mounting for live code updates

### Common Commands

```bash
# Start development server
docker-compose up

# Start in detached mode
docker-compose up -d

# View logs
docker-compose logs -f

# Stop containers
docker-compose down

# Rebuild after dependency changes
docker-compose up --build

# Enter container shell
docker-compose exec web bash

# Check container status
docker-compose ps
```

### Directory Structure

```
openalgo/
├── Dockerfile
├── docker-compose.yml
├── .dockerignore
├── .env
├── app.py
├── requirements-nginx.txt
└── db/
    └── openalgo.db
```

### Development Tips

1. **Live Reload:**
   * Code changes will automatically reload
   * Check logs for errors after changes
2. **Database Access:**
   * SQLite database persists in ./db directory
   * Can be accessed from both host and container
3. **Debugging:**
   * Logs are printed to console
   * Debug mode enables detailed error pages
4. **Dependencies:**
   * Add new packages to requirements-nginx.txt
   * Rebuild container after adding dependencies:

     ```bash
     docker-compose up --build
     ```

### Troubleshooting

1. **Port Already In Use:**

   ```bash
   # Check what's using port 5000
   sudo lsof -i :5000

   # Stop the container and restart
   docker-compose down
   docker-compose up
   ```
2. **Database Issues:**

   ```bash
   # Fix permissions if needed
   chmod -R 777 db/
   ```
3. **Container Won't Start:**

   ```bash
   # Check logs
   docker-compose logs

   # Remove container and try again
   docker-compose down
   docker-compose up --build
   ```
4. **Package Installation Issues:**

   ```bash
   # Rebuild without cache
   docker-compose build --no-cache
   docker-compose up
   ```

### Note

This configuration is optimized for development. For production deployment, additional security measures and optimizations would be necessary.
