1. 环境准备和依赖检查
bash
# 检查 Docker 是否已安装
docker --version
# 检查 Docker Compose 是否可用
docker compose version
# 如果未安装 Docker,请先安装(以 Ubuntu 为例)
# 更新包索引
sudo apt update
# 安装 Docker 依赖
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
# 添加 Docker 官方 GPG 密钥
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# 添加 Docker 仓库
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
# 安装 Docker
sudo apt update
sudo apt install -y docker-ce
# 启动 Docker 服务
sudo systemctl start docker
sudo systemctl enable docker
# 将当前用户添加到 docker 组(避免每次使用 sudo)
sudo usermod -aG docker $USER
# 重新登录或执行以下命令使组更改生效
newgrp docker
2. 获取 Dify 代码
bash
# 克隆 Dify 仓库
git clone https://github.com/langgenius/dify.git
# 进入 docker 目录
cd dify/docker
# 如果需要安装特定版本(例如 v0.6.0),可以使用:
# git clone --branch v0.6.0 https://github.com/langgenius/dify.git
# 如果速度太慢,使用镜像地址克隆
# git clone https://bgithub.xyz/langgenius/dify.git
3. 配置和启动 Dify
bash
# 复制环境配置文件
cp .env.example .env
# 可选:编辑配置文件(如果需要修改端口等设置)
# nano .env 或 vim .env
# 启动所有服务
docker compose up -d
# 查看服务状态
docker compose ps
# 查看日志(可选)
docker compose logs -f
4. 完整的 docker-compose.yml 文件内容
bash
version: '3.8'
services:
web:
image: langgenius/dify-web:latest
container_name: dify-web
ports:
- "${NGINX_HTTP_PORT}:80"
- "${NGINX_HTTPS_PORT}:443"
depends_on:
- app
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d
- ./nginx/logs:/var/log/nginx
- upload_data:/app/api/storage
env_file:
- .env
networks:
- dify
app:
image: langgenius/dify-api:latest
container_name: dify-app
depends_on:
- db
- redis
volumes:
- upload_data:/app/api/storage
env_file:
- .env
environment:
- DB_HOST=db
- REDIS_HOST=redis
- CONSOLE_API_URL=http://web:80
- CONSOLE_WEB_URL=http://web:80
networks:
- dify
db:
image: postgres:16-alpine
container_name: dify-db
environment:
- POSTGRES_DB=${DB_NAME}
- POSTGRES_USER=${DB_USERNAME}
- POSTGRES_PASSWORD=${DB_PASSWORD}
volumes:
- db_data:/var/lib/postgresql/data
networks:
- dify
redis:
image: redis:6-alpine
container_name: dify-redis
volumes:
- redis_data:/data
networks:
- dify
volumes:
db_data:
redis_data:
upload_data:
networks:
dify:
driver: bridge
5. 环境配置文件 (.env) 示例
bash
# 数据库配置
DB_NAME=dify
DB_USERNAME=postgres
DB_PASSWORD=your_secure_password_here
DB_HOST=db
DB_PORT=5432
# Redis 配置
REDIS_HOST=redis
REDIS_PORT=6379
REDIS_PASSWORD=
# 应用配置
SECRET_KEY=your_secret_key_here
API_COMPRESSION_ENABLED=false
DEBUG=false
DEPLOY_ENV=PRODUCTION
SERVER_URL=http://localhost:80
CONSOLE_API_URL=http://web:80
CONSOLE_WEB_URL=http://web:80
# 文件上传限制
UPLOAD_FILE_SIZE_LIMIT=50
UPLOAD_FILE_BATCH_LIMIT=5
UPLOAD_IMAGE_FILE_SIZE_LIMIT=10
# Nginx 配置
NGINX_HTTP_PORT=80
NGINX_HTTPS_PORT=443
NGINX_CLIENT_MAX_BODY_SIZE=50m
# 功能开关
CONSOLE_WEB_ENABLED=true
CONSOLE_API_ENABLED=true
EDITOR_WS_ENABLED=true
6. 安装后操作
bash
# 检查所有容器是否正常运行
docker compose ps
# 查看应用日志
docker compose logs app
# 查看 Web 日志
docker compose logs web
# 停止服务
docker compose down
# 停止并删除所有数据(重置安装)
docker compose down -v
# 更新 Dify
cd dify/docker
git pull origin main
docker compose down
docker compose pull
docker compose up -d
7. 访问和初始化
安装完成后,在浏览器中访问:
bash
http://你的服务器IP地址
8. 故障排查
bash
# 检查容器状态
docker ps -a
# 检查特定容器日志
docker logs dify-app
# 进入容器内部检查
docker exec -it dify-app bash
# 检查网络
docker network ls
# 检查卷
docker volume ls