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

Preamble

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

-Dockerfile
-docker-compose.yml
-app
|-- package.json

Dockerfile

Dockefile


#Image specification
FROM node:latest

#Environment variable settings
ENV NODE_ENV="development"

#Create working directory&Configuration
WORKDIR /src

COPY ./app /src

RUN npm install

docker-compose.yml

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.

docker-compose.yml


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

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

package.json

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.

app/package.json


{
  "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.

https://qiita.com/tamoco/items/caffca436546a1a5fcc8

Now let's run the container using Docker.

Terminal


$ 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      0.0.0.0:3000->5000/tcp
ta-app_database_1   docker-entrypoint.sh postgres   Up      5432/tcp

$ docker exec -it n-app bash

root@f195575a066f:/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

https://qiita.com/rockguitar67/items/0020d734201632077cb5

References

Build Docker-Express https://qiita.com/tamoco/items/caffca436546a1a5fcc8

https://gitlab.com/tamoco-mocomoco/study-docker-compose/-/tree/master/app/routes

https://qiita.com/yoshiplum/items/129e7ad1ffc3a02b9eb2

About Docker-Volumes https://qiita.com/gounx2/items/23b0dc8b8b95cc629f32

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