Skip to content

Commit d264483

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

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
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: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
# base-image for node on any machine using a template variable,
22
# see more about dockerfile templates here: https://www.balena.io/docs/learn/develop/dockerfile/#dockerfile-templates
33
# and about balena base images here: https://www.balena.io/docs/reference/base-images/base-images/
4-
FROM node:22-bookworm-slim
4+
# The image being used to build the application in multi-stage build
5+
FROM node:22-bookworm-slim AS build
56

67
# Defines our working directory in container
7-
WORKDIR /usr/src/app
8+
WORKDIR /build
89

910
# Copies the package.json first for better cache on later pushes
1011
COPY package*.json ./
1112

1213
# This install npm dependencies on the balena build server,
1314
# 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/*
15+
RUN JOBS=MAX npm ci --omit=dev
1516

1617
# This will copy all files in our root to the working directory in the container
1718
COPY . ./
1819

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

0 commit comments

Comments
 (0)