DEV Community

Cover image for Strapi v4 setup with Docker Compose and MySQL
Sergej Brazdeikis
Sergej Brazdeikis

Posted on • Originally published at blog.brazdeikis.io

Strapi v4 setup with Docker Compose and MySQL

I have to say it was confusing - today existing docker image strapi/strapi has v3 under the hood and there is no simple way or docs to install Strapiv4 via Docker Compose.

Today the only way to create new project Strapi v4 is to use:

npx create-strapi-app@latest strapi-project --quickstart
Enter fullscreen mode Exit fullscreen mode

and use it natively with node with

npm run develop
Enter fullscreen mode Exit fullscreen mode

Today, I'm sharing my solution with Docker-compose.

Create Strapi project via Docker

Install Strapi project files:

  • set node version. Here is version 16.
  • set project name. I use strapi-project
docker run -it \
    -v $PWD:/data\
    -w=/data\
    node:16-alpine sh -c "yes | npx create-strapi-app@latest strapi-project --no-run --dbclient=mysql --dbhost=mariadb --dbport=3306 --dbname=strapi --dbusername=strapi --dbpassword=strapi"
Enter fullscreen mode Exit fullscreen mode

All those CLI options are listed here on the Strapi Github repo - packages/cli/create-strapi-app/create-strapi-app.js With these options the configuration is setup correctly, together with the right database driver in package.json:

...
"dependencies": {
    ...
    "mysql": "2.18.1"
  }
...
Enter fullscreen mode Exit fullscreen mode

Launch with Docker compose with Mysql together

Then lets launch the docker compose. Here as an option I use MariaDb as a choise.

For reference I used this folder structure:

Strapi Docker Compose folder structure

Use following docker-compose.yml

version: '3'
services:
  strapi:
    image: node:16-alpine
    command: npm run develop
    labels:
      com.example.description: "Strapi project"
    environment:
      DATABASE_CLIENT: mysql
      DATABASE_HOST: mariadb
      DATABASE_PORT: 3306
      DATABASE_NAME: strapi
      DATABASE_USERNAME: strapi
      DATABASE_PASSWORD: strapi
      DATABASE_SSL: 'false'
    volumes:
      - ./strapi-project:/data
    working_dir: /data
    ports:
      - '1337:1337'
    depends_on:
      - mariadb

  mariadb:
    image: mariadb
    volumes:
      - ./mariadb:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: strapi
      MYSQL_DATABASE: strapi
      MYSQL_USER: strapi
      MYSQL_PASSWORD: strapi
Enter fullscreen mode Exit fullscreen mode

Deploy with Docker compose to the production environment

For production deployment - please change the passwords above.

Also, it seems Strapi suggests using strapi start instead as launch command.

Happy content creation!

Top comments (0)