Setting Up Docker Compose with Node.js, Express, Sequelize, and MySQL

Updated: December 29, 2023 By: Guest Contributor Post a comment

Introduction

Developing with Node.js and MySQL is a well-worn path with plenty of support. Adding Docker to the mix elevates the configuration to offer greater manageability and portability. This guide builds an environment wherein Node.js runs with Express as the server framework, Sequelize ORM to interact with MySQL, and Docker Compose to handle the orchestration of the services. This method fosters an environment that is consistent across workstations and production severs, eases setup for new developers, and can improve deployment as well as continuous integration and development practices.

Environment Setup

Before you begin, ensure you have Docker and Docker Compose installed on your system. You’ll write a docker-compose.yml file to define the services your application needs, and Docker Compose will set them up for you.

Define Your Docker Compose Configuration

Create a new directory for your project and then create a docker-compose.yml file to define the services: a Node.js app server and a MySQL database server.

version: '3.8'
services:
  app:
    build: .
    ports:
      - '3000:3000'
    depends_on:
      - db
    environment:
      - DATABASE_URL=mysql://user:password@db:3306/mydb
  db:
    image: mysql:5.7
    environment:
      - MYSQL_ROOT_PASSWORD=secretpassword
      - MYSQL_DATABASE=mydb
      - MYSQL_USER=user
      - MYSQL_PASSWORD=password
    volumes:
      - db-data:/var/lib/mysql

volumes:
  db-data:

Set Up Your Node.js Application

Bootstrap a basic Node.js application using Express. Begin by creating a Dockerfile for your Node.js application:

FROM node:14

# Set working directory
WORKDIR /usr/src/app

# Install app dependencies
COPY package*.json ./
RUN npm install

# Copy app source code
COPY . .

EXPOSE 3000

# Starts the application
CMD [ "npm", "start" ]

Create your Node.js application inside the project directory. It uses Express for routing and Sequelize to manage database interactions:

Integrating Sequelize

Sequelize is a promise-based Node.js ORM that supports MySQL and other databases. To integrate Sequelize, begin by installing it and the MySQL driver:

npm install sequelize mysql2

Create a Sequelize configuration file and specify the connection details in accordance with the environment variables set in your Docker Compose file. Be sure to set the dialect to mysql:

const Sequelize = require('sequelize');

const sequelize = new Sequelize(process.env.DATABASE_URL, {
  dialect: 'mysql'
});

module.exports = sequelize;

To handle migrations, models, and seeds, consider using the Sequelize CLI (npx sequelize-cli). Create models to shape the data structure within your MySQL database.

Try It

The complete example—Docker Compose yml, Dockerfile, together with the basic Node.js application code setup and Sequelize integration—will manifest a complete stack that is runnable on Docker. Do ensure that your codebase has the requisite package.json, includes all of your source code and is set to connect properly through Docker’s service linking.

Start your stack with:

docker-compose up

After execution, both services should be operational. Your app will listen on port 3000, and MySQL will start with the specified database and user account ready.

Conclusion

This guide provided a rough pathway to configuring an environment with the powerful combination of Docker, Node.js, Express, Sequelize, and MySQL. A dockerized setup ensures consistency across various work environments and deployments. A potential next step could be enriching the application’s features, optimizing the Docker setup for production, or exploring Kubernetes for greater orchestration control.