Skip to content

Commit c929bce

Browse files
committed
Add Docker container functionality
Discord bot could potentially be Dockerized easily. Construct Docker container for it. This requires some adjustments: - Add build process to package.json to allow for multi-stage builds - Modify Logger to have JSON format for stdout. While having colorized output is nice for stdout, JSON is far more useful and the only method we can access said output for Docker containers. In the future, creating a LoggerManager might be the way to go, since we'll need to dependency inject envvars, among other things, which may let us dynamically allocate transports depending on environment. This could let us maintain colorized output locally whilst having JSON stdout logs for the Docker container environment.
1 parent 1a1c44a commit c929bce

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

Dockerfile

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FROM node:14-alpine AS builder
2+
WORKDIR /usr/src/bot
3+
COPY package*.json ./
4+
RUN npm ci
5+
COPY tsconfig.json ./
6+
COPY src src
7+
RUN npm run build
8+
9+
FROM node:14-alpine
10+
ENV NODE_ENV=production
11+
RUN apk add --no-cache tini curl
12+
WORKDIR /usr/src/bot
13+
COPY package*.json ./
14+
RUN npm install
15+
COPY --from=builder /usr/src/bot/dist dist
16+
RUN chown -R node:node .
17+
USER node
18+
WORKDIR /usr/src/bot/dist
19+
CMD ["tini", "--", "node", "src/index.js"]

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "ACM at UCSD's Discord Bot",
55
"main": "server.js",
66
"scripts": {
7+
"build": "tsc",
78
"lint": "eslint src --ext .ts",
89
"lint:fix": "eslint src --ext .ts --fix",
910
"test": "echo \"Error: no test specified\" && exit 1",

src/utils/Logger.ts

+2-14
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,11 @@ import {
22
createLogger, transports as Transports, format,
33
} from 'winston';
44
import 'winston-daily-rotate-file';
5-
import { DateTime } from 'luxon';
65

76
const {
8-
printf, combine, json, timestamp, colorize,
7+
combine, json, timestamp,
98
} = format;
109

11-
/**
12-
* Formatting for the standard output transport.
13-
*
14-
* Ideally, we don't have to read JSON whilst reading stdout, so we'll make a readable format
15-
* with the timestamp, log level and message.
16-
*/
17-
const consoleLogFormat = printf((information) => {
18-
const currentTime = DateTime.now().setZone('America/Los_Angeles').toISO();
19-
return `[${currentTime}] [${information.level}]: ${information.message}`;
20-
});
21-
2210
/**
2311
* Logger for the bot with split transports.
2412
*
@@ -30,7 +18,7 @@ export default createLogger({
3018
transports: [
3119
new Transports.Console({
3220
level: 'debug',
33-
format: combine(colorize(), consoleLogFormat),
21+
format: combine(timestamp(), json()),
3422
}),
3523
new Transports.DailyRotateFile({
3624
level: 'info',

0 commit comments

Comments
 (0)