Skip to content

Commit d8c1acf

Browse files
committed
First commit.
0 parents  commit d8c1acf

23 files changed

+7719
-0
lines changed

.dockerignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Dockerfile*
2+
docker-compose*
3+
.dockerignore
4+
npm-debug.log
5+
.git
6+
.gitignore
7+
node_modules
8+
dist
9+
build
10+
README.md
11+
.vscode

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/**

.eslintrc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"extends": [
3+
"koa",
4+
"prettier",
5+
"prettier/@typescript-eslint"
6+
],
7+
"plugins": [
8+
"prettier",
9+
"@typescript-eslint"
10+
],
11+
"parser": "@typescript-eslint/parser",
12+
"env": {
13+
"node": true,
14+
"mocha": true
15+
},
16+
"rules": {
17+
"arrow-parens": 0,
18+
"no-unused-vars": 0,
19+
}
20+
}

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
node_modules
2+
test.js
3+
coverage
4+
npm-debug.log
5+
.DS_Store
6+
.nyc_output
7+
.env.dev
8+
.env.test
9+
dist

Dockerfile

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# 1. --- Base ---
2+
FROM node:12.13-stretch-slim AS base
3+
4+
# install dependencies first, in a different location for easier app bind mounting for local development
5+
# due to default /opt permissions we have to create the dir with root and change perms
6+
RUN mkdir -p /opt/node_app && chown -R node:node /opt/node_app
7+
WORKDIR /opt/node_app
8+
9+
# the official node image provides an unprivileged user as a security best practice
10+
# but we have to manually enable it. We put it here so npm installs dependencies as the same
11+
# user who runs the app.
12+
# https://github.com/nodejs/docker-node/blob/master/docs/BestPractices.md#non-root-user
13+
USER node
14+
COPY package*.json ./
15+
16+
17+
# 2. --- Dependencies ---
18+
FROM base AS dependencies
19+
ENV NODE_ENV development
20+
ENV PORT 3000
21+
22+
RUN npm install
23+
ENV PATH /opt/node_app/node_modules/.bin:$PATH
24+
25+
# copy in our source code last, as it changes the most
26+
COPY --chown=node:node . .
27+
RUN npm run build
28+
CMD [ "npm", "run", "dev" ]
29+
30+
31+
# 3. --- Release ---
32+
FROM base AS release
33+
ENV NODE_ENV production
34+
ENV PORT 3000
35+
ENV PATH /opt/node_app/node_modules/.bin:$PATH
36+
37+
COPY --from=dependencies ./opt/node_app/dist ./dist
38+
RUN npm ci --only=production
39+
CMD [ "npm", "run", "start" ]
40+

Dockerfile-dev

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
FROM node:12.13-stretch-slim
2+
3+
ARG NODE_ENV=development
4+
ENV NODE_ENV $NODE_ENV
5+
6+
ARG PORT=3000
7+
ENV PORT $PORT
8+
EXPOSE $PORT
9+
10+
RUN mkdir -p /opt/node_app && chown -R node:node /opt/node_app
11+
WORKDIR /opt/node_app
12+
13+
# the official node image provides an unprivileged user as a security best practice
14+
# but we have to manually enable it. We put it here so npm installs dependencies as the same
15+
# user who runs the app.
16+
# https://github.com/nodejs/docker-node/blob/master/docs/BestPractices.md#non-root-user
17+
18+
USER node
19+
COPY package*.json ./
20+
RUN npm ci
21+
ENV PATH /opt/node_app/node_modules/.bin:$PATH
22+
23+
# copy in our source code last, as it changes the most
24+
COPY --chown=node:node . .
25+
26+
RUN tsc
27+
28+
CMD [ "npm", "run", "dev" ]

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Introduction
2+
This is a typescript koa project, with docker, docker compose, error handlers, test, prettier, eslint set up
3+
4+
# Setup
5+
## Install required tools
6+
* [docker setup](https://docs.docker.com/install/)
7+
* [docker-compose setup](https://docs.docker.com/compose/install/)
8+
9+
## Clone project
10+
* `git clone git@github.com:hanchiang/typescript-node-docker.git`
11+
12+
## Set up environment variables
13+
`.env.dev`:
14+
* `COOKIE_SECRET=mycookiesecret`
15+
* `NODE_ENV=development`
16+
17+
# Running the project
18+
**Run in development mode**
19+
* Build: `docker-compose build`
20+
* Start: `docker-compose up -d`
21+
* Server is available at: `localhost:6001`
22+
* Stop: `docker-compose down`
23+
24+
# Note
25+
Whenever new packages are added via `npm install`:
26+
* You need to stop the service: `docker-compose stop <service>` or `docker-compose down`
27+
* Build the service: `docker-compose build <service>`
28+
* Start the service: `docker-compose start <service>` or `docker-compose up -d`
29+
30+
# Reference
31+
* Multi stage docker build - https://medium.com/@ankit.wal/the-why-and-how-of-multi-stage-docker-build-with-typescript-example-bcadbce2686c

docker-compose.override.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
version: "3.4"
2+
3+
services:
4+
node-app:
5+
environment:
6+
- NODE_ENV=development
7+
- COOKIE_SECRET=cookiesecret
8+
ports:
9+
- 3000:3000

docker-compose.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
version: "3.4"
2+
3+
services:
4+
node-app:
5+
container_name: node-app
6+
build:
7+
context: .
8+
target: dependencies
9+
dockerfile: Dockerfile
10+
image: node-app:dev
11+
command: npm run dev
12+
volumes:
13+
- ./:/opt/node_app
14+
- ./package.json:/opt/node_app/package.json
15+
- ./package-lock.json:/opt/node_app/package-lock.json
16+
- /opt/node_app/node_modules

0 commit comments

Comments
 (0)