How to Self-Host Dify: Step-by-Step Guide
This page may contain affiliate links. If you buy through them we may earn a commission at no extra cost to you.
Direct answer: self-hosting Dify gets you the full source-available platform, knowledge bases, agent workflows, and a built-in prompt IDE, running on your own infrastructure with Docker Compose in about fifteen minutes. This guide covers what the quick-start skips: setting a stable SECRET_KEY before your first launch, choosing a vector store you won't regret later, and the migration step that breaks most self-hosted instances after an upgrade.
What you need before you start
You need a server or VPS with Docker and Docker Compose v2 installed, plus openssl to generate a secret key. Dify isn't one lightweight container, it's a full stack: an API service, a background worker, the web frontend behind an included Nginx proxy, Postgres for application data, Redis for caching and queues, and a vector store for knowledge-base embeddings. Budget roughly 2 CPU cores and 4GB of RAM as a starting point for a small team instance running all of that together, and more if you swap in a heavier vector store or expect real concurrent usage.
Step 1: Clone the repo and prepare the Docker directory
Dify ships an official docker folder with a ready-to-edit Compose file:
git clone https://github.com/langgenius/dify.git
cd dify/docker
cp .env.example .env
Everything you configure from here lives in that .env file, so keep it somewhere you back up, not just on the server's disk.
Step 2: Set a fixed SECRET_KEY
This is the step most quick-start guides rush past, and it's the one that causes real confusion later. Dify uses SECRET_KEY to sign sessions and encrypt sensitive values. Leave it blank and the app generates one automatically on first boot, which is fine right up until you recreate the container without that exact value available, at which point existing sessions break and encrypted data becomes unreadable, a problem that looks like a login bug rather than a missing configuration value.
Generate one yourself and set it explicitly in .env:
openssl rand -base64 42
Paste the result into SECRET_KEY. Back this value up separately from your database dumps, the same way you'd handle any other credential, since losing it alongside your Postgres backup leaves you with nothing to recover sessions or decrypt stored secrets with.
Step 3: Decide on your vector store before loading data
Dify bundles Weaviate as the default vector store in its Docker Compose file, and for most small-to-mid instances it's a reasonable default that needs no extra setup. If you already run Qdrant, Milvus, or PGVector elsewhere, or you want to standardize on one vector database across multiple tools, you can point VECTOR_STORE at it instead with a one-line .env change. The catch: this decision doesn't migrate cleanly after the fact. Changing the vector store later doesn't carry your existing embeddings over, so every knowledge base you'd already built would need re-indexing from scratch. Pick before you upload real documents, not after.
| Weaviate (default) | Qdrant / Milvus / PGVector | |
|---|---|---|
| Setup effort | None, already bundled | One .env line, plus running the service yourself if not using an existing instance |
| Best fit | New instance, no existing vector infra | Teams standardizing on one vector database across tools |
| Switching later | N/A, it's the starting point | Requires re-indexing every knowledge base |
If you're running Qdrant already for another part of your stack, our how to self-host Qdrant guide covers setting that side up first.
Step 4: Launch the stack
From the docker directory, start everything with:
docker compose up -d
Give it a minute or two to initialize, since several services (Postgres, Redis, the vector store, the API) all need to come up and connect before the app is fully usable. Visit http://localhost or your server's address, and complete the first-run admin account setup.
Step 5: Put a real domain and TLS in front of it
Dify's Compose file includes its own Nginx container, but that's handling internal routing between services, not acting as a production-ready, TLS-terminating edge on its own out of the box. For anything beyond local testing, put your own reverse proxy, Caddy is the lower-effort option for automatic Let's Encrypt renewal, in front of the stack, pointed at the exposed web port, and firewall the server so only 80 and 443 are reachable externally. Never expose the raw service ports directly.
Step 6: Back up what actually matters
Three things need backing up, and they're not all in one place: the Postgres volume (your apps, conversations, and configuration), the vector store's data (your knowledge-base embeddings), and your uploaded files, stored locally by default or in S3-compatible storage if you've configured that instead. A nightly pg_dump covers the first. Back up your .env file, specifically the SECRET_KEY value, separately from that dump. Losing any one of the three alone is recoverable; losing the database without the vector store (or the reverse) leaves you with an app that runs but can't answer questions correctly.
Step 7: Upgrade correctly, not just by pulling a new image
When a new Dify release comes out, pulling fresh images and restarting isn't the whole upgrade. Database schema changes ship alongside new versions, and skipping the migration step is the most common way a self-hosted instance breaks right after an update, symptoms that look like a random bug but are really a mismatch between your old schema and the new code expecting it. Check the release notes for the specific migration command for that version before you upgrade, and take a fresh Postgres backup immediately before running it.
Common mistakes to avoid
Leaving SECRET_KEY unset and letting Dify generate one is the mistake we see most, it works until the container state changes and sessions or encrypted data break with no obvious cause. Choosing a vector store casually and loading real knowledge bases before confirming it's the right long-term choice is the second, since there's no clean migration path once you have real data in it. And treating an upgrade as a simple image pull, skipping the database migration step, is the third, and the one most likely to take down a working instance without warning.
What's next
Once your instance is running, our Dify vs Flowise comparison covers how this self-hosted setup stacks up against the closest alternative on setup speed and licensing, and our Dify alternatives roundup covers four more self-hosted options if you're still confirming Dify is the right platform for your knowledge base. If you're connecting a workflow automation layer on top rather than using Dify standalone, our n8n vs Dify comparison covers where that boundary actually sits. And if a Postgres-compatible vector store fits your stack better than Weaviate's default, our Qdrant vs Pinecone for AI agents comparison is worth reading before you commit to Step 3 above.
Frequently asked questions
Is self-hosting Dify actually free?
Yes, for the core product. Dify's source is available to self-host with no artificial cap on apps, workflows, or team members, and no feature gate versus its cloud tiers. Its license is fair-code style: free for internal use, but it restricts standing up your own hosted, multi-tenant version of Dify as a competing commercial service, which is worth reading before you plan to resell access to your instance.
How much server capacity does Dify actually need?
Plan for roughly 2 CPU cores and 4GB of RAM as a starting point for a small team instance, enough to run the API, worker, web, Postgres, Redis, and the bundled vector store together comfortably. That's a starting point, not a hard floor; confirm current numbers against Dify's own deployment docs before sizing a production server, and budget more if you swap in a heavier vector store like Milvus.
What happens if I never set a fixed SECRET_KEY?
Dify generates one automatically on first launch if you leave it blank, which works fine until the container is recreated without that exact value persisted somewhere. At that point, existing sessions and anything encrypted with the old key breaks, which looks like a confusing login or decryption bug rather than the configuration issue it actually is. Generate a fixed value yourself before your first docker compose up and back it up like a password.
Can I switch vector stores after I've already loaded data?
Not cleanly. Changing VECTOR_STORE in your .env file points new Dify instances at a different backend, but it doesn't migrate your existing embeddings over, you'd need to re-index your knowledge bases from scratch against the new store. Decide on Weaviate, Qdrant, Milvus, or PGVector before you load real documents, not after.