Skip to content

rendyproklamanta/docker-express-crud-mysql-sequalize

Repository files navigation

Express-Sequelize-MySQL With Dockerfile and docker-compose.yml

Prerequisite

  1. Install node

  2. Install yarn

  3. Install XAMPP For Running Local Mysql Server

  4. Create a file named .env in the root directory & copy the texts from .env.example

  5. Create a MySql DB & change the value of database in .env

How to run without docker

  1. Go to the project root directory

  2. Run yarn install on terminal/CMD

  3. Then run yarn global add sequelize-cli nodemon on terminal/CMD

  4. Then run migrations. The commands for migration & creating model are listed below in DB Commands section

  5. Go back to root directory and run yarn start

  6. Now your backend will be running on localhost:3000

DB Commands

  1. Open XAMPP/MAMP and start mysql service
  2. Add user root with any host in mysql :
CREATE USER 'root_any'@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'root_any'@'%';
  1. Edit database config in .env
  2. If you running with docker. Dont forget to change database host to :
host.docker.internal

If you running without docker using yarn start, change host to :

localhost
  1. For migration, run sequelize db:migrate
  2. For creating a model, run sequelize model:create --name TableName --attributes columnName:string, ...

All other sequelize commands are available here Sequelize Documentation


Running on Dockerfile only

The Dockerfile is also included. You need to just build for production with live mysql credential using :

Build with dockerfile

docker build -t express-sequelize-mysql .

Build with custom dockerfile.test

docker build -f Dockerfile.test -t express-sequelize-mysql .

Run

docker run --rm -it -p 3000:3000 express-sequelize-mysql

Open URL in browser : http://localhost:3000

Note : You can change docker name {express-sequelize-mysql} with your own



Run with Docker Compose support hot / live reload


Production

Run not in background:

docker-compose up -t express-sequelize-mysql

Run in background

docker-compose up -t express-sequelize-mysql --build --force-recreate --no-deps -d

Local development
  1. The service can be run on the background with command :
docker-compose -f docker-compose.dev.yml up --build --force-recreate --no-deps -d
  1. And goto url http://localhost:3000
  2. To test edit text in server.js , save and reload web to check hot-reload work

Stop the System

Stopping all the running containers is also simple with a single command:

docker-compose down

With custom docker-compose

docker-compose -f docker-compose.dev.yml down

If you need to stop and remove all containers, networks, and all images used by any service in docker-compose.yml file, use the command:

docker-compose down --rmi all

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published