How can I generate multiple Strapi databases?

System Information
  • Strapi Version: 4.7.0
  • Operating System: macOS Ventura 13.0
  • Database: MariaDB (Docker)
  • Node Version: 16.19.1 (16:alpine in Docker)
  • NPM Version: 8.19.3
  • Yarn Version: 1.22.18

I’m trying to use the same project to create multiple sites. The project consists of a Strapi folder with all the necessary code and a local Dockerfile, plus a Docker Compose file to start everything. The Docker Compose file has a .env file, and the MariaDB database container is using a Docker Volume called strapi-data-test.

Strapi Dockerfile
FROM node:16-alpine
# Installing libvips-dev for sharp compatability
RUN apk update && apk add --no-cache build-base gcc autoconf automake zlib-dev libpng-dev vips-dev > /dev/null 2>&1
ARG NODE_ENV=development
ENV NODE_ENV=${NODE_ENV}
WORKDIR /opt/
COPY ./package.json ./yarn.lock ./.yarnrc.yml ./
ENV PATH /opt/node_modules/.bin:$PATH
RUN yarn config set network-timeout 600000 -g && yarn install
WORKDIR /opt/app
COPY ./ .
RUN yarn add sharp
RUN yarn build
EXPOSE 1337
CMD ["yarn", "develop"]

Docker Compose file
version: '3'
services:
  strapi:
    container_name: strapi
    platform: linux/amd64 #for platform error on Apple M1 chips
    build:
      context: ./strapi
      dockerfile: Dockerfile.local
    image: strapi:latest
    restart: unless-stopped
    env_file: .env
    environment:
      DATABASE_CLIENT: ${DATABASE_CLIENT}
      DATABASE_HOST: strapiDB
      DATABASE_PORT: ${DATABASE_PORT}
      DATABASE_NAME: ${DATABASE_NAME}
      DATABASE_USERNAME: ${DATABASE_USERNAME}
      DATABASE_PASSWORD: ${DATABASE_PASSWORD}
      JWT_SECRET: ${JWT_SECRET}
      ADMIN_JWT_SECRET: ${ADMIN_JWT_SECRET}
      APP_KEYS: ${APP_KEYS}
      NODE_ENV: ${NODE_ENV}
      API_TOKEN_SALT: ${API_TOKEN_SALT}
    volumes:
      - ./strapi:/opt/app
      - /opt/app/node_modules
    ports:
      - '1337:1337'
    depends_on:
      - strapiDB

  strapiDB:
    container_name: strapiDB
    platform: linux/amd64 #for platform error on Apple M1 chips
    restart: unless-stopped
    env_file: .env
    image: mariadb:latest
    environment:
      MYSQL_USER: ${DATABASE_USERNAME}
      MYSQL_ROOT_PASSWORD: ${DATABASE_PASSWORD}
      MYSQL_PASSWORD: ${DATABASE_PASSWORD}
      MYSQL_DATABASE: ${DATABASE_NAME}
    volumes:
      - strapi-data-test:/var/lib/mysql
    ports:
      - '3306:3306'

volumes:
  strapi-data-test:

My .env file looks like this (keys / secrets / tokens removed):

.env file
DATABASE_CLIENT=mysql
DATABASE_PORT=3306
DATABASE_NAME=first
DATABASE_USERNAME=first
DATABASE_PASSWORD=randompassword
JWT_SECRET=
ADMIN_JWT_SECRET=
APP_KEYS=
NODE_ENV=development
PUBLIC_URL=http://localhost:1337
API_TOKEN_SALT=
CMS_BEARER_TOKEN=

If I run docker compose -f docker-compose.local.yml up --build strapiDB strapi, everything works as expected. I can go to http://localhost:1337/admin, and I can see that a database called first has been created.

However, the problems start when I try to build a second site, still using the strapi-data-test volume for the MariaDB container. I modified the .env file so it used a different db name and username:

Updated .env file
DATABASE_CLIENT=mysql
DATABASE_PORT=3306
DATABASE_NAME=second
DATABASE_USERNAME=second
DATABASE_PASSWORD=randompassword
JWT_SECRET=
ADMIN_JWT_SECRET=
APP_KEYS=
NODE_ENV=development
PUBLIC_URL=http://localhost:1337
API_TOKEN_SALT=
CMS_BEARER_TOKEN=

Next, I deleted the Docker containers, and ran docker compose -f docker-compose.local.yml up --build strapiDB strapi again. Instead of creating a new database and user like the first time I ran everything, I get the following error:

How does Strapi know whether to create a new database and user? Is there a way around this?

You will need to either create a separate DATABASE (easiest method) so run 2 strapi instances and 2 databases)
Else you have to create a init script for mariadb to create the other tables.
Here is an example of it

Using the multiple tables, you can then change the variables for each one to connect to a separate table and when it starts it creates them.

1 Like

Sorry for the late reply! Thanks, having multiple strapi instances and databases worked perfectly

1 Like