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 -d
docker-compose up --build -d
```
use mongodb:
use mongodb (mongo shell):
```
docker-compose exec mongo-orders mongo admin
```
......@@ -17,4 +16,4 @@ show collections;
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
package-lock.json
\ No newline at end of file
node_modules
\ No newline at end of file
FROM node:11
FROM node:11-alpine
WORKDIR /home/nodejs/app
COPY . .
COPY .env_docker .env
RUN npm install --production
CMD ["npm", "start"]
CMD npm start
require('dotenv').config();
const u = require('./utils');
const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://mongo-orders:27017';
const dbName = 'order';
const url = process.env.DB_URL;
const dbName = process.env.DB_NAME;
let db;
MongoClient.connect(url, function(err, client) {
......@@ -10,7 +11,7 @@ MongoClient.connect(url, function(err, client) {
console.log(err);
}
else {
console.log('Connected successfully to server');
console.log('Connected successfully to db at "' + url + '/' + dbName + '"');
db = client.db(dbName);
}
});
......
This diff is collapsed.
......@@ -21,6 +21,7 @@
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"dotenv": "^8.0.0",
"express": "^4.17.0",
"mongodb": "^3.2.5"
},
......
// modules
require('dotenv').config();
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
......
version: "2"
version: '2'
services:
mongo-orders:
image: mongo
ports:
- "27017:27017"
depends_on:
- mongo-seed
- '27017:27017'
mongo-seed:
build: ./mongodb
depends_on:
- mongo-orders
api:
build: ./api
ports:
- "8080:8080"
- '8080:8080'
depends_on:
- mongo-orders
FROM mongo
FROM mongo:latest
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)}}))'
\ No newline at end of file
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment