Docker Compose describes a SurrealDB server, and any services that use it, in one file. This page shows a Compose file for the official image with persistent storage.
The Compose file below starts SurrealDB with a persistent volume, reads the root credentials from a .env file, and loads a schema file every time the server starts.
services:
surrealdb:
image: surrealdb/surrealdb:latest
ports:
- "8000:8000"
env_file:
- .env
volumes:
- surrealdb-data:/data
- ./schema.surql:/schema.surql:ro
command: start --import-file /schema.surql surrealkv:/data/app.db
volumes:
surrealdb-data:SURREAL_USER=root
SURREAL_PASS=secretSome details about this file:
Credentials: the server reads
SURREAL_USERandSURREAL_PASSfrom its own environment, so the.envfile supplies them without a--useror--passargument. This avoids${VARIABLE}substitution incommand, which Compose performs with the variables of the shell that runsdocker composeand the.envfile next tocompose.yaml, not with the variables inenv_file. An empty substitution creates a root user with an empty name.Named volume: from the SurrealDB 4.0 image,
/dataand/logsbelong to the container user (UID65532), so a new named volume mounted at/datais writable by the server. With an earlier image, a new named volume belongs toroot, and the server fails to start withPermission deniedunless the service also setsuser: root.--import-file: the file is imported on every start, not only the first one. It can also be set with theSURREAL_IMPORT_FILEenvironment variable.
Because the import runs on every start, write the file so that it can run again on a database that already has the schema:
DEFINE NAMESPACE IF NOT EXISTS app;
USE NS app;
DEFINE DATABASE IF NOT EXISTS main;
USE DB main;
DEFINE TABLE IF NOT EXISTS person SCHEMAFULL;
DEFINE FIELD IF NOT EXISTS name ON person TYPE string; A statement in the import file that fails, such as a DEFINE TABLE without IF NOT EXISTS for a table that already exists, is skipped without an error in the log, and the server starts normally. Use IF NOT EXISTS so that the file gives the same result on every start, or OVERWRITE for a definition that the file must always replace.