Sling Academy
Home/Node.js/How to Setup Docker Compose, Node.js, Express, Sequelize, and Postgres

How to Setup Docker Compose, Node.js, Express, Sequelize, and Postgres

Last updated: December 29, 2023

Introduction

Setting up a development environment can be a complex process, especially when you’re working with multiple technologies. In this guide, we’ll walk through the process of setting up a project using Docker Compose, Node.js, Express, Sequelize, and Postgres to create a consistent, reproducible development environment. This setup ensures that your application will work seamlessly across different machines and that new team members can get started quickly without configuring each tool individually.

Before proceeding, make sure you have Docker installed on your machine. Docker Compose will be used to define and run multi-container Docker applications, and it requires Docker Engine. Visit the official Docker website to install Docker if you haven’t already.

Setting Up Docker Compose

Create a directory for your project and within it, create a ‘docker-compose.yml’ file. This file will define your services, networks, and volumes. Add the following configuration to your file:

version: '3'
services:
  db:
    image: postgres
    environment:
      POSTGRES_DB: yourdbname
      POSTGRES_USER: yourusername
      POSTGRES_PASSWORD: yourpassword
  app:
    build: .
    command: npm start
    volumes:
      - .:/usr/src/app
    ports:
      - '3000:3000'
    depends_on:
      - db

Node.js and Express Setup

Now let’s set up Node.js and Express. Inside your project directory, create a ‘Dockerfile’ for your application with the following content:

FROM node:14
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ['npm', 'start']

Next, create a ‘package.json’ file by running the ‘npm init’ command, or if you have a template you prefer, you can copy that instead.

Sequelize.js Setup

Install Sequelize and the PostgreSQL driver by running:

npm install sequelize pg pg-hstore

Add a ‘sequelize’ script in your ‘package.json’ to run Sequelize CLI commands inside your Docker container.

Developing the Application

Create an ‘index.js’ file as your entry point and set up a basic Express server. Also establish a connection to Postgres using Sequelize.

const express = require('express');
const { Sequelize } = require('sequelize');

const sequelize = new Sequelize('postgres://yourusername:yourpassword@db:5432/yourdbname');

const app = express();

app.get('/', (req, res) => {
  res.send('Hello World!
');
});
app.listen(3000);

Launching the Application

You can launch your application by running ‘docker-compose up’ from the command line. This will build the image for your app service and start all services defined in your ‘docker-compose.yml’ file.

Project Structure

Here is a simplified version of what the project structure and main files might look like:

- project-directory/
  - Dockerfile
  - docker-compose.yml
  - package.json
  - index.js

Conclusion

In conclusion, Docker Compose combined with Node.js, Express, Sequelize, and PostgreSQL provides a powerful stack for developing web applications. The environment setup in this guide facilitates easy collaboration and environment consistency across even the most demanding projects.

Next Article: How to Setup a Node.js Cluster for Speed & Stability

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

Series: Node.js Intermediate Tutorials

Node.js

You May Also Like

  • NestJS: How to create cursor-based pagination (2 examples)
  • Cursor-Based Pagination in SequelizeJS: Practical Examples
  • MongooseJS: Cursor-Based Pagination Examples
  • Node.js: How to get location from IP address (3 approaches)
  • SequelizeJS: How to reset auto-increment ID after deleting records
  • SequelizeJS: Grouping Results by Multiple Columns
  • NestJS: Using Faker.js to populate database (for testing)
  • NodeJS: Search and download images by keyword from Unsplash API
  • NestJS: Generate N random users using Faker.js
  • Sequelize Upsert: How to insert or update a record in one query
  • NodeJS: Declaring types when using dotenv with TypeScript
  • Using ExpressJS and Multer with TypeScript
  • NodeJS: Link to static assets (JS, CSS) in Pug templates
  • NodeJS: How to use mixins in Pug templates
  • NodeJS: Displaying images and links in Pug templates
  • ExpressJS + Pug: How to use loops to render array data
  • ExpressJS: Using MORGAN to Log HTTP Requests
  • NodeJS: Using express-fileupload to simply upload files
  • ExpressJS: How to render JSON in Pug templates