mirror of
https://github.com/docker-training/node-bulletin-board.git
synced 2025-07-01 21:39:31 +08:00
Merge pull request #1 from sixeyed/v3
Add logging & config for image optimization exercise
This commit is contained in:
commit
7b8510945e
48
bb-stack-final.yaml
Normal file
48
bb-stack-final.yaml
Normal file
@ -0,0 +1,48 @@
|
||||
version: '3.7'
|
||||
|
||||
services:
|
||||
bb-db:
|
||||
image: ${dockerId}/bb-db:v3
|
||||
build:
|
||||
context: ./bulletin-board-db
|
||||
networks:
|
||||
- bb-net
|
||||
volumes:
|
||||
- sqlbackup:/var/opt/mssql
|
||||
|
||||
bb-app:
|
||||
image: ${dockerId}/bb-app:v3.3
|
||||
build:
|
||||
context: ./bulletin-board-app
|
||||
dockerfile: Dockerfile.v3.3
|
||||
networks:
|
||||
- bb-net
|
||||
configs:
|
||||
- source: logConfig
|
||||
target: /app/config/logConfig.js
|
||||
secrets:
|
||||
- source: dbConfig
|
||||
target: /app/config/dbConfig.js
|
||||
|
||||
bb-proxy:
|
||||
image: ${dockerId}/bb-proxy:v3
|
||||
build:
|
||||
context: ./bulletin-board-proxy
|
||||
ports:
|
||||
- "80:80"
|
||||
networks:
|
||||
- bb-net
|
||||
|
||||
networks:
|
||||
bb-net:
|
||||
|
||||
volumes:
|
||||
sqlbackup:
|
||||
|
||||
configs:
|
||||
logConfig:
|
||||
external: true
|
||||
|
||||
secrets:
|
||||
dbConfig:
|
||||
external: true
|
33
bb-stack.yaml
Normal file
33
bb-stack.yaml
Normal file
@ -0,0 +1,33 @@
|
||||
version: '3.7'
|
||||
|
||||
services:
|
||||
bb-db:
|
||||
image: ${dockerId}/bb-db:v3
|
||||
build:
|
||||
context: ./bulletin-board-db
|
||||
networks:
|
||||
- bb-net
|
||||
volumes:
|
||||
- sqlbackup:/var/opt/mssql
|
||||
|
||||
bb-app:
|
||||
image: ${dockerId}/bb-app:v3
|
||||
build:
|
||||
context: ./bulletin-board-app
|
||||
networks:
|
||||
- bb-net
|
||||
|
||||
bb-proxy:
|
||||
image: ${dockerId}/bb-proxy:v3
|
||||
build:
|
||||
context: ./bulletin-board-proxy
|
||||
ports:
|
||||
- "80:80"
|
||||
networks:
|
||||
- bb-net
|
||||
|
||||
networks:
|
||||
bb-net:
|
||||
|
||||
volumes:
|
||||
sqlbackup:
|
@ -7,5 +7,4 @@ RUN npm install
|
||||
EXPOSE 8080
|
||||
CMD [ "npm", "start" ]
|
||||
|
||||
COPY . .
|
||||
|
||||
COPY . .
|
15
bulletin-board-app/Dockerfile.v3.1
Normal file
15
bulletin-board-app/Dockerfile.v3.1
Normal file
@ -0,0 +1,15 @@
|
||||
FROM node AS builder
|
||||
|
||||
WORKDIR /src
|
||||
COPY package.json .
|
||||
RUN npm install
|
||||
|
||||
# app image
|
||||
FROM node
|
||||
|
||||
EXPOSE 8080
|
||||
CMD npm start
|
||||
|
||||
WORKDIR /app
|
||||
COPY --from=builder /src/node_modules/ /app/node_modules/
|
||||
COPY . .
|
15
bulletin-board-app/Dockerfile.v3.2
Normal file
15
bulletin-board-app/Dockerfile.v3.2
Normal file
@ -0,0 +1,15 @@
|
||||
FROM node AS builder
|
||||
|
||||
WORKDIR /src
|
||||
COPY package.json .
|
||||
RUN npm install
|
||||
|
||||
# app image
|
||||
FROM node:alpine
|
||||
|
||||
EXPOSE 8080
|
||||
CMD npm start
|
||||
|
||||
WORKDIR /app
|
||||
COPY --from=builder /src/node_modules/ /app/node_modules/
|
||||
COPY . .
|
19
bulletin-board-app/Dockerfile.v3.3
Normal file
19
bulletin-board-app/Dockerfile.v3.3
Normal file
@ -0,0 +1,19 @@
|
||||
FROM node:10.15-stretch AS builder
|
||||
|
||||
WORKDIR /src
|
||||
COPY package.json .
|
||||
RUN npm install
|
||||
|
||||
# app image
|
||||
FROM node:10.15.3-alpine
|
||||
|
||||
EXPOSE 8080
|
||||
CMD [ "node", "server.js" ]
|
||||
|
||||
USER node
|
||||
HEALTHCHECK --interval=5s --timeout=10s --start-period=15s \
|
||||
CMD [ "node", "healthcheck.js" ]
|
||||
|
||||
WORKDIR /app
|
||||
COPY --from=builder /src/node_modules/ /app/node_modules/
|
||||
COPY . .
|
@ -1,20 +1,21 @@
|
||||
var db = require('./db.js');
|
||||
var db = require('./db');
|
||||
var log = require('../log');
|
||||
|
||||
exports.events = function (req, res) {
|
||||
console.log('Loading DB events...');
|
||||
log.Logger.info('Loading DB events...');
|
||||
db.Events
|
||||
.findAll()
|
||||
.then(events => {
|
||||
console.log('Fetched events, count: ' + events.length);
|
||||
log.Logger.debug('Fetched events, count: ' + events.length);
|
||||
res.json(events);
|
||||
})
|
||||
.catch(err => {
|
||||
console.error('** Fetch failed: ', err);
|
||||
log.Logger.error('** Fetch failed: ', err);
|
||||
});
|
||||
};
|
||||
|
||||
exports.event = function (req, res) {
|
||||
console.log('Handling event call, method: ' + req.method + ', event ID: ' + req.params.eventId)
|
||||
log.Logger.debug('Handling event call, method: ' + req.method + ', event ID: ' + req.params.eventId)
|
||||
switch(req.method) {
|
||||
case "DELETE":
|
||||
db.Events
|
||||
@ -23,7 +24,7 @@ exports.event = function (req, res) {
|
||||
id: req.params.eventId
|
||||
}
|
||||
}).then(function() {
|
||||
console.log('Deleted event with id: ' + req.params.eventId)
|
||||
log.Logger.info('Deleted event with id: ' + req.params.eventId)
|
||||
res.status(200).end();
|
||||
});
|
||||
break
|
||||
@ -35,6 +36,7 @@ exports.event = function (req, res) {
|
||||
date: req.body.date
|
||||
})
|
||||
.then(function() {
|
||||
log.Logger.info('Created event with title: ' + req.body.title)
|
||||
res.send('{}');
|
||||
res.status(201).end();
|
||||
});
|
||||
|
@ -1,25 +1,29 @@
|
||||
var Sequelize = require('sequelize');
|
||||
var username = 'sa';
|
||||
var password = 'DockerCon!!!';
|
||||
var host = 'bb-db';
|
||||
var dbName = 'BulletinBoard';
|
||||
var dbConfig = require('../config/dbConfig');
|
||||
var log = require('../log');
|
||||
|
||||
var sequelize = new Sequelize(dbName, username, password, {
|
||||
log.Logger.debug('Initializing connection to SQL Server: %s', dbConfig.connection.host);
|
||||
|
||||
var sequelize = new Sequelize(dbConfig.connection.dbName, dbConfig.connection.username, dbConfig.connection.password, {
|
||||
dialect: 'mssql',
|
||||
host: host,
|
||||
port: 1433,
|
||||
host: dbConfig.connection.host,
|
||||
port: dbConfig.connection.port,
|
||||
pool: {
|
||||
max: dbConfig.pool.max
|
||||
},
|
||||
dialectOptions: {
|
||||
requestTimeout: 30000
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
sequelize
|
||||
.authenticate()
|
||||
.then(() => {
|
||||
console.log('Successful connection to SQL Server.');
|
||||
log.Logger.info('Successful connection to SQL Server: %s', dbConfig.connection.host);
|
||||
log.Logger.info('--Using connection pool max: %d', dbConfig.pool.max)
|
||||
})
|
||||
.catch(err => {
|
||||
console.error('** SQL Server connection failed: ', err);
|
||||
log.Logger.error('** SQL Server connection failed: ', err);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
|
13
bulletin-board-app/config/dbConfig.js
Normal file
13
bulletin-board-app/config/dbConfig.js
Normal file
@ -0,0 +1,13 @@
|
||||
var dbConfig = module.exports = {};
|
||||
|
||||
dbConfig.connection = {
|
||||
username: 'sa',
|
||||
password: 'DockerCon!!!',
|
||||
host: 'bb-db',
|
||||
post: 1433,
|
||||
dbName: 'BulletinBoard'
|
||||
};
|
||||
|
||||
dbConfig.pool = {
|
||||
max: 10
|
||||
};
|
14
bulletin-board-app/config/logConfig.js
Normal file
14
bulletin-board-app/config/logConfig.js
Normal file
@ -0,0 +1,14 @@
|
||||
const { format, transports } = require('winston');
|
||||
var logConfig = module.exports = {};
|
||||
|
||||
logConfig.options = {
|
||||
format: format.combine(
|
||||
format.splat(),
|
||||
format.simple()
|
||||
),
|
||||
transports: [
|
||||
new transports.Console({
|
||||
level: 'debug'
|
||||
})
|
||||
]
|
||||
};
|
24
bulletin-board-app/healthcheck.js
Normal file
24
bulletin-board-app/healthcheck.js
Normal file
@ -0,0 +1,24 @@
|
||||
var http = require("http");
|
||||
|
||||
var options = {
|
||||
host : "localhost",
|
||||
port : "8080",
|
||||
timeout : 2000
|
||||
};
|
||||
|
||||
var request = http.request(options, (res) => {
|
||||
console.log(`STATUS: ${res.statusCode}`);
|
||||
if (res.statusCode == 200) {
|
||||
process.exit(0);
|
||||
}
|
||||
else {
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
|
||||
request.on('error', function(err) {
|
||||
console.log('ERROR');
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
request.end();
|
5
bulletin-board-app/log.js
Normal file
5
bulletin-board-app/log.js
Normal file
@ -0,0 +1,5 @@
|
||||
const winston = require('winston');
|
||||
var logConfig = require('./config/logConfig');
|
||||
|
||||
const logger = winston.createLogger(logConfig.options);
|
||||
exports.Logger = logger;
|
@ -14,7 +14,8 @@
|
||||
"vue-resource": "^0.1.17",
|
||||
"tedious": "^2.0.1",
|
||||
"sequelize": "^4.20.1",
|
||||
"prom-client": "^10.2.2"
|
||||
"prom-client": "^10.2.2",
|
||||
"winston": "3.2.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"body-parser": "^1.14.1",
|
||||
|
@ -5,7 +5,8 @@ var express = require('express'),
|
||||
morgan = require('morgan'),
|
||||
prometheus = require('prom-client'),
|
||||
routes = require('./backend'),
|
||||
api = require('./backend/api');
|
||||
api = require('./backend/api'),
|
||||
log = require('./log');
|
||||
|
||||
var app = module.exports = express();
|
||||
|
||||
@ -68,4 +69,4 @@ app.use((req, res, next) => {
|
||||
prometheus.collectDefaultMetrics();
|
||||
|
||||
app.listen(8080);
|
||||
console.log('Magic happens on port 8080...');
|
||||
log.Logger.info('Magic happens on port 8080...');
|
@ -1,4 +1,4 @@
|
||||
FROM microsoft/mssql-server-linux:2017-CU1
|
||||
FROM microsoft/mssql-server-linux:2017-CU13
|
||||
|
||||
ENV ACCEPT_EULA=Y \
|
||||
MSSQL_SA_PASSWORD=DockerCon!!!
|
||||
|
@ -1,4 +1,4 @@
|
||||
FROM nginx:1.13.6
|
||||
FROM nginx:1.15.10-alpine
|
||||
|
||||
RUN mkdir -p /data/nginx/cache
|
||||
COPY nginx.conf /etc/nginx/nginx.conf
|
Loading…
x
Reference in New Issue
Block a user