Skip to content

Commit 037cb6d

Browse files
committed
[refactor] reduce Docker image size
[add] Health Check configuration of Docker services [optimize] update Upstream packages
1 parent b78ac1d commit 037cb6d

File tree

4 files changed

+504
-465
lines changed

4 files changed

+504
-465
lines changed

Dockerfile

+16-14
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
FROM node:18-slim
1+
# Reference: https://pnpm.io/docker#example-1-build-a-bundle-in-a-docker-container
22

3-
USER root
3+
FROM node:18-slim AS base
4+
RUN apt-get update && \
5+
apt-get install curl -y --no-install-recommends
6+
ENV PNPM_HOME="/pnpm"
7+
ENV PATH="$PNPM_HOME:$PATH"
8+
RUN corepack enable
9+
COPY . /app
10+
WORKDIR /app
411

5-
RUN npm rm yarn -g
6-
RUN npm i pnpm -g
12+
FROM base AS prod-deps
13+
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm i --prod --frozen-lockfile
714

8-
RUN mkdir /home/node/app
9-
WORKDIR /home/node/app
10-
11-
COPY package.json pnpm-lock.yaml /home/node/app/
12-
RUN pnpm i --frozen-lockfile
13-
14-
COPY . /home/node/app
15+
FROM base AS build
16+
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm i --frozen-lockfile
1517
RUN pnpm build
1618

17-
RUN pnpm prune --prod || true \
18-
pnpm store prune
19-
19+
FROM base
20+
COPY --from=prod-deps /app/node_modules /app/node_modules
21+
COPY --from=build /app/dist /app/dist
2022
EXPOSE 8080
2123
CMD ["npm", "start"]

docker-compose.yml

+12
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ services:
2929
- 8080:8080
3030
networks:
3131
- idea2app
32+
healthcheck:
33+
test: ['CMD-SHELL', 'curl -f http://localhost:8080/ || exit 1']
34+
interval: 3s
35+
retries: 5
36+
start_period: 30s
37+
labels:
38+
- autoheal=true
39+
restart: always
40+
autoheal:
41+
image: willfarrell/autoheal:1.2.0
42+
volumes:
43+
- /var/run/docker.sock:/var/run/docker.sock
3244
restart: always
3345
caddy:
3446
depends_on:

package.json

+15-15
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"dependencies": {
2121
"@koa/cors": "^4.0.0",
2222
"@koa/multer": "^3.0.2",
23-
"@koa/router": "^12.0.0",
23+
"@koa/router": "^12.0.1",
2424
"class-transformer": "^0.5.1",
2525
"class-validator": "^0.14.0",
2626
"cross-env": "^7.0.3",
@@ -31,7 +31,7 @@
3131
"koa-jwt": "^4.0.4",
3232
"koa-logger": "^3.2.1",
3333
"koa-mount": "^4.0.0",
34-
"koa2-swagger-ui": "^5.9.1",
34+
"koa2-swagger-ui": "^5.10.0",
3535
"koagger": "^0.3.0",
3636
"mobx-restful": "^0.6.12",
3737
"pg": "^8.11.3",
@@ -44,20 +44,20 @@
4444
"web-utility": "^4.1.3"
4545
},
4646
"devDependencies": {
47-
"@types/jsonwebtoken": "^9.0.3",
48-
"@types/koa": "^2.13.9",
49-
"@types/koa-logger": "^3.1.3",
50-
"@types/node": "^18.18.1",
51-
"@typescript-eslint/eslint-plugin": "^6.7.4",
52-
"@typescript-eslint/parser": "^6.7.4",
53-
"eslint": "^8.50.0",
47+
"@types/jsonwebtoken": "^9.0.5",
48+
"@types/koa": "^2.13.12",
49+
"@types/koa-logger": "^3.1.5",
50+
"@types/node": "^18.18.13",
51+
"@typescript-eslint/eslint-plugin": "^6.12.0",
52+
"@typescript-eslint/parser": "^6.12.0",
53+
"eslint": "^8.54.0",
5454
"eslint-plugin-simple-import-sort": "^10.0.0",
5555
"husky": "^8.0.3",
56-
"lint-staged": "^14.0.1",
57-
"prettier": "^3.0.3",
56+
"lint-staged": "^15.1.0",
57+
"prettier": "^3.1.0",
5858
"sqlite3": "^5.1.6",
5959
"ts-node-dev": "^2.0.0",
60-
"typescript": "~5.2.2"
60+
"typescript": "~5.3.2"
6161
},
6262
"prettier": {
6363
"singleQuote": true,
@@ -69,7 +69,7 @@
6969
"*.{md,json,yml,ts}": "prettier --write"
7070
},
7171
"scripts": {
72-
"prepare": "husky install",
72+
"prepare": "husky install || true",
7373
"dev": "ts-node-dev source/",
7474
"test": "lint-staged",
7575
"build": "rm -rf dist/ type/*.d.ts && tsc && mv dist/model/*.d.ts type/",
@@ -79,7 +79,7 @@
7979
"migration:run": "npm run typeorm -- migration:run",
8080
"upgrade:dev": "npm run migration:generate -- migration/version && npm run migration:run",
8181
"upgrade:pro": "cross-env NODE_ENV=production npm run migration:generate -- .data/version && npm run migration:run",
82-
"pack-image": "docker build -t rest-node-ts:latest .",
83-
"container": "docker rm -f rest-node-ts && docker run --name rest-node-ts -p 8080:8080 -d rest-node-ts:latest"
82+
"pack-image": "docker build -t idea2app/rest-node-ts:latest .",
83+
"container": "docker rm -f rest-node-ts && docker run --name rest-node-ts -p 8080:8080 -d idea2app/rest-node-ts:latest"
8484
}
8585
}

0 commit comments

Comments
 (0)