Building AI apps shouldn't require a PhD in MLOps. Dify gives you a visual workflow builder, RAG pipelines, multi-agent orchestration, and API endpoints—all self-hosted and under your control. This Dify AI platform setup guide walks you through deploying Dify with Docker Compose, connecting your LLM providers, and building your first AI workflow in under an hour.
Want more depth? Check our related guides: Dify AI Platform Setup: Build AI Apps Without the Infrastructure Nightmare, Dify AI Platform Setup: Advanced RAG Pipelines, Agents, and Production Workflows for Real Apps, and Dify AI Platform Setup: Build and Ship AI Apps Without the Infrastructure Nightmare.
What You'll Build
By the end of this guide, you'll have:
- Dify running self-hosted with Docker Compose
- PostgreSQL and Redis as backing services
- Weaviate or Qdrant as your vector database for RAG
- Connected OpenAI, Anthropic, or local Ollama LLM providers
- A working chatbot app with knowledge base retrieval
- API endpoints you can call from your existing applications
Prerequisites
Before we start, make sure you have:
- Docker 24.0+ and Docker Compose v2.24.0+ installed
- A Linux server (Ubuntu 22.04/24.04 LTS recommended) or macOS/Windows with WSL2
- At least 4GB RAM and 2 CPU cores (Dify is hungry—give it room)
- Ports 80 and 443 available (or custom ports if you know what you're doing)
- An API key from at least one LLM provider (OpenAI, Anthropic, Azure, or local Ollama)
gitandcurlinstalled
Dify bundles PostgreSQL, Redis, Weaviate, and nginx in its Docker Compose stack. You don't need to install these separately. Just make sure your server has the resources to run them all.
Step 1: Clone and Configure Dify
Dify's official repo includes a production-ready Docker Compose setup. Let's grab it and configure it.
1.1 Clone the Repository
git clone https://github.com/langgenius/dify.git
cd dify/docker
1.2 Copy Environment Configuration
cp .env.example .env
1.3 Edit Essential Variables
Open .env and set these minimum required values:
# App configuration
CONSOLE_API_URL=http://localhost
CONSOLE_WEB_URL=http://localhost
SERVICE_API_URL=http://localhost
APP_API_URL=http://localhost
APP_WEB_URL=http://localhost
# Admin credentials (set these before first startup)
INIT_PASSWORD=change_this_immediately_123
# LLM provider keys (add the ones you use)
OPENAI_API_KEY=sk-your-openai-key-here
ANTHROPIC_API_KEY=sk-ant-your-anthropic-key-here
# Vector database (weaviate is default, qdrant is lighter)
VECTOR_STORE=weaviate
Key notes:
INIT_PASSWORDcreates the first admin account. Set it before starting Dify or you'll need to reset via database.- API keys are optional at startup—you can add providers later in the UI. But having one ready means you can test immediately.
VECTOR_STOREdefaults to Weaviate. For smaller deployments, switch toqdrantto save RAM.
Step 2: Deploy with Docker Compose
Dify's Docker Compose includes all services: web UI, API, worker, PostgreSQL, Redis, Weaviate, and nginx. Start them all.
2.1 Start the Stack
docker compose up -d
2.2 Verify All Services Are Running
docker compose ps
You should see containers for api, worker, web, db, redis, weaviate, and nginx all in Up state. If any show Restarting or Exited, check logs:
docker compose logs -f api
docker compose logs -f worker
2.3 Access the UI
Open http://YOUR_SERVER_IP in a browser. Dify's nginx listens on port 80 by default. You'll see the setup wizard.
First login:
- Email:
[email protected](default, can be changed in settings later) - Password: The
INIT_PASSWORDyou set in.env
If you didn't set INIT_PASSWORD before first startup, Dify auto-generates one. Check the API logs for it.
Step 3: Connect Your LLM Providers
Dify supports 20+ model providers out of the box. Let's connect the ones you actually use.
3.1 Add OpenAI (or Anthropic, Azure, etc.)
- In Dify, go to Settings → Model Provider
- Click OpenAI (or your provider)
- Paste your API key
- Click Save and test the connection
Dify validates the key by making a test request. If it fails, check your key hasn't expired and your server can reach the provider's API endpoint.
3.2 Connect Local Ollama (Optional)
For privacy-sensitive deployments or cost control, run models locally with Ollama:
# Install Ollama
curl -fsSL https://ollama.com/install.sh | sh
# Pull a model (Llama 3.1 is solid)
ollama pull llama3.1
# Start Ollama server (listens on 11434)
ollama serve
In Dify's Model Provider settings:
- Add Ollama as a provider
- Base URL:
http://host.docker.internal:11434(macOS/Windows) orhttp://YOUR_SERVER_IP:11434(Linux) - Model name:
llama3.1
Note: On Linux, Ollama runs on the host network. Use the host's IP, not localhost, because Dify containers see their own loopback.
Step 4: Build Your First AI App
Dify has two app types: Chatbot (conversational) and Agent (autonomous with tools). Let's build a knowledge-base chatbot.
4.1 Create a Chatbot App
- Click Create Application → Chatbot
- Name it
Knowledge Assistant - Select your model (e.g., GPT-4o or Llama 3.1)
- Write a system prompt:
You are a helpful assistant that answers questions based on the provided knowledge base. If you don't know the answer, say so.
4.2 Upload Knowledge (RAG Setup)
- Go to the Knowledge tab
- Click Create Knowledge
- Name it
Product Docs - Upload files (PDF, TXT, Markdown, DOCX supported) or paste text
- Choose chunking strategy: Automatic (recommended) or custom split rules
- Click Save & Process
Dify chunks your documents, generates embeddings via your configured model, and stores vectors in Weaviate. Processing time depends on document size—typically seconds to minutes.
4.3 Connect Knowledge to Your App
- Back in your app, go to Context → Add Knowledge
- Select
Product Docs - Set retrieval mode: Semantic Search (vector similarity) or Full-Text (keyword)
- Adjust Top K (how many chunks to retrieve, default 3)
Test the chatbot. Ask a question about your uploaded docs. Dify retrieves relevant chunks, injects them into the prompt context, and the LLM answers based on your actual content—not hallucinated training data.
Step 5: Deploy with API and Embed
Dify apps aren't just chat UIs. Every app gets a REST API and embeddable widget.
5.1 Get Your API Credentials
- In your app, go to API tab
- Copy the API Key and API Endpoint
5.2 Call the API from Your Backend
curl -X POST 'http://YOUR_SERVER_IP/v1/chat-messages' \
-H 'Authorization: Bearer YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"inputs": {},
"query": "What are the pricing tiers?",
"response_mode": "blocking",
"conversation_id": "",
"user": "user-123"
}'
Response includes the LLM's answer, retrieved knowledge chunks, and token usage. Switch response_mode to streaming for SSE-style real-time output.
5.3 Embed the Chat Widget
Dify generates a JavaScript snippet for embedding:
<script>
window.difyChatbotConfig = {
token: 'YOUR_EMBED_TOKEN',
baseUrl: 'http://YOUR_SERVER_IP'
};
</script>
<script
src="http://YOUR_SERVER_IP/embed.min.js"
id="YOUR_EMBED_TOKEN"
defer>
</script>
Drop this into any HTML page. A floating chat widget appears, fully connected to your Dify backend with conversation history, file upload, and knowledge retrieval.
Step 6: Tips, Troubleshooting, and Production Hardening
6.1 Dify Won't Start
Check the most common culprits:
docker compose logs db | grep -i "error\|fatal"
docker compose logs weaviate | tail -n 20
Common issues:
- Port 80 already in use: Change nginx's port mapping in
docker-compose.yamlto8080:80 - Weaviate won't start: It needs significant RAM. On a 2GB VPS, switch to
VECTOR_STORE=qdrantin.envand restart - Database migration failures: Wipe the PostgreSQL volume and restart (loses data, but fixes corrupt state):
docker compose down -v && docker compose up -d
6.2 LLM Provider Connection Fails
- API key invalid: Regenerate at your provider's dashboard and re-enter in Dify
- Rate limited: OpenAI and Anthropic throttle free/tier-1 accounts. Add retries or upgrade
- Ollama not reachable: Verify Ollama is running with
curl http://localhost:11434/api/tags. Check firewall rules on Linux
6.3 Knowledge Retrieval Is Weak
If your RAG answers miss the mark:
- Chunk size too large: Smaller chunks (500-1000 tokens) improve precision. Adjust in Knowledge settings
- Top K too low: Increase from 3 to 5-7 for broader context retrieval
- Wrong embedding model: Dify uses the default model for embeddings. Switch to a better model (e.g.,
text-embedding-3-large) in settings - Hybrid search: Enable both semantic and keyword search for better coverage
6.4 Production Security Checklist
- Change the default admin password immediately after setup
- Enable HTTPS: Use Traefik or nginx with Let's Encrypt certificates. Dify's Docker stack includes a certbot container for automatic SSL
- Restrict API access: Use API key rotation and IP whitelisting if possible
- Backup your database: PostgreSQL holds all app configs, conversations, and knowledge metadata
- Environment isolation: Run separate Dify instances for dev/staging/prod
6.5 Backup and Restore
Backup PostgreSQL data:
docker compose exec db pg_dump -U postgres dify > dify-backup-$(date +%F).sql
Restore:
docker compose exec -T db psql -U postgres dify < dify-backup-2026-07-05.sql
Also backup the volumes/app-storage directory for uploaded files and knowledge documents.
6.6 Monitoring and Scaling
For production workloads:
- Scale the
workercontainer horizontally for concurrent knowledge processing - Monitor PostgreSQL connection pool—Dify is connection-hungry under load
- Use external Redis (AWS ElastiCache, Redis Cloud) for multi-node deployments
- Consider migrating Weaviate to a dedicated vector database cluster for large knowledge bases
Wrapping Up
You now have a fully self-hosted AI platform that can build chatbots, RAG pipelines, agent workflows, and API endpoints—without sending your data to third-party SaaS platforms or paying per-token fees to middlemen.
Dify's visual builder eliminates the boilerplate of chaining LLM calls, managing context windows, and building retrieval systems from scratch. For teams shipping AI features, it's the fastest path from idea to deployed API.
That said, production AI deployments at scale—custom model fine-tuning, multi-tenant isolation, advanced agent orchestration, and compliance requirements—need architecture beyond a single Docker host.
Need help designing a production Dify deployment, integrating custom models, or building advanced agent workflows for your product? Talk to our team—we've shipped AI-powered features for fintech, healthcare, and SaaS platforms using Dify and custom LLM infrastructure. We'll get your AI stack right.