Skip to content

Commit d1d0149

Browse files
patch: Optimize Dockerfile
Signed-off-by: Vipul Gupta (@vipulgupta2048) <[email protected]>
1 parent edc3033 commit d1d0149

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

.dockerignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Don't send node_modules the daemon - we want the build server to install these.
2-
node_modules/
2+
node_modules
3+
npm-debug.log
34
.git
4-
repo.yml
5+
.gitignore

Dockerfile.template

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
1-
# base-image for node on any machine using a template variable,
21
# see more about dockerfile templates here: https://www.balena.io/docs/learn/develop/dockerfile/#dockerfile-templates
3-
# and about balena base images here: https://www.balena.io/docs/reference/base-images/base-images/
4-
FROM node:22-bookworm-slim
2+
# The image being used to build the application in multi-stage build
3+
FROM node:22-bookworm-slim AS build
54

65
# Defines our working directory in container
7-
WORKDIR /usr/src/app
6+
WORKDIR /build
87

98
# Copies the package.json first for better cache on later pushes
109
COPY package*.json ./
1110

12-
# This install npm dependencies on the balena build server,
13-
# making sure to clean up the artifacts it creates in order to reduce the image size.
14-
RUN JOBS=MAX npm install --production --unsafe-perm && npm cache verify && rm -rf /tmp/*
11+
# Install npm dependencies
12+
RUN JOBS=MAX npm ci --omit=dev
1513

1614
# This will copy all files in our root to the working directory in the container
1715
COPY . ./
1816

19-
# server.js will run when container starts up on the device
20-
CMD ["npm", "start"]
17+
# Image that will be used to run the application
18+
FROM node:22-bookworm-slim
19+
20+
ENV NODE_ENV=production
21+
WORKDIR /usr/src/app
22+
23+
COPY --from=build /build .
24+
25+
# Use node user instead of root for security
26+
USER node
27+
28+
# Use exec form for better signal handling
29+
CMD ["node", "server.js"]

0 commit comments

Comments
 (0)