0

I am currently trying to set up surrealDB with a docker-compose-file for a new project.

When executing docker compose up --build, I want to start surrealDB and then import a provided surql-file, so that the started db already has data to work with.

This is my docker-compose for the surrealDB container now:

version: '3'

services:
    surrealdb:
        env_file:
            - .env
        restart: always
        entrypoint: /surreal
        command: start --log trace --user root --pass root file:/data/database.db
        image: surrealdb/surrealdb:latest
        ports:
            - 8000:8000
        volumes:
            - ./data:/data/

When looking for how to add several commands into one docker-compose I've found several solutions, including sh -c, > [command] && [command] and several others similar to both.

But none of them worked how I expected. The most common error war surrealDB not knowing the command (sh for example) or not executing the second command:

command: >
     start --log trace --user root --pass root file://data/database.db &&
     import --conn http://localhost:8000 --user root --pass root --ns test --db test data/database.surql

This executes the start command but the import never starts. It's nowhere to be found within the logs, no command, no error, but also no data.

So my question is:

What is the best way to start a surrealDB database and import a surql-file into this database from within a docker-compose-file?

And another question that I haven't researched yet: Will executing the import command always overwrite the initialized data within the database? I've worked with postgres before and there an init.sql-file was copied into the docker entrypoint directory and only executed if the database wasn't already built. Is there maybe a similar way for surrealDB?

Thanks alot for your help

marvin
  • 15
  • 5
  • 1
    Doing this during build is probably not what you want. The solution is to wide to fit in an answer. Meanwhile there are several official db images (postgres, mysql, mongodb....) which manage that through an entrypoint and dedicated environment variables and specific content folders to initialize the database as requested if the volume containing the data is empty at container start. Doing it this way lets you start with a clean and functionnal db whenever you start from scratch while keeping your current version of the database if you need to. – Zeitounator Jul 07 '23 at 14:23
  • 1
    I suggest you study those examples and eventually do something similar with you own extended image if the base surealDB image does not provide such features. See e.g. [the mysql image documentation](https://hub.docker.com/_/mysql/) and the [mysql image entrypoint](https://github.com/docker-library/mysql/blob/1bfa4724fe112b4246672ed2b3c42142f17d5636/8.0/docker-entrypoint.sh). You can look for the same information for the other dbs listed above to have a wider view. – Zeitounator Jul 07 '23 at 14:28

0 Answers0