mysqldockernext.jsdocker-composesequelize-cli

How to fix sequelize 'npx sequelize-cli db:migrate' from docker-compose.yml?


I am facing

Sequelize CLI [Node: 21.7.1, CLI: 6.6.2, ORM: 6.37.1] | | Loaded configuration file "database/config/config.json". | Using environment "production". | ERROR: Access denied for user 'root'@'172.21.0.3' (using password: YES)

Setting .env.production and applying command sudo docker compose --env-file ./.env.production up --build.

Here is my docker-compose.yml:

version: '3.8'

networks:
  common:
    driver: bridge

services:
  db:
    networks: 
      - common
    container_name: database
    volumes:
      - mysql_data:/var/lib/mysql
    restart: on-failure
    ports:
      - 3306:3306
    build:
      context: .
      dockerfile: Dockerfile.mysql
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-p$MYSQL_ROOT_PASSWORD"]
      interval: 1s
      retries: 120
      
  adminer:
    image: adminer
    restart: always
    container_name: adminer-g-database
    links:
      - db:db
    ports:
      - 8080:8080
    
  app:
    networks:
      - common
    build:
      context: .
      dockerfile: Dockerfile.app
    container_name: g-app
    restart: always
    links:
      - db:db
    ports:
      - 3000:3000
    depends_on:
      db: 
        condition: service_healthy

    command: sh -c "npx sequelize-cli db:migrate && npm install && npm run build && npm start"
    volumes:
      - .:/usr/src/app
      - /usr/src/app/node_modules

volumes:
  mysql_data:
    driver: local

Here is my Dockerfile for Docker.mysql:

#Create MySQL
FROM mysql

LABEL name=gel

EXPOSE 3306

It is nextJS app where trying to build app with running mysql container and applying sequelize migration command. Thanks in Advance


Solution

  • Here's a minimal working setup that should get you started.

    ├── config
    │   └── config.json
    ├── docker-compose.yml
    ├── Dockerfile.app
    ├── migrations
    └── package.json
    

    The migrations folder is empty for me.

    🗎 config.json (I've set this up for a development environment but you can change to production once you have this working.)

    {
        "development": {
          "username": "root",
          "password": "password",
          "database": "mysql",
          "host": "db",
          "dialect": "mysql"
        }
      }
    

    🗎 docker-compose.yml

    version: '3.8'
    
    services:
      db:
        image: mysql
        restart: on-failure
        environment:
          MYSQL_ROOT_PASSWORD: password
        healthcheck:
          test: ["CMD", "mysqladmin", "ping", "-p$MYSQL_ROOT_PASSWORD"]
          interval: 1s
          retries: 120
    
      app:
        build:
          context: .
          dockerfile: Dockerfile.app
        links:
          - db:db
        ports:
          - 3000:3000
        depends_on:
          db:
            condition: service_healthy
    

    🗎 Dockerfile.app

    FROM node:16
    
    WORKDIR /usr/src/app
    
    COPY package*.json ./
    
    RUN npm install
    
    COPY . .
    
    RUN npm run build
    
    CMD ["sh", "-c", "npx sequelize-cli db:migrate && npm start"]
    

    🗎 package.json (The start script is just a place holder.)

    {
        "name": "test",
        "version": "1.0.0",
        "description": "",
        "scripts": {
          "build": "echo 'Build script not defined'",
          "start": "echo 'Start here...'"
        },
        "dependencies": {
          "express": "^4.17.1",
          "mysql2": "^2.3.0",
          "sequelize": "^6.6.5", 
          "sequelize-cli": "^6.2.0"
        },
        "devDependencies": {
        }
      }
    

    enter image description here