Skip to content
Snippets Groups Projects
Commit 2c7bf224 authored by steven.liatti's avatar steven.liatti
Browse files

Improve docker-compose build and run

On branch master

Changes to be committed:
	modified:   README.md
	new file:   api/.dockerignore
	new file:   api/.env
	new file:   api/.env_docker
	modified:   api/.gitignore
	modified:   api/Dockerfile
	modified:   api/app/queries.js
	new file:   api/package-lock.json
	modified:   api/package.json
	modified:   api/server.js
	modified:   docker-compose.yml
	modified:   mongodb/Dockerfile
parent 7827a077
No related branches found
No related tags found
No related merge requests found
configure and run MongoDB 3.4 with dump: build and run MongoDB and Node.js API with `orders.json` dump:
``` ```
docker-compose build docker-compose up --build -d
docker-compose up -d
``` ```
use mongodb: use mongodb (mongo shell):
``` ```
docker-compose exec mongo-orders mongo admin docker-compose exec mongo-orders mongo admin
``` ```
...@@ -17,4 +16,4 @@ show collections; ...@@ -17,4 +16,4 @@ show collections;
db.orders.find(); db.orders.find();
``` ```
Open api at http://localhost:8080/ API in Node.js available, example route http://localhost:8080/by-customer-pseudo/A**D
.gitignore
\ No newline at end of file
PORT=3000
DB_URL=mongodb://localhost:27017
DB_NAME=order
\ No newline at end of file
PORT=8080
DB_URL=mongodb://mongo-orders:27017
DB_NAME=order
\ No newline at end of file
node_modules node_modules
package-lock.json \ No newline at end of file
\ No newline at end of file
FROM node:11 FROM node:11-alpine
WORKDIR /home/nodejs/app WORKDIR /home/nodejs/app
COPY . . COPY . .
COPY .env_docker .env
RUN npm install --production RUN npm install --production
CMD ["npm", "start"] CMD npm start
require('dotenv').config();
const u = require('./utils'); const u = require('./utils');
const MongoClient = require('mongodb').MongoClient; const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://mongo-orders:27017'; const url = process.env.DB_URL;
const dbName = 'order'; const dbName = process.env.DB_NAME;
let db; let db;
MongoClient.connect(url, function(err, client) { MongoClient.connect(url, function(err, client) {
...@@ -10,7 +11,7 @@ MongoClient.connect(url, function(err, client) { ...@@ -10,7 +11,7 @@ MongoClient.connect(url, function(err, client) {
console.log(err); console.log(err);
} }
else { else {
console.log('Connected successfully to server'); console.log('Connected successfully to db at "' + url + '/' + dbName + '"');
db = client.db(dbName); db = client.db(dbName);
} }
}); });
......
This diff is collapsed.
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"body-parser": "^1.19.0", "body-parser": "^1.19.0",
"dotenv": "^8.0.0",
"express": "^4.17.0", "express": "^4.17.0",
"mongodb": "^3.2.5" "mongodb": "^3.2.5"
}, },
......
// modules // modules
require('dotenv').config();
const express = require('express'); const express = require('express');
const app = express(); const app = express();
const bodyParser = require('body-parser'); const bodyParser = require('body-parser');
......
version: "2" version: '2'
services: services:
mongo-orders: mongo-orders:
image: mongo image: mongo
ports: ports:
- "27017:27017" - '27017:27017'
depends_on:
- mongo-seed
mongo-seed: mongo-seed:
build: ./mongodb build: ./mongodb
depends_on:
- mongo-orders
api: api:
build: ./api build: ./api
ports: ports:
- "8080:8080" - '8080:8080'
depends_on: depends_on:
- mongo-orders - mongo-orders
FROM mongo FROM mongo:latest
COPY ./data/orders.json /orders.json COPY ./data/orders.json /orders.json
CMD mongoimport --host mongo-orders --db order --collection orders --type json --file orders.json --jsonArray && mongo --host mongo-orders order --eval 'db.orders.find( { "customer.id.floatApprox": { $exists: true } } ).forEach( tr => db.orders.update({_id: tr._id},{$set:{"customer.id": tr.customer.id.floatApprox}} ) )' && mongo --host mongo-orders order --eval 'db.orders.find().forEach( tr => db.orders.update({_id: tr._id}, {$set:{"shipping.when": new Date(tr.shipping.when)}}))' CMD mongo --host mongo-orders order --eval 'db.dropDatabase()' && mongoimport --host mongo-orders --db order --collection orders --type json --file orders.json --jsonArray && mongo --host mongo-orders order --eval 'db.orders.find( { "customer.id.floatApprox": { $exists: true } } ).forEach( tr => db.orders.update({_id: tr._id},{$set:{"customer.id": tr.customer.id.floatApprox}} ) )' && mongo --host mongo-orders order --eval 'db.orders.find().forEach( tr => db.orders.update({_id: tr._id}, {$set:{"shipping.when": new Date(tr.shipping.when)}}))'
\ No newline at end of file \ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment