├── .env ├── README.md ├── bezkoder-app ├── .env.sample ├── Dockerfile ├── README.md ├── app │ ├── config │ │ └── db.config.js │ ├── controllers │ │ └── tutorial.controller.js │ ├── models │ │ ├── index.js │ │ └── tutorial.model.js │ └── routes │ │ └── turorial.routes.js ├── package.json └── server.js └── docker-compose.yml /.env: -------------------------------------------------------------------------------- 1 | MYSQLDB_USER=root 2 | MYSQLDB_ROOT_PASSWORD=123456 3 | MYSQLDB_DATABASE=bezkoder_db 4 | MYSQLDB_LOCAL_PORT=3307 5 | MYSQLDB_DOCKER_PORT=3306 6 | 7 | NODE_LOCAL_PORT=6868 8 | NODE_DOCKER_PORT=8080 -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Docker Compose Nodejs and MySQL example 2 | 3 | ## Run the System 4 | We can easily run the whole with only a single command: 5 | ```bash 6 | docker compose up 7 | ``` 8 | 9 | Docker will pull the MySQL and Node.js images (if our machine does not have it before). 10 | 11 | The services can be run on the background with command: 12 | ```bash 13 | docker compose up -d 14 | ``` 15 | 16 | ## Stop the System 17 | Stopping all the running containers is also simple with a single command: 18 | ```bash 19 | docker compose down 20 | ``` 21 | 22 | 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: 23 | ```bash 24 | docker compose down --rmi all 25 | ``` 26 | 27 | For more detail, please visit: 28 | > [Dockerize Node.js Express and MySQL example - Docker Compose](https://www.bezkoder.com/docker-compose-nodejs-mysql/) 29 | 30 | Related Posts: 31 | > [Build Node.js Rest APIs with Express & MySQL](https://www.bezkoder.com/node-js-rest-api-express-mysql/) 32 | 33 | > [Upload/store images in MySQL using Node.js, Express & Multer](https://www.bezkoder.com/node-js-upload-image-mysql/) 34 | 35 | > [Node.js: Upload CSV file data into Database with Express](https://bezkoder.com/node-js-upload-csv-file-database/) 36 | 37 | > [Node.js: Upload Excel file data into Database with Express](https://www.bezkoder.com/node-js-upload-excel-file-database/) 38 | 39 | > [Build Node.js Rest APIs with Express, Sequelize & MySQL](https://bezkoder.com/node-js-express-sequelize-mysql/) 40 | 41 | > [Server side Pagination in Node.js with Sequelize and MySQL](https://bezkoder.com/node-js-sequelize-pagination-mysql/) 42 | 43 | > [Deploying/Hosting Node.js app on Heroku with MySQL database](https://bezkoder.com/deploy-node-js-app-heroku-cleardb-mysql/) 44 | 45 | Security: 46 | > [Node.js Express: JWT example | Token Based Authentication & Authorization](https://bezkoder.com/node-js-jwt-authentication-mysql/) 47 | 48 | Associations: 49 | > [Sequelize Associations: One-to-Many Relationship example](https://bezkoder.com/sequelize-associate-one-to-many/) 50 | 51 | > [Sequelize Associations: Many-to-Many Relationship example](https://bezkoder.com/sequelize-associate-many-to-many/) 52 | -------------------------------------------------------------------------------- /bezkoder-app/.env.sample: -------------------------------------------------------------------------------- 1 | DB_HOST=localhost 2 | DB_USER=root 3 | DB_PASSWORD=123456 4 | DB_NAME=bezkoder_db 5 | DB_PORT=3306 6 | 7 | NODE_DOCKER_PORT=8080 -------------------------------------------------------------------------------- /bezkoder-app/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM node:14 2 | 3 | WORKDIR /bezkoder-app 4 | COPY package.json . 5 | RUN npm install 6 | COPY . . 7 | CMD npm start 8 | -------------------------------------------------------------------------------- /bezkoder-app/README.md: -------------------------------------------------------------------------------- 1 | # Node.js Rest APIs with Express, Sequelize & MySQL example 2 | 3 | For more detail, please visit: 4 | > [Build Node.js Rest APIs with Express, Sequelize & MySQL](https://bezkoder.com/node-js-express-sequelize-mysql/) 5 | 6 | > [Server side Pagination in Node.js with Sequelize and MySQL](https://bezkoder.com/node-js-sequelize-pagination-mysql/) 7 | 8 | > [Deploying/Hosting Node.js app on Heroku with MySQL database](https://bezkoder.com/deploy-node-js-app-heroku-cleardb-mysql/) 9 | 10 | Security: 11 | > [Node.js Express: JWT example | Token Based Authentication & Authorization](https://bezkoder.com/node-js-jwt-authentication-mysql/) 12 | 13 | Associations: 14 | > [Sequelize Associations: One-to-Many Relationship example](https://bezkoder.com/sequelize-associate-one-to-many/) 15 | 16 | > [Sequelize Associations: Many-to-Many Relationship example](https://bezkoder.com/sequelize-associate-many-to-many/) 17 | 18 | Fullstack: 19 | > [Vue.js + Node.js + Express + MySQL example](https://bezkoder.com/vue-js-node-js-express-mysql-crud-example/) 20 | 21 | > [Vue.js + Node.js + Express + MongoDB example](https://bezkoder.com/vue-node-express-mongodb-mevn-crud/) 22 | 23 | > [Angular 8 + Node.js + Express + MySQL example](https://bezkoder.com/angular-node-express-mysql/) 24 | 25 | > [Angular 10 + Node.js + Express + MySQL example](https://bezkoder.com/angular-10-node-js-express-mysql/) 26 | 27 | > [Angular 11 + Node.js Express + MySQL example](https://bezkoder.com/angular-11-node-js-express-mysql/) 28 | 29 | > [Angular 12 + Node.js Express + MySQL example](https://bezkoder.com/angular-12-node-js-express-mysql/) 30 | 31 | > [Angular 13 + Node.js + Express + MySQL example](https://www.bezkoder.com/angular-13-node-js-express-mysql/) 32 | 33 | > [Angular 14 + Node.js + Express + MySQL example](https://www.bezkoder.com/angular-14-node-js-express-mysql/) 34 | 35 | > [Angular 15 + Node.js + Express + MySQL example](https://www.bezkoder.com/angular-15-node-js-express-mysql/) 36 | 37 | > [Angular 16 + Node.js + Express + MySQL example](https://www.bezkoder.com/angular-16-node-js-express-mysql/) 38 | 39 | > [React + Node.js + Express + MySQL example](https://bezkoder.com/react-node-express-mysql/) 40 | 41 | Integration (run back-end & front-end on same server/port) 42 | > [Integrate React with Node.js Restful Services](https://bezkoder.com/integrate-react-express-same-server-port/) 43 | 44 | > [Integrate Angular with Node.js Restful Services](https://bezkoder.com/integrate-angular-10-node-js/) 45 | 46 | > [Integrate Vue with Node.js Restful Services](https://bezkoder.com/serve-vue-app-express/) 47 | 48 | ## Project setup 49 | ``` 50 | npm install 51 | ``` 52 | 53 | ### Run 54 | ``` 55 | node server.js 56 | ``` 57 | -------------------------------------------------------------------------------- /bezkoder-app/app/config/db.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | HOST: process.env.DB_HOST, 3 | USER: process.env.DB_USER, 4 | PASSWORD: process.env.DB_PASSWORD, 5 | DB: process.env.DB_NAME, 6 | port: process.env.DB_PORT, 7 | dialect: "mysql", 8 | pool: { 9 | max: 5, 10 | min: 0, 11 | acquire: 30000, 12 | idle: 10000 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /bezkoder-app/app/controllers/tutorial.controller.js: -------------------------------------------------------------------------------- 1 | const db = require("../models"); 2 | const Tutorial = db.tutorials; 3 | const Op = db.Sequelize.Op; 4 | 5 | // Create and Save a new Tutorial 6 | exports.create = (req, res) => { 7 | // Validate request 8 | if (!req.body.title) { 9 | res.status(400).send({ 10 | message: "Content can not be empty!" 11 | }); 12 | return; 13 | } 14 | 15 | // Create a Tutorial 16 | const tutorial = { 17 | title: req.body.title, 18 | description: req.body.description, 19 | published: req.body.published ? req.body.published : false 20 | }; 21 | 22 | // Save Tutorial in the database 23 | Tutorial.create(tutorial) 24 | .then(data => { 25 | res.send(data); 26 | }) 27 | .catch(err => { 28 | res.status(500).send({ 29 | message: 30 | err.message || "Some error occurred while creating the Tutorial." 31 | }); 32 | }); 33 | }; 34 | 35 | // Retrieve all Tutorials from the database. 36 | exports.findAll = (req, res) => { 37 | const title = req.query.title; 38 | var condition = title ? { title: { [Op.like]: `%${title}%` } } : null; 39 | 40 | Tutorial.findAll({ where: condition }) 41 | .then(data => { 42 | res.send(data); 43 | }) 44 | .catch(err => { 45 | res.status(500).send({ 46 | message: 47 | err.message || "Some error occurred while retrieving tutorials." 48 | }); 49 | }); 50 | }; 51 | 52 | // Find a single Tutorial with an id 53 | exports.findOne = (req, res) => { 54 | const id = req.params.id; 55 | 56 | Tutorial.findByPk(id) 57 | .then(data => { 58 | res.send(data); 59 | }) 60 | .catch(err => { 61 | res.status(500).send({ 62 | message: "Error retrieving Tutorial with id=" + id 63 | }); 64 | }); 65 | }; 66 | 67 | // Update a Tutorial by the id in the request 68 | exports.update = (req, res) => { 69 | const id = req.params.id; 70 | 71 | Tutorial.update(req.body, { 72 | where: { id: id } 73 | }) 74 | .then(num => { 75 | if (num == 1) { 76 | res.send({ 77 | message: "Tutorial was updated successfully." 78 | }); 79 | } else { 80 | res.send({ 81 | message: `Cannot update Tutorial with id=${id}. Maybe Tutorial was not found or req.body is empty!` 82 | }); 83 | } 84 | }) 85 | .catch(err => { 86 | res.status(500).send({ 87 | message: "Error updating Tutorial with id=" + id 88 | }); 89 | }); 90 | }; 91 | 92 | // Delete a Tutorial with the specified id in the request 93 | exports.delete = (req, res) => { 94 | const id = req.params.id; 95 | 96 | Tutorial.destroy({ 97 | where: { id: id } 98 | }) 99 | .then(num => { 100 | if (num == 1) { 101 | res.send({ 102 | message: "Tutorial was deleted successfully!" 103 | }); 104 | } else { 105 | res.send({ 106 | message: `Cannot delete Tutorial with id=${id}. Maybe Tutorial was not found!` 107 | }); 108 | } 109 | }) 110 | .catch(err => { 111 | res.status(500).send({ 112 | message: "Could not delete Tutorial with id=" + id 113 | }); 114 | }); 115 | }; 116 | 117 | // Delete all Tutorials from the database. 118 | exports.deleteAll = (req, res) => { 119 | Tutorial.destroy({ 120 | where: {}, 121 | truncate: false 122 | }) 123 | .then(nums => { 124 | res.send({ message: `${nums} Tutorials were deleted successfully!` }); 125 | }) 126 | .catch(err => { 127 | res.status(500).send({ 128 | message: 129 | err.message || "Some error occurred while removing all tutorials." 130 | }); 131 | }); 132 | }; 133 | 134 | // find all published Tutorial 135 | exports.findAllPublished = (req, res) => { 136 | Tutorial.findAll({ where: { published: true } }) 137 | .then(data => { 138 | res.send(data); 139 | }) 140 | .catch(err => { 141 | res.status(500).send({ 142 | message: 143 | err.message || "Some error occurred while retrieving tutorials." 144 | }); 145 | }); 146 | }; 147 | -------------------------------------------------------------------------------- /bezkoder-app/app/models/index.js: -------------------------------------------------------------------------------- 1 | const dbConfig = require("../config/db.config.js"); 2 | 3 | const Sequelize = require("sequelize"); 4 | const sequelize = new Sequelize(dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD, { 5 | host: dbConfig.HOST, 6 | dialect: dbConfig.dialect, 7 | port: dbConfig.port, 8 | operatorsAliases: false, 9 | 10 | pool: { 11 | max: dbConfig.pool.max, 12 | min: dbConfig.pool.min, 13 | acquire: dbConfig.pool.acquire, 14 | idle: dbConfig.pool.idle 15 | } 16 | }); 17 | 18 | const db = {}; 19 | 20 | db.Sequelize = Sequelize; 21 | db.sequelize = sequelize; 22 | 23 | db.tutorials = require("./tutorial.model.js")(sequelize, Sequelize); 24 | 25 | module.exports = db; 26 | -------------------------------------------------------------------------------- /bezkoder-app/app/models/tutorial.model.js: -------------------------------------------------------------------------------- 1 | module.exports = (sequelize, Sequelize) => { 2 | const Tutorial = sequelize.define("tutorial", { 3 | title: { 4 | type: Sequelize.STRING 5 | }, 6 | description: { 7 | type: Sequelize.STRING 8 | }, 9 | published: { 10 | type: Sequelize.BOOLEAN 11 | } 12 | }); 13 | 14 | return Tutorial; 15 | }; 16 | -------------------------------------------------------------------------------- /bezkoder-app/app/routes/turorial.routes.js: -------------------------------------------------------------------------------- 1 | module.exports = app => { 2 | const tutorials = require("../controllers/tutorial.controller.js"); 3 | 4 | var router = require("express").Router(); 5 | 6 | // Create a new Tutorial 7 | router.post("/", tutorials.create); 8 | 9 | // Retrieve all Tutorials 10 | router.get("/", tutorials.findAll); 11 | 12 | // Retrieve all published Tutorials 13 | router.get("/published", tutorials.findAllPublished); 14 | 15 | // Retrieve a single Tutorial with id 16 | router.get("/:id", tutorials.findOne); 17 | 18 | // Update a Tutorial with id 19 | router.put("/:id", tutorials.update); 20 | 21 | // Delete a Tutorial with id 22 | router.delete("/:id", tutorials.delete); 23 | 24 | // Delete all Tutorials 25 | router.delete("/", tutorials.deleteAll); 26 | 27 | app.use('/api/tutorials', router); 28 | }; 29 | -------------------------------------------------------------------------------- /bezkoder-app/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nodejs-express-sequelize-mysql", 3 | "version": "1.0.0", 4 | "description": "Node.js Rest Apis with Express, Sequelize & MySQL", 5 | "main": "server.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [ 10 | "nodejs", 11 | "express", 12 | "rest", 13 | "api", 14 | "sequelize", 15 | "mysql" 16 | ], 17 | "author": "bezkoder", 18 | "license": "ISC", 19 | "dependencies": { 20 | "dotenv": "^10.0.0", 21 | "cors": "^2.8.5", 22 | "express": "^4.17.1", 23 | "mysql2": "^2.0.2", 24 | "sequelize": "^5.21.2" 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /bezkoder-app/server.js: -------------------------------------------------------------------------------- 1 | require("dotenv").config(); 2 | const express = require("express"); 3 | const cors = require("cors"); 4 | 5 | const app = express(); 6 | 7 | var corsOptions = { 8 | origin: "http://localhost:8081" 9 | }; 10 | 11 | app.use(cors(corsOptions)); 12 | 13 | // parse requests of content-type - application/json 14 | app.use(express.json()); 15 | 16 | // parse requests of content-type - application/x-www-form-urlencoded 17 | app.use(express.urlencoded({ extended: true })); 18 | 19 | const db = require("./app/models"); 20 | 21 | db.sequelize.sync(); 22 | // // drop the table if it already exists 23 | // db.sequelize.sync({ force: true }).then(() => { 24 | // console.log("Drop and re-sync db."); 25 | // }); 26 | 27 | // simple route 28 | app.get("/", (req, res) => { 29 | res.json({ message: "Welcome to bezkoder application." }); 30 | }); 31 | 32 | require("./app/routes/turorial.routes")(app); 33 | 34 | // set port, listen for requests 35 | const PORT = process.env.NODE_DOCKER_PORT || 8080; 36 | app.listen(PORT, () => { 37 | console.log(`Server is running on port ${PORT}.`); 38 | }); 39 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3.8' 2 | 3 | services: 4 | mysqldb: 5 | image: mysql:5.7 6 | restart: unless-stopped 7 | env_file: ./.env 8 | environment: 9 | - MYSQL_ROOT_PASSWORD=$MYSQLDB_ROOT_PASSWORD 10 | - MYSQL_DATABASE=$MYSQLDB_DATABASE 11 | ports: 12 | - $MYSQLDB_LOCAL_PORT:$MYSQLDB_DOCKER_PORT 13 | volumes: 14 | - db:/var/lib/mysql 15 | app: 16 | depends_on: 17 | - mysqldb 18 | build: ./bezkoder-app 19 | restart: unless-stopped 20 | env_file: ./.env 21 | ports: 22 | - $NODE_LOCAL_PORT:$NODE_DOCKER_PORT 23 | environment: 24 | - DB_HOST=mysqldb 25 | - DB_USER=$MYSQLDB_USER 26 | - DB_PASSWORD=$MYSQLDB_ROOT_PASSWORD 27 | - DB_NAME=$MYSQLDB_DATABASE 28 | - DB_PORT=$MYSQLDB_DOCKER_PORT 29 | stdin_open: true 30 | tty: true 31 | 32 | volumes: 33 | db: --------------------------------------------------------------------------------