Seeding
Learn how to seed your databases.
We currently recommend that you incorporate seed scripts into your images. Another way to seed your environments is to create a separate service on your compose file.
Building images with seeds
In most cases, using a built image with a seed script is the easiest way to seed a preview environment.
Mongo example
If you’re using MongoDB, you can create a Dockerfile
using the mongo
image as the base image, and copy the necessary seed files to the /docker-entrypoint-initdb.d
path within that image. Any files within that path will be automatically executed by the image when the mongo
service starts.
Assume you have the seed script below, named seed.js
within a scripts
folder, for example.
In that case, you’ll simply configure your Dockerfile
so that the JavaScript file is copied to /docker-entrypoint-initdb.d
when building the image.
Finally, you’ll reference this Dockerfile
in the docker-compose.yml
file used by Ergomake.
In this example, we assume the following file-tree structure:
Postgres Example
If you’re using the postgres
image, you can simply copy your seed scripts to /docker-entrypoint-initb.d
. That’s because the image is configured to execute any files within that directory when the postgres
service starts.
Assume you had the seed script below, named dump.sql
, within a scripts
folder.
In that case, you’d first create a script to run it, let’s say seed.sh
, within that same scripts
folder.
Then, you’d configure your Dockerfile
to copy seed.sh
the dump.sql
to docker-entrypoint-initdb.d
, as shown below.
Finally, you’ll reference that Dockerfile
in the docker-compose.yml
you use for Ergomake.
In this example, we assume the following file-tree structure:
Seeding environments with services
To seed an environment with a separate service, all you need to do is create a separate service in your compose and change its command so that it runs the seed command.
Another way to seed it and still have a single service is to use multiple commands on your compose.
Make sure that restarting your service will not cause the seed command to fail. That could happen if you’re trying to create a database that already exists every time your seed script runs.
Make sure that your command
can run multiple times without errors so that your preview environments stay up.