Skip to content

Official Crawl4AI Cloud: a hosted web scraping, extraction, and search API for AI agents. Free API key, 1,000 credits a month, no card. Fully open source: this repo is the Apache-2.0 crawler that powers it, and you can self-host it.

fix(docker): forward CRAWL4AI_API_TOKEN through compose, make .llm.env optional

Soham Kukretie07ac4fd3201
diff --git a/deploy/docker/.llm.env.example b/deploy/docker/.llm.env.example
index 2d7a2ea2..6f84f1ff 100644
--- a/deploy/docker/.llm.env.example
+++ b/deploy/docker/.llm.env.example
@@ -1,11 +1,3 @@
-# REQUIRED for a reachable server: API token for the Docker server (0.9.0+).
-# Without it the server binds loopback inside the container and the published
-# port answers with "connection reset". Any non-empty value works, but treat it
-# as a password — use a long random string (e.g. from: openssl rand -hex 32).
-# Note: with docker compose, the token MUST be set here — exporting it in your
-# shell does not reach the container.
-CRAWL4AI_API_TOKEN=
-
# Optional: enable declarative hooks support (disabled by default)
# CRAWL4AI_HOOKS_ENABLED=true
diff --git a/deploy/docker/MIGRATION.md b/deploy/docker/MIGRATION.md
index b895ae58..bcd2097c 100644
--- a/deploy/docker/MIGRATION.md
+++ b/deploy/docker/MIGRATION.md
@@ -25,12 +25,13 @@ loopback by default and will not expose itself without a credential.
export CRAWL4AI_API_TOKEN="$(openssl rand -hex 32)"
```
-> ⚠️ **Docker Compose users:** `export` alone does **not** work — the shipped
-> `docker-compose.yml` does not forward host environment variables. Set the
-> token in the `.llm.env` file at the project root instead (the example file
-> ships an empty `CRAWL4AI_API_TOKEN=` line — fill it in).
->
-> For plain `docker run`, pass it explicitly:
+With `docker compose`, run the export in the same shell before
+`docker compose up`; the compose file passes the token into the container.
+For a persistent setup, put the `CRAWL4AI_API_TOKEN=...` line in a `.env`
+file in the project root instead — compose reads it automatically (a shell
+export still takes precedence).
+
+> ⚠️ For plain `docker run`, pass it explicitly:
> `-e CRAWL4AI_API_TOKEN="$CRAWL4AI_API_TOKEN"` (the value-less shorthand
> `-e CRAWL4AI_API_TOKEN` silently passes empty from a shell where the variable
> isn't set).
diff --git a/deploy/docker/entrypoint.sh b/deploy/docker/entrypoint.sh
index 3d003791..22c7483c 100644
--- a/deploy/docker/entrypoint.sh
+++ b/deploy/docker/entrypoint.sh
@@ -32,7 +32,7 @@ else
GUNICORN_BIND="127.0.0.1:${PORT}"
echo "entrypoint: no CRAWL4AI_API_TOKEN set; binding loopback only (${GUNICORN_BIND})." >&2
echo "entrypoint: WARNING: this is the CONTAINER's loopback - published ports (-p ${PORT}:${PORT}) will NOT work; connections from the host will be reset." >&2
- echo "entrypoint: to make the server reachable, set CRAWL4AI_API_TOKEN (docker run -e CRAWL4AI_API_TOKEN=..., or the .llm.env file with docker compose), then restart. (If you enabled security.jwt_enabled in a custom config.yml, set CRAWL4AI_JWT_ENABLED=true instead.)" >&2
+ echo "entrypoint: to make the server reachable, set CRAWL4AI_API_TOKEN (docker run -e CRAWL4AI_API_TOKEN=..., or 'export CRAWL4AI_API_TOKEN=...' before 'docker compose up'), then restart. (If you enabled security.jwt_enabled in a custom config.yml, set CRAWL4AI_JWT_ENABLED=true instead.)" >&2
fi
export GUNICORN_BIND
diff --git a/docker-compose.yml b/docker-compose.yml
index 5939b332..6beb0016 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -1,13 +1,15 @@
-version: '3.8'
-
# Shared configuration for all environments
x-base-config: &base-config
ports:
- "11235:11235" # Gunicorn port
env_file:
- - .llm.env # API keys (create from .llm.env.example)
+ # API keys (create from .llm.env.example); optional so a fresh clone runs.
+ - path: .llm.env
+ required: false
+ environment:
+ # Auth token passthrough from the host shell (overwrites .llm.env).
+ - CRAWL4AI_API_TOKEN=${CRAWL4AI_API_TOKEN:-}
# Uncomment to set default environment variables (will overwrite .llm.env)
- # environment:
# - OPENAI_API_KEY=${OPENAI_API_KEY:-}
# - DEEPSEEK_API_KEY=${DEEPSEEK_API_KEY:-}
# - ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY:-}
diff --git a/docs/md_v2/core/self-hosting.md b/docs/md_v2/core/self-hosting.md
index 966672f0..09e5e37a 100644
--- a/docs/md_v2/core/self-hosting.md
+++ b/docs/md_v2/core/self-hosting.md
@@ -62,7 +62,7 @@ When you self-host, you can scale from a single container to a full browser infr
## Prerequisites
Before we dive in, make sure you have:
-- Docker installed and running (version 20.10.0 or higher), including `docker compose` (usually bundled with Docker Desktop).
+- Docker installed and running (version 20.10.0 or higher), including `docker compose` v2.24+ (usually bundled with Docker Desktop).
- `git` for cloning the repository.
- At least 4GB of RAM available for the container (more recommended for heavy use).
- Python 3.10+ (if using the Python SDK).
@@ -212,29 +212,36 @@ cd crawl4ai
#### 2. Environment Setup (Required)
-The compose file loads `.llm.env` from the **project root directory** — the
-file must exist even if you don't use LLMs, or compose will fail with
-"env file .llm.env not found". Create it from the example and add an API token:
+Export an API token in your shell — the compose file passes it into the
+container. **Required**, or the server will be unreachable (loopback-only,
+published port → connection reset):
```bash
-# Make sure you are in the 'crawl4ai' root directory
-cp deploy/docker/.llm.env.example .llm.env
+export CRAWL4AI_API_TOKEN="$(openssl rand -hex 32)"
```
-Then open `.llm.env` and fill in the `CRAWL4AI_API_TOKEN=` line at the top —
-**required**, or the server will be unreachable (loopback-only). Any long
-random string works, e.g. from `openssl rand -hex 32`. One-liner:
+Prefer a file? Put the same line in a `.env` file in the project root —
+compose reads it automatically on every run, no export needed (if both are
+set, the shell export wins):
```bash
-sed -i.bak "s|^CRAWL4AI_API_TOKEN=.*|CRAWL4AI_API_TOKEN=$(openssl rand -hex 32)|" .llm.env && rm .llm.env.bak
+echo "CRAWL4AI_API_TOKEN=$(openssl rand -hex 32)" > .env
```
-Optionally add your LLM API keys in the same file.
+If you use LLMs, also create `.llm.env` in the **project root directory** with
+your API keys (optional — compose starts fine without it):
+
+```bash
+# Make sure you are in the 'crawl4ai' root directory
+cp deploy/docker/.llm.env.example .llm.env
+
+# Now edit .llm.env and add your LLM API keys
+```
-> ⚠️ **The token must go inside `.llm.env`.** `export CRAWL4AI_API_TOKEN=...`
-> in your shell does **not** work with compose — the compose file does not
-> forward host environment variables, and the server silently starts in
-> loopback-only mode (published port → connection reset).
+> ⚠️ Run the export in the **same shell** you run `docker compose up` from.
+> With compose, only the shell export or a `.env` line carries the token — a
+> `CRAWL4AI_API_TOKEN=` line in `.llm.env` is overridden by the compose
+> passthrough.
**Flexible LLM Provider Configuration:**
@@ -305,7 +312,7 @@ The `docker-compose.yml` file in the project root provides a simplified approach
> The server will be available at `http://localhost:11235` (allow ~10 seconds
> for startup). All endpoints except `GET /health` require
-> `Authorization: Bearer <your token from .llm.env>`.
+> `Authorization: Bearer <your exported token>`.
#### 4. Stopping the Service