Skip to content

OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.

Python

fix: persist Langflow database across container restarts

Mike Pawlowski8566c05539f2
diff --git a/.env.example b/.env.example
index a2a90fa59..2cb3df3fc 100644
--- a/.env.example
+++ b/.env.example
@@ -79,6 +79,11 @@ OPENSEARCH_DATA_PATH=./opensearch-data
# Default: ./langflow-data
LANGFLOW_DATA_PATH=./langflow-data
+# Langflow database URL. Defaults to SQLite stored in LANGFLOW_DATA_PATH.
+# Override with a PostgreSQL URL for production deployments, e.g.:
+# LANGFLOW_DATABASE_URL=postgresql://user:pass@host:5432/langflow
+LANGFLOW_DATABASE_URL=sqlite:////app/langflow-data/langflow.db
+
# OpenSearch Connection
OPENSEARCH_HOST=opensearch
OPENSEARCH_PORT=9200
diff --git a/Dockerfile.langflow b/Dockerfile.langflow
index abb651c74..122c8e5ea 100644
--- a/Dockerfile.langflow
+++ b/Dockerfile.langflow
@@ -1,20 +1,11 @@
# syntax=docker/dockerfile:1.4
FROM langflowai/langflow:1.8.0
-# Switch to root so the entrypoint can fix data directory ownership before dropping privileges.
-# The base image runs as uid=1000; we restore that in the entrypoint script.
-USER root
-
-# (+) Install uv
-# (+) Pre-create the Langflow data directory.
-# - For named Docker volumes, this seeds the volume with the correct path on first mount.
-# - For bind mounts, the entrypoint chowns the directory at startup.
+# Install uv and pre-create the Langflow data directory.
+# The base image already runs as uid=1000 and owns /app, so no root or chown needed.
RUN pip install uv \
&& mkdir -p /app/langflow-data
-COPY --chmod=755 docker-entrypoint-langflow.sh /docker-entrypoint-langflow.sh
-
EXPOSE 7860
-ENTRYPOINT ["/docker-entrypoint-langflow.sh"]
CMD ["langflow", "run", "--host", "0.0.0.0", "--port", "7860"]
diff --git a/docker-compose.yml b/docker-compose.yml
index 6a2a07f16..f0fd620a5 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -147,7 +147,7 @@ services:
- WATSONX_PROJECT_ID=${WATSONX_PROJECT_ID}
- OLLAMA_BASE_URL=${OLLAMA_ENDPOINT}
- LANGFLOW_CONFIG_DIR=/app/langflow-data
- - LANGFLOW_DATABASE_URL=sqlite:////app/langflow-data/langflow.db
+ - LANGFLOW_DATABASE_URL=${LANGFLOW_DATABASE_URL:-sqlite:////app/langflow-data/langflow.db}
- LANGFLOW_SECRET_KEY=${LANGFLOW_SECRET_KEY}
- JWT=None
- OWNER=None
diff --git a/docker-entrypoint-langflow.sh b/docker-entrypoint-langflow.sh
deleted file mode 100644
index 550ddcd53..000000000
--- a/docker-entrypoint-langflow.sh
+++ /dev/null
@@ -1,13 +0,0 @@
-#!/bin/sh
-set -e
-
-# Fix ownership of the Langflow data directory so the container user (uid=1000) can write to it.
-# When the directory is bind-mounted from a host with a different UID (e.g. CI runners at uid=1001),
-# the container user cannot create files. Running chown here as root — before dropping privileges —
-# mirrors the pattern used by official database images (OpenSearch, PostgreSQL, Redis).
-chown -R 1000:1000 /app/langflow-data
-
-# Drop from root to uid=1000 and exec the main process.
-# Python is used for privilege drop — it is guaranteed to be present in the Langflow image
-# and requires no additional packages (unlike gosu or su-exec).
-exec python3 -c 'import os, sys; os.setgid(1000); os.setuid(1000); os.execvp(sys.argv[1], sys.argv[1:])' "$@"
diff --git a/kubernetes/helm/openrag/templates/langflow/deployment.yaml b/kubernetes/helm/openrag/templates/langflow/deployment.yaml
index aa57b8762..3c25bfa68 100644
--- a/kubernetes/helm/openrag/templates/langflow/deployment.yaml
+++ b/kubernetes/helm/openrag/templates/langflow/deployment.yaml
@@ -123,10 +123,10 @@ spec:
env:
# Langflow core settings
- - name: LANGFLOW_LOAD_FLOWS_PATH
- value: {{ .Values.langflow.persistence.mountPath }}/{{ .Values.langflow.persistence.flowsSubPath }}
- name: LANGFLOW_DATABASE_URL
value: "sqlite:///{{ .Values.langflow.persistence.mountPath }}/{{ .Values.langflow.persistence.dbSubPath }}"
+ - name: LANGFLOW_CONFIG_DIR
+ value: {{ .Values.langflow.persistence.mountPath }}
- name: LANGFLOW_DEACTIVATE_TRACING
value: {{ .Values.langflow.deactivateTracing | quote }}
- name: LANGFLOW_LOG_LEVEL
diff --git a/src/main.py b/src/main.py
index 8eae9b293..8f59edf56 100644
--- a/src/main.py
+++ b/src/main.py
@@ -1203,11 +1203,16 @@ async def startup_tasks(services):
# Ensure all configured flows exist in Langflow (create-only, never overwrites).
# This replaces LANGFLOW_LOAD_FLOWS_PATH, which performed a blind upsert on
# every container start and discarded any user edits made in the Langflow UI.
+ newly_created: set[str] = set()
try:
flows_service = services["flows_service"]
- await flows_service.ensure_flows_exist()
+ newly_created = await flows_service.ensure_flows_exist()
except Exception as e:
- logger.warning("Failed to ensure Langflow flows exist at startup", error=str(e))
+ logger.error(
+ "Failed to ensure Langflow flows exist at startup — "
+ "flows may be missing until the next restart",
+ error=str(e),
+ )
# Check if flows were reset and reapply settings if config is edited
try:
@@ -1216,6 +1221,9 @@ async def startup_tasks(services):
logger.info("Checking if Langflow flows were reset")
flows_service = services["flows_service"]
reset_flows = await flows_service.check_flows_reset()
+ # Exclude flows that were just seeded — they match the JSON by design,
+ # not because they were externally reset.
+ reset_flows = [f for f in reset_flows if f not in newly_created]
if reset_flows:
logger.info(
diff --git a/src/services/flows_service.py b/src/services/flows_service.py
index 137af1080..f06d7b948 100644
--- a/src/services/flows_service.py
+++ b/src/services/flows_service.py
@@ -799,7 +799,7 @@ class FlowsService:
logger.error(f"Error comparing flow {flow_id} with file: {str(e)}")
return False
- async def ensure_flows_exist(self):
+ async def ensure_flows_exist(self) -> set[str]:
"""
Ensure all configured flows exist in Langflow.
@@ -810,6 +810,8 @@ class FlowsService:
This replaces the LANGFLOW_LOAD_FLOWS_PATH mechanism, which performed a
blind upsert on every container start and discarded user edits.
+
+ Returns the set of flow type names that were actually created.
"""
flow_configs = [
("nudges", NUDGES_FLOW_ID),
@@ -817,6 +819,7 @@ class FlowsService:
("ingest", LANGFLOW_INGEST_FLOW_ID),
("url_ingest", LANGFLOW_URL_INGEST_FLOW_ID),
]
+ created_flow_types: set[str] = set()
for flow_type, flow_id in flow_configs:
if not flow_id:
@@ -832,6 +835,13 @@ class FlowsService:
)
continue
+ if response.status_code != 404:
+ logger.warning(
+ f"Unexpected status checking {flow_type} flow (ID: {flow_id}): "
+ f"HTTP {response.status_code} — skipping creation to avoid overwriting existing data"
+ )
+ continue
+
flow_path = self._find_flow_file_by_id(flow_id)
if not flow_path:
logger.warning(
@@ -849,6 +859,7 @@ class FlowsService:
logger.info(
f"Created {flow_type} flow (ID: {flow_id}) from {os.path.basename(flow_path)}"
)
+ created_flow_types.add(flow_type)
else:
logger.warning(
f"Failed to create {flow_type} flow (ID: {flow_id}): "
@@ -860,6 +871,8 @@ class FlowsService:
f"Error ensuring {flow_type} flow (ID: {flow_id}) exists: {e}"
)
+ return created_flow_types
+
async def check_flows_reset(self):
"""
Check if any flows have been reset by comparing with JSON files.