はじめに
OSS版のDifyの環境を構築する方法を紹介する。
動作確認環境
- Ubuntu 22.04 x86_64
- Docker Engine Version 27.3.1
- Docker Compose Version v2.29.7
方法
Docker Composeを使って環境構築する。
OSS版Difyのクローン
cd {your_workspace} # 任意のワークスペース
git clone https://github.com/langgenius/dify.git
.envファイルの編集
用意されている.env.example
をコピーする。
cd dify/docker
cp .env.example .env
vi .env
以下に主要な.envファイルに記載されている環境変数の修正方法を記載する。.envファイルに記載の環境変数の詳細については、公式ドキュメントを参照。ただし、ドキュメントが更新されていないため、最新版の情報はソースコードを参照することを推奨する。以下の2つのファイルが該当する。
ポート番号の設定
HTTPのポート番号を変更する。変更するには、.envファイル内のEXPOSE_NGINX_PORT
を変更する。
# ------------------------------
# Docker Compose Service Expose Host Port Configurations
# ------------------------------
EXPOSE_NGINX_PORT=80
EXPOSE_NGINX_SSL_PORT=443
# ------------------------------
# Docker Compose Service Expose Host Port Configurations
# ------------------------------
EXPOSE_NGINX_PORT=8080
EXPOSE_NGINX_SSL_PORT=443
WebアプリURLの設定
デプロイ先のコンソールWebアプリのURLを設定する。Dify中のリンク先アドレスなどが設定したURLに変更される。
WebアプリURLを指定すると、外部から通信できなくなる場合がある。
localhost(127.0.0.1)の指定やIPアドレスの解決ができない場合は通信できなくなるので、外部から通信ができなくなった場合は、空欄のままでも良い。
# ------------------------------
# Common Variables
# ------------------------------
# The backend URL of the console API,
# used to concatenate the authorization callback.
# If empty, it is the same domain.
# Example: https://api.console.dify.ai
CONSOLE_API_URL=
# The front-end URL of the console web,
# used to concatenate some front-end addresses and for CORS configuration use.
# If empty, it is the same domain.
# Example: https://console.dify.ai
CONSOLE_WEB_URL=
# Service API Url,
# used to display Service API Base Url to the front-end.
# If empty, it is the same domain.
# Example: https://api.dify.ai
SERVICE_API_URL=
# WebApp API backend Url,
# used to declare the back-end URL for the front-end API.
# If empty, it is the same domain.
# Example: https://api.app.dify.ai
APP_API_URL=
# WebApp Url,
# used to display WebAPP API Base Url to the front-end.
# If empty, it is the same domain.
# Example: https://app.dify.ai
APP_WEB_URL=
# File preview or download Url prefix.
# used to display File preview or download Url to the front-end or as Multi-model inputs;
# Url is signed and has expiration time.
FILES_URL=
デプロイ先をhttp://127.0.0.1:8080
に変更する例を以下に示す。
# ------------------------------
# Common Variables
# ------------------------------
# The backend URL of the console API,
# used to concatenate the authorization callback.
# If empty, it is the same domain.
# Example: https://api.console.dify.ai
CONSOLE_API_URL=
# The front-end URL of the console web,
# used to concatenate some front-end addresses and for CORS configuration use.
# If empty, it is the same domain.
# Example: https://console.dify.ai
CONSOLE_WEB_URL=http://127.0.0.1:8080
# Service API Url,
# used to display Service API Base Url to the front-end.
# If empty, it is the same domain.
# Example: https://api.dify.ai
SERVICE_API_URL=http://127.0.0.1:8080
# WebApp API backend Url,
# used to declare the back-end URL for the front-end API.
# If empty, it is the same domain.
# Example: https://api.app.dify.ai
APP_API_URL=http://127.0.0.1:8080
# WebApp Url,
# used to display WebAPP API Base Url to the front-end.
# If empty, it is the same domain.
# Example: https://app.dify.ai
APP_WEB_URL=http://127.0.0.1:8080
# File preview or download Url prefix.
# used to display File preview or download Url to the front-end or as Multi-model inputs;
# Url is signed and has expiration time.
FILES_URL=http://127.0.0.1:8080
ログのタイムゾーンの変更
ログのタイムゾーンを変更する場合は、以下の項目を修正する。
# ------------------------------
# Server Configuration
# ------------------------------
# The log level for the application.
# Supported values are `DEBUG`, `INFO`, `WARNING`, `ERROR`, `CRITICAL`
LOG_LEVEL=INFO
# Log file path
LOG_FILE=/app/logs/server.log
# Log file max size, the unit is MB
LOG_FILE_MAX_SIZE=20
# Log file max backup count
LOG_FILE_BACKUP_COUNT=5
# Log dateformat
LOG_DATEFORMAT=%Y-%m-%d %H:%M:%S
# Log Timezone
LOG_TZ=UTC
# ------------------------------
# Server Configuration
# ------------------------------
# The log level for the application.
# Supported values are `DEBUG`, `INFO`, `WARNING`, `ERROR`, `CRITICAL`
LOG_LEVEL=INFO
# Log file path
LOG_FILE=/app/logs/server.log
# Log file max size, the unit is MB
LOG_FILE_MAX_SIZE=20
# Log file max backup count
LOG_FILE_BACKUP_COUNT=5
# Log dateformat
LOG_DATEFORMAT=%Y-%m-%d %H:%M:%S
# Log Timezone
LOG_TZ=Asia/Tokyo
メール設定
ユーザ登録時のメール送信のための設定。
# ------------------------------
# Mail related configuration
# ------------------------------
# Mail type, support: resend, smtp
MAIL_TYPE=resend
# Default send from email address, if not specified
MAIL_DEFAULT_SEND_FROM=
# API-Key for the Resend email provider, used when MAIL_TYPE is `resend`.
RESEND_API_URL=https://api.resend.com
RESEND_API_KEY=your-resend-api-key
# SMTP server configuration, used when MAIL_TYPE is `smtp`
SMTP_SERVER=
SMTP_PORT=465
SMTP_USERNAME=
SMTP_PASSWORD=
SMTP_USE_TLS=true
SMTP_OPPORTUNISTIC_TLS=false
例としてSMTP(認証なし)で以下の設定を行う。
- SMTPサーバ: mail.example.com
- SMTPポート: 25
- 送信アドレス: my.dify@example.com
# ------------------------------
# Mail related configuration
# ------------------------------
# Mail type, support: resend, smtp
MAIL_TYPE=smtp
# Default send from email address, if not specified
MAIL_DEFAULT_SEND_FROM=my.dify@example.com
# API-Key for the Resend email provider, used when MAIL_TYPE is `resend`.
RESEND_API_URL=https://api.resend.com
RESEND_API_KEY=your-resend-api-key
# SMTP server configuration, used when MAIL_TYPE is `smtp`
SMTP_SERVER=mail.example.com
SMTP_PORT=25
SMTP_USERNAME=
SMTP_PASSWORD=
SMTP_USE_TLS=false
SMTP_OPPORTUNISTIC_TLS=false
起動
docker compose up -d
アップグレードする場合
【オプション】docker-composeファイルのバックアップ
cd {your_workspace}/dify
cd ./docker
cp docker-compose.yaml docker-compose.yaml.$(date +%s).bak
リポジトリを最新版に更新
git checkout main
git pull origin main
コンテナの停止と削除
docker compose down
【オプション】データのバックアップ
sudo tar -cvf volumes-$(date +%s).tgz volumes
DifyのDockerコンテナで利用されるデータは、ホストのdify/docker/volumes
にマウントされる設定となっているのでコンテナを削除してもデータは残る。
コンテナ起動
docker compose up -d
起動後の設定
初期設定
初回アクセス時に管理者アカウントの設定画面が表示されるので、設定を行う。
ワークスペース名を変更
以下の記事を参照。
言語とタイムゾーンの変更
「設定」-「言語」から設定する。
まとめ
OSS版Difyの環境を構築する方法を紹介した。URLの変更やメール通知設定などを行っておくと、運用しやすくなるので試してみてほしい。
また、ワークフローの最大実行時間の変更、ループブロックでの回数変更は以下の記事に記載したので参考にしてほしい。
参考