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

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

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.