Skip to content

Running

/

Docker

Docker Compose

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.

compose.yaml
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:
.env
SURREAL_USER=root
SURREAL_PASS=secret

Some details about this file:

  • Credentials: the server reads SURREAL_USER and SURREAL_PASS from its own environment, so the .env file supplies them without a --user or --pass argument. This avoids ${VARIABLE} substitution in command, which Compose performs with the variables of the shell that runs docker compose and the .env file next to compose.yaml, not with the variables in env_file. An empty substitution creates a root user with an empty name.

  • Named volume: from the SurrealDB 4.0 image, /data and /logs belong to the container user (UID 65532), so a new named volume mounted at /data is writable by the server. With an earlier image, a new named volume belongs to root, and the server fails to start with Permission denied unless the service also sets user: root.

  • --import-file: the file is imported on every start, not only the first one. It can also be set with the SURREAL_IMPORT_FILE environment 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:

schema.surql
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;
Warning

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.

Was this page helpful?

Last updated