↓ Here is a revised version of this article (a version that uses mongoose). [[App Development 1] [Node.js express Docker] Build an environment for Node.js Express MongoDB (mongoose) using Docker [December 2020]] (https://qiita.com/sho_U/items/43f6483aac8ca45a12f6)
We have decided to develop an application using Node.js Express MongoDB as a contract development. I would like to record the knowledge gained during development within a range that does not hinder it. Aside from the contents of the application, since we will develop it as a team, we will first build a development environment using Docker.
--Create an app container and mongodb container using docker-compose. --Save data in the test model using the connection test (mongoose) between the app container and the mongodb container. --Confirm data insertion from mongo compass --Provide authentication for mongodb. The authority of the user to create is "root", "read", "owner" --Database-like passwords are managed by environment variables (remove from git management with gitignore)
"bcrypt"
"body-parser"
"connect-flash"
"cookie-parser"
"debug"
"ejs"
"express"
"express-ejs-layouts"
"express-generator"
"express-session"
"express-validator"
"http-errors"
"http-status-codes"
"method-override"
"mongoose"
"morgan"
"nodemon"
"passport"
"passport-local-mongoose"
.
├── .env
├── .gitignore
├── Dockerfile
├── data
│ └── db //Empty directory
├── docker-compose.yml
├── docker_app
│ └── Dockerfile
├── secret_file
│ ├── db.env
│ └── db_init
└── src
├── controllers
│ └── initTestsController.js
├── models
│ └── init_test.js
└── package.json
.env
docker-compose build docker-compose up -d docker exec -it uniq_app_cnt bash
npx express-generator --view=ejs
paste package.json
npm install
├── Dockerfile
├── data
│ └── db
├── docker-compose.yml
├── src
│ ├── node_modules
│ ├── package-lock.json
│ ├── package.json
│ └── test_db.js
├── .env
└── .gitignore
Dockerfile.
FROM node:12
WORKDIR /app
RUN npm install
docker-compose.yml
version: '3'
services:
app:
build: .
container_name: <app name>_app_cnt
ports:
- 8080:8080
restart: always
working_dir: /app
tty: true
volumes:
- /etc/passwd:/etc/passwd:ro
- /etc/group:/etc/group:ro
- ./src:/app
command: bash
networks:
- <app name>-network
depends_on:
- mongo
mongo:
image: mongo:4.2.5-bionic
container_name: <app name>_db_cnt
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
MONGO_INITDB_DATABASE: ${MONGO_INITDB_DATABASE}
volumes:
- ./data/db:/data/db
command:
- mongod
networks:
- <app name>-network
networks:
<app name>-network:
external: true
.env.
APP_USER=hoge
MONGO_INITDB_ROOT_USERNAME=test
MONGO_INITDB_ROOT_PASSWORD=pass
MONGO_INITDB_DATABASE=testdb
.gitignore.
node_modules/
data/
.env
test_db.js
const mongodb = require('mongodb')
const MongoClient = mongodb.MongoClient
let url = 'mongodb://test:pass@mongo:27017/'
let db_name = 'testdb'
let collection_name = 'testUser'
console.log(url)
console.log(db_name)
console.log(collection_name)
const option = {
useNewUrlParser: true,
useUnifiedTopology: true,
}
MongoClient.connect(url, option, (err, client) => {//mongodb connection
if (err != null || client == null) {
console.log("<<<log>>>fail")
console.log(err)
} else {
const db = client.db(db_name)
let rec = { "name": "Taro", "age": 30 }
db.collection(collection_name).insertOne(rec, (err, res) => {//Insert test data
if (err != null) {
console.log("<<<log>>>err: insert")
console.log(err)
client.close()
} else {
db.collection(collection_name).find({}).toArray((err, result) => {//Data retrieval
if (err != null) {
console.log("<<<log>>>err: select")
console.log(err)
client.close()
} else {
console.log(result)//Result output
client.close()
}
})
}
})
}
})
docker-compoes up
Check connection.
Tab open by terminal Move to the application directory
docker exec -it <app name>_app_cnt bash
//Enter the container
node test_db
Confirm insertion and retrieval of test data
testdb
testUser
[ { _id: 5fe60041416135000e5cd787, name: 'Taro', age: 30 } ]
Connection completed
Recommended Posts