Try building Express + PostgreSQL + Sequelize with Docker [Part 1]


In the future, we aim to configure Vue for the front end and Express for the back end. First, build the environment with Express + PostgreSQL + Sequelize on the back end!

① Until container operation with Docker

Initial file structure

|-- package.json



#Image specification
FROM node:latest

#Environment variable settings
ENV NODE_ENV="development"

#Create working directory&Configuration

COPY ./app /src

RUN npm install


Now design the structure of the Express (server side) container of Node.js and the structure of the DB container.

--The port number is generally 5432. --Volumes allows you to persist data even if you destroy the container. Therefore, the data is saved outside the container.


version: '3'
  #Dockerfile storage location
      context: ./  
      - database
    image: n-app
    #volumes setting
     - "./app:/src"
    #Container name
    container_name: n-app
    #Port connection
      - 3000:5000
      PORT: 5000
      DB_USER: postgres
      DB_HOST: database
      DB_PORT: 5432
      DB_NAME: mydatabase
    tty: true

    image: postgres:12.3
      - ./init-sql:/docker-entrypoint-initdb.d:ro
      POSTGRES_DB: mydatabase
      TZ: "Asia/Tokyo"


It's for myself. By setting Volumes, the package installed on the container is also saved on the host and container. Even if you destroy the container, you can restore the environment of the destroyed container by'npm install'based on package.json by setting Volumes.

--In scripts, set the "○○" part of the "node ○○" command. Please note that currently ./bin/www is not created unless you generate an Express template file.


  "name": "myapp",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www",
  "dependencies": {
    "cookie-parser": "~1.4.3",
    "debug": "~2.6.9",
    "ejs": "~2.5.7",
    "express": "~4.16.0",
    "http-errors": "^1.6.3",
    "morgan": "~1.9.0",
    "nodemon": "^2.0.4",
    "pg": "*",
    "sequelize": "^6.3.5",
    "sequelize-cli": "^6.2.0"

I referred to the following site. I cloned from github of the following site and added the necessary information. It was very convenient because the Express template was prepared in advance.

Now let's run the container using Docker.


$ docker-compose build

Building app
Step 1/5 : FROM node:10.12
 ---> a2b9536415c2
Step 2/5 : ENV NODE_ENV="development"
 ---> Using cache
 ---> 40f981aef1ce
Step 3/5 : WORKDIR /src
 ---> Using cache
 ---> ec233d742a63
Step 4/5 : COPY ./app /src
 ---> Using cache
 ---> 88f269307e53
Step 5/5 : RUN npm install
 ---> Using cache
 ---> b22a8c36f08e

$ docker-compose up -d

$ docker-compose ps

      Name                     Command              State           Ports
n-app               node                            Up>5000/tcp
ta-app_database_1 postgres   Up      5432/tcp

$ docker exec -it n-app bash

[email protected]:/src# 

I was able to enter the container built above.

② Express environment construction

Express is an MVC type framework for web applications that runs on Node.js. In my case, I cloned it from the repository of a great ancestor, so I had a template for Express in advance.

If you want to generate an Express template, it looks like this.

# npm install express-generator -g

1.If you want to create a view as a pug file(Default)
# express --view=pug myapp

If you want to create a view with an ejs file
2. express -e myapp 

next time


Build Docker-Express

About Docker-Volumes

Recommended Posts

Try building Express + PostgreSQL + Sequelize with Docker [Part 2]
Try building Express + PostgreSQL + Sequelize with Docker [Part 1]
Building Rails 6 and PostgreSQL environment with Docker
Try running cloudera manager with docker
Try manipulating PostgreSQL arrays with JDBC
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Sequelize ~
Try running Slack's (Classic) Bot with docker
[Docker] Create Node.js + express + webpack environment with Docker
Try using Kong + Konga with Docker Compose.
Install Docker with WSL2 Memo ([Part 2] Docker introduction)
Experience CI / CD with katacoda (for beginners) --Part10 (Building Docker Images using Jenkins)
Try running MPLS-VPN with FR Routing on Docker
Try running OSPF with FR Routing on Docker
Building OpenPose (Pytorch_Realtime_Multi-Person_Pose_Estimation) environment on Docker: training part
Try using another Servlet container Jetty with Docker
Rails Docker ~ Part 1 ~
Rails Docker ~ Part 2 ~
Until you try running Apache Kafka with docker image
Feel free to try Elasticsearch cluster with WSL2 + Docker
Try connecting to AzureCosmosDB Emulator for Docker with Java
Run Mosquitto with Docker and try WebSocket communication with MQTT
Try building Java into a native module with GraalVM