Strapi removes newly created tables on server restart

System Information
  • Strapi Version: 4.7
  • Operating System: Windows WSL2 docker
  • Database: mysql
  • Node Version:18
  • NPM Version:
  • Yarn Version:

I do have following docker compose

version: ‘3’

services:
strapi:
container_name: strapi_app
build: .
ports:
- “1337:1337”
environment:
DATABASE_CLIENT: mysql
DATABASE_HOST: db
DATABASE_PORT: 3306
DATABASE_NAME: strapi
DATABASE_USERNAME: strapi
DATABASE_PASSWORD: strapi
depends_on:
- db

db:
container_name: strapi_db
image: mysql:8.1
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: strapi
MYSQL_USER:
MYSQL_PASSWORD:
ports:
- “3306:3306”
volumes:
- mysql_data:/var/lib/mysql
command: [‘–default-authentication-plugin=mysql_native_password’]

volumes:
mysql_data:

and dockerfile
FROM node:18

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

CMD [“npm”, “run”, “develop”]

when I do docker compose down and restart it removes all tables and data related to new created contenttypes

What does your database.js file look like?
Might still use sqlite which would explain what happends.

this is how my database.js is

module.exports = ({ env }) => {
const client = env(“DATABASE_CLIENT”, “mysql”);

const connections = {
mysql: {
connection: {
connectionString: env(“DATABASE_URL”, “”),
host: env(“DATABASE_HOST”, “db”),
port: env.int(“DATABASE_PORT”, 3306),
database: env(“DATABASE_NAME”, “strapi”),
user: env(“DATABASE_USERNAME”, “strapi”),
password: env(“DATABASE_PASSWORD”, “somepasswordgoeshere”),
ssl: env.bool(“DATABASE_SSL”, false) && {
key: env(“DATABASE_SSL_KEY”, undefined),
cert: env(“DATABASE_SSL_CERT”, undefined),
ca: env(“DATABASE_SSL_CA”, undefined),
capath: env(“DATABASE_SSL_CAPATH”, undefined),
cipher: env(“DATABASE_SSL_CIPHER”, undefined),
rejectUnauthorized: env.bool(
“DATABASE_SSL_REJECT_UNAUTHORIZED”,
true
),
},
},
pool: {
min: env.int(“DATABASE_POOL_MIN”, 2),
max: env.int(“DATABASE_POOL_MAX”, 10),
},
}
};

some more info I used strapi v4.14.4
default DB structre keep remains and new data for those data too, if I create new content type then those will removed upon restart.

This is because you build your docker image at the current state of strapi when you run docker build. (Creating content types happens on the filesystem.) So by restarting you delete all of the newly created content types,which in turn will delete the tables (most likely) on restart.
What is your use case with that setup? As you use yarn develop I suppose it’s development? Any reason you are using docker for strapi instead of local node? If so you need to mount the relevant strapi folders in the container for it to persist.

If you’re trying to use this setup for production. Don’t.

Hi thanks for the explanation, couldn’t find strapi docker image for latest version so used the current state when building docker

regarding yarn develop → if I use yar build on my prod environment i won’t able to create new contentTypes on the fly right or can ?

and I tried voulme mounting it works thanks

Yes its true that you wont be able to create contentTypes after you used yarn build and yarn start, but thats the point.
if you develop (creating content types etc is developing) on your Prod environment , you can accidentally delete stuff etc, if you misclick.

I would recomment you develop the content types locally, then build a docker image where you run yarn build inside the Dockerfile and use yarn start as an entrypoint (or something along the lines)
then deploy that image to prod. Also make sure to make regular backup of your production Database.

For development that docker-compose file with mounted folders should suffice.
But for production read up a bit (idk if its in the docs , but i guess you’ll find some guidelines somewhere)

Good luck :slight_smile: